This README provides an overview of the deployment process. Be sure to customize your Lambda function's configuration, environment variables, and IAM permissions according to your specific requirements.
Follow these steps to deploy and run this Lambda function on AWS:
-
Set up your AWS Account:
If you haven't already, create an AWS account or use an existing one.
-
Configure AWS CLI:
Install the AWS Command Line Interface (CLI) and configure it with your AWS credentials.
pip install awscli aws configure
-
Package Dependencies:
Package your Lambda function along with its dependencies into a deployment package.
pip install -r requirements.txt -t ./
-
Create a Deployment Package:
Create a ZIP archive containing your Lambda function and its dependencies.
zip -r lambda_function.zip ./* -
Create an IAM Role:
Create an IAM Role with permissions for your Lambda function. Ensure it has permissions to call the OpenAI API and any other AWS services you might use.
-
Deploy the Lambda Function:
Use the AWS CLI to create the Lambda function. Replace
YOUR_ROLE_ARNwith the ARN of the IAM Role you created.aws lambda create-function --function-name openai-api-lambda \ --runtime python3.8 \ --role YOUR_ROLE_ARN \ --handler lambda_function.lambda_handler \ --zip-file fileb://lambda_function.zip
To deploy this script as an AWS Lambda function, follow these steps:
-
Create an AWS Lambda function using the AWS Management Console or AWS CLI.
-
Upload the Lambda deployment package, including your code and any required dependencies.
-
Set the environment variable
OPENAI_API_KEYin your Lambda function configuration with your OpenAI API key. -
Configure the Lambda function's handler to be
lambda_function.lambda_handler, wherelambda_functionis the name of your script. -
Monitor your Lambda function's execution and use AWS CloudWatch Logs for troubleshooting.
Certainly, here are steps 5 to 11 related to setting up the API Gateway:
-
Create an API Gateway:
- In the AWS Management Console, navigate to API Gateway.
- Click on "Create API" and choose "HTTP API" or "REST API" based on your requirements.
-
Create a New Resource and Method:
- Inside your API, create a new resource (e.g.,
/myresource) and a method (e.g., POST) under that resource.
- Inside your API, create a new resource (e.g.,
-
Enable CORS for Your API Gateway Resource:
- Select the resource you just created.
- In the "Actions" dropdown, click on "Enable CORS."
- Configure CORS settings as needed to allow cross-origin requests.
-
Enable Lambda Proxy Integration:
- In the method configuration, under "Integration," choose "Lambda Function" as the integration type.
- Select the Lambda function you previously created.
-
Use the Mapping Template:
-
In the method configuration, under "Integration Request," choose "Mapping Templates."
-
Add a new mapping template with content:
#set($allParams = $input.params()) { "body-json": $input.json('$'), "context" : { "http-method" : "$context.httpMethod" } }
-
Save the mapping template.
-
-
Deploy Your API:
- In the API Gateway menu, select "Deployments."
- Create a new deployment stage (e.g., "prod" or "test").
- Deploy your API to the stage you created.
-
Obtain the Endpoint URL:
- After deployment, your API will have an endpoint URL (e.g., https://your-api-id.execute-api.us-east-1.amazonaws.com/your-stage).
- This URL is where you will make API requests.
With these steps completed, your API Gateway is configured to pass incoming requests to your Lambda function. You can now make API requests to your endpoint URL, and the Lambda function will process them.
To generate code using the API, make a POST request to your API Gateway endpoint with the following JSON payload:
{
"action": "GenerateCode",
"language": "Java",
"package": "",
"request": {
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "java binary search for an array of 10 elements"
}
],
"temperature": 1,
"max_tokens": 2048,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
}The Lambda function will process the request and return the generated code.