Creating GPT-3 powered API with AWS lambda and API gateway

Creating GPT-3 powered API with AWS lambda and API gateway


GPT-3 is a state-of-the-art machine learning model developed by OpenAI. It is capable of generating human-like text, making it a powerful tool for various natural languages processing tasks such as text generation, summarization, question answering, and more. Creating a product description generator using GPT-3 allows for the efficient and accurate generation of product descriptions.

AWS Lambda

AWS Lambda provides an easy way to run your code without having to worry about managing or maintaining servers. With just a few simple steps, you can create a serverless application and focus on writing your code. Define a lambda function that can be invoked in response to a certain event and let Lambda take care of the rest. It can automatically scale to handle a high number of requests and charges only for the computing time consumed.

API Gateway

API Gateway, when used in conjunction with Lambda, provides a powerful solution for creating GPT-3 powered API. It allows you to create, manage and monitor all of your application's APIs in one place and control access, throttling, caching and track usage and error rates.

Creating GPT-3 powered API

To create a GPT-3 powered API using AWS Lambda and API Gateway, simply create a Lambda function that invokes the GPT-3 API and returns the response to the user. Then, create an API Gateway that allows users to access your Lambda function. Connect your API Gateway to your Lambda function so that when a user requests your API, it triggers the Lambda function to run.

Here is an example of one such use case:

In the below example, we are trying to create a product description generator using GPT-3.

There are several reasons why one may choose to use GPT-3 for creating a product description generator.

  1. Efficiency: GPT-3 can generate human-like text quickly and accurately, allowing for the efficient generation of product descriptions.

  2. Consistency: GPT-3 can ensure consistency in the tone and style of product descriptions, resulting in a more professional and polished final product.

  3. Scalability: As GPT-3 can generate text at high speed, it can handle large amounts of product descriptions, making it a scalable solution for businesses with many products.

  4. Cost-effective: Since GPT-3 is a pre-trained model, it doesn't require extensive data and computational resources to train. This makes it a cost-effective solution as compared to developing a custom model.

  5. Personalization: GPT-3 can generate product descriptions that are personalized to the attributes of the specific product, which can be a great way to target a specific audience.

Overall, using GPT-3 to generate product descriptions can save time and resources while also producing high-quality, consistent, and personalized descriptions for a wide range of products.

import json
import openai

def lambda_handler(event,context):
    openai.api_key="your api key"

    prompt=f'''Write a precise product description for marketing and SEO purposes based on below product attributes.
product name: Acer Nitro XV272U KV
category: Monitor
size: 27 inch
refresh rate: 165hz

product description:
Experience all your content like never before with the Acer Nitro XV272U KV Monitor. This 27" monitor offers a 165hz refresh rate, ensuring that your visuals are always crisp and clear. 


product name: Samsung 192 L 2 Star
category: Refrigerator
color: Grey Silver
number of doors: 2

product description:
The Samsung 192L 2 Star Refrigerator is the perfect addition to any kitchen. Featuring two doors and a sleek grey silver color, this refrigerator provides ample storage space and energy efficient cooling.



product description:'''



    return {
        "statusCode": 200,
        "body": json.dumps(final),

Event Object

The first argument is the event object. An event is a JSON-formatted document that contains data for a Lambda function to process. The Lambda runtime converts the event to an object and passes it to your function code. It is usually of the Python dict type.

The event body in this example is a JSON object containing the attribute of a product. This attribute information is used as input for the GPT-3 model to generate a product description.

The structure of the event body in this example is as follows:

"attributes": {
"product_name": "Acer Nitro XV272U KV",
"category": "Monitor",
"size": "27 inch",
"refresh_rate": "165hz"

This event body structure is passed as an input to the lambda function when the API Gateway is triggered by a user request. This input data is extracted from the event object in the lambda function and used to generate the product description.

OpenAI Request Parameters for GPT-3 Text Generation

  • "prompt" is the input text or question for the GPT-3 model

  • "temperature" controls the deterministic nature of the model

  • "max tokens" sets the maximum number of tokens to generate

  • "top_p" sets the proportion of the mass of the distribution to sample from

  • "frequency_penalty" decreases the likelihood of generating common tokens

  • "presence_penalty" decreases the likelihood of generating tokens already present in the prompt.

If you want to learn more about GPT-3, check out this playlist on YouTube

Brief Overview of the Above Code:

  • The JSON and OpenAI libraries are imported for use in the function.

  • The function lambda_handler takes in two parameters, event and context, with the event containing the attribute information as a JSON object in the body of the request.

  • The OpenAI API key is set and the attribute information is extracted from the event object.

  • A prompt is created by combining the attribute information with examples of product descriptions.

  • The openai.Completion.create method is used to generate a new product description based on the prompt and specified parameters.

  • The generated product description is stored in a dictionary and returned as a JSON object in the response.

It is important to use a tool like postman to test the request and response of the GPT-3 product description generator using AWS Lambda and API Gateway. To see the request and response structure, you can refer to the Postman screenshot.

Youtube Video on Creating a Product Description Generator using GPT-3 is a valuable resource for those who want to stay informed about the latest AI tools and technologies. It provides an incredible opportunity to explore the potential of AI through video demos. The website's goal is to educate and inform about the many possibilities of AI, making it an essential stop for anyone interested in this field. The website is updated regularly with new demos, so it's a good idea to visit on a regular basis to stay up-to-date on the latest AI tools.