<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Felipe Curty</title>
    <description>The latest articles on DEV Community by Felipe Curty (@felipecrp).</description>
    <link>https://dev.to/felipecrp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F843184%2F4043a8ad-4e7d-4145-adea-27554903f2f5.png</url>
      <title>DEV Community: Felipe Curty</title>
      <link>https://dev.to/felipecrp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/felipecrp"/>
    <language>en</language>
    <item>
      <title>Creating a lambda function to call ChatGPT</title>
      <dc:creator>Felipe Curty</dc:creator>
      <pubDate>Mon, 13 Feb 2023 03:20:06 +0000</pubDate>
      <link>https://dev.to/aws-builders/creating-a-lambda-function-to-call-chatgpt-pj</link>
      <guid>https://dev.to/aws-builders/creating-a-lambda-function-to-call-chatgpt-pj</guid>
      <description>&lt;p&gt;This post will explain creating a Lambda function to call ChatGPT API. It will provide the instructions and the python code to create and deploy the function as an API. &lt;/p&gt;

&lt;p&gt;Lambda is a serverless computing platform provided by Amazon Web Services (AWS). Using Lambda, developers can create applications without the need to manage their infrastructure. Lambda also enables applications to auto-scale according to demand, making the platform cost-effective (pay only for what you use).&lt;/p&gt;

&lt;p&gt;ChatGPT is a product developed by OpenAI to enable users to talk with an advanced language model. Using ChatGPT, developers can create applications that automatically answer questions, generate and complete text, summarize long articles, and translate text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Notes:&lt;/strong&gt; This post is intended for learning purposes and will create a public API without protecting the API key secret. In a production environment it would be advisable enabling authentication and protecting the API secret key.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Lambda function
&lt;/h2&gt;

&lt;p&gt;The first step is to create a Lambda function. On the AWS dashboard, enter the Lambda Service and create a new function from scratch. The following are the parameters to create the Lambda Function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Author from scratch
- Basic Information:
  - Function name: chatGPT
  - Runtime: Python 3.9 (or greater)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Lambda function &lt;em&gt;chatGPT&lt;/em&gt; is created. The console provides the &lt;em&gt;Code&lt;/em&gt; tab to insert the code, but since the function is empty, it does nothing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Calling the Open AI API
&lt;/h2&gt;

&lt;p&gt;We need to create an &lt;em&gt;API key&lt;/em&gt; to call the Open AI API. First, we need to navigate to the &lt;a href="https://platform.openai.com/overview" rel="noopener noreferrer"&gt;Open AI API site&lt;/a&gt; and create an account. There is a &lt;em&gt;View API Keys&lt;/em&gt; link on the user profile page, where we can create a new secret key to call the API.&lt;/p&gt;

&lt;p&gt;Once the API key is created, you can create the Lambda function code. The code below implements a Lambda function that call the OpenAI API. In a production environment, it would be safer to store the API-KEY in a vault or at least in an environment variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lambda_handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;API-KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;question&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;question&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;openai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Completion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;text-davinci-003&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;question&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;temperature&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;max_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;statusCode&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;body&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;choices&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;text&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code depends on the &lt;em&gt;openai&lt;/em&gt; lib, which you need to install to run the Lambda function. There are some &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/python-package.html" rel="noopener noreferrer"&gt;tutorials&lt;/a&gt; that explain how to install python dependencies in a Lambda environment.&lt;/p&gt;

&lt;p&gt;Moreover, the Open AI service my take some time to run. Hence, we need to configure and increase the Lambda function timeout. In the &lt;em&gt;Configuration&lt;/em&gt; tab, you can navigate to &lt;em&gt;General Configuration&lt;/em&gt; and change the &lt;em&gt;Timeout&lt;/em&gt; to 10 seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the Lambda Function
&lt;/h2&gt;

&lt;p&gt;The code is ready to run and now we need to test it. In the &lt;em&gt;Test&lt;/em&gt; tab, we can create a new test event and write the following &lt;em&gt;Event JSON&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"question"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Provide a brief description of aws lambda"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After run the test, we expect a message telling that the execution result succeeded. We can expand the result to check the Open AI result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"statusCode"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"body"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s2"&gt;AWS Lambda is a serverless computing service provided by Amazon Web Services (AWS). Lambda allows developers to run code without managing any infrastructure. Lambda executes code in response to certain events, such as an HTTP request or an upload to an Amazon S3 bucket. Lambda is compatible with a variety of programming languages, including Node.js, Java, Python and C#."&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying the Lambda function
&lt;/h2&gt;

&lt;p&gt;We need to create a trigger calling the Lambda function to publish the function. Since we want to create an API, we will use the API Gateway trigger. Click on the &lt;em&gt;add trigger&lt;/em&gt; button, choose the API Gateway, and provide the following parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API Gateway
- Intent: Create a new API
- API Type: REST API
- Security: Open
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once created, the trigger will provide a URL to call the API.&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
