Design ChatGPT Chatbot API using AWS Services

Neha Tomar
2 min readJan 5, 2023

--

The AI revolution is stunning the globe. Big Language Models have emerged for never leaving. Organizations such as OpenAI, and Meta have created incredible technologies that can augment human capabilities… if people understand how to utilise them.

In this blog, I am explaining step-by-step how to use the OpenAI API, specifically the ChatGPT model, to adapt a custom solution you’d like. The good thing is that we are building it using a serverless stack, so have to pay usage charges whenever we use

Solution Diagram

Fig: Solution to access ChatGPT Chatbot API using AWS Serverless Stack

Prerequisites:

Design Steps:

We will follow below steps for end to end setup.

  1. Setup ChatGPT API Keys
  2. Create Lambda function
  3. Connect API Gateway to Lambda

Step1 : Setup ChatGPT API Keys:

Follow steps mentioned in https://help.openai.com/en/articles/4936850-where-do-i-find-my-secret-api-key to generate the API Keys

Step 2: Create Lambda function

Create a AWS Lambda function with below source code.

import os
import openai
import urllib3
import json
import re

def lambda_handler(event, context):
secret_id = "OPENAI"
secret_name = "OPENAI_API_KEY"
auth_headers = {"X-Aws-Parameters-Secrets-Token": os.environ.get('AWS_SESSION_TOKEN')}

http = urllib3.PoolManager()
r = http.request("GET", "http://localhost:2773/secretsmanager/get?secretId=" + secret_id, headers=auth_headers)

parameter = json.loads(r.data)
OPENAI_API_KEY = json.loads(parameter["SecretString"])[secret_name]
openai.api_key = OPENAI_API_KEY
response = openai.Completion.create(
model="text-davinci-003",
prompt="Get a random motivational quote",
temperature=0.9,
max_tokens=150
)
quote = response["choices"][0]["text"]
return (quote)

Above code will first retrieve the OpenAPI API key from the AWS Secrets Manager. Then it will call the ChatGPT API and asks for a motivational quote and returns it to the user. For the full documentation of the ChatGPT module, you can refer to the official documentation here.

Make sure that the Lambda policy is allowed to retrieve the API key from the AWS Secrets Manager and decrypt it. Below is the sample Lambda Policy.

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"secretsmanager:GetSecretValue",
"kms:Decrypt"
],
"Resource": [
"arn:aws:secretsmanager:<region>:<accountID>:secret:<secretName>",
"arn:aws:kms:<region>:<accountID>:key/<keyID>"
]
}
]
}

Step 2: Connect API Gateway to Lambda

You can create a web API with an HTTP endpoint for your Lambda function by using Amazon API Gateway. API Gateway provides tools for creating and documenting web APIs that route HTTP requests to Lambda functions. You can secure access to your API with authentication and authorization controls. Your APIs can serve traffic over the internet or can be accessible only within your VPC. Please follow steps mentioned in AWS documentation for same.

Conclusion

In this blog we discussed detailed step-by-step approach for implementing ChatGPT on AWS. We have explained the capabilities of ChatGPT and how it can be integrated. Furthermore, we have provided an example of a use case for ChatGPT on AWS.

By following this guide, you should now have a solid understanding of how to securely implement ChatGPT on AWS.

--

--

Neha Tomar
Neha Tomar

No responses yet