You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A REST API implementing each of the [30 example applications](https://platform.openai.com/examples) from the official [OpenAI API Documentation](https://platform.openai.com/docs/api-reference/making-requests?lang=python) using a modularized Terraform approach. Implemented as a serverless microservice using AWS [API Gateway](https://aws.amazon.com/api-gateway/), [Lambda](https://aws.amazon.com/lambda/) and the [OpenAI Python Library](https://pypi.org/project/openai/). Leverages OpenAI's suite of AI models, including [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5), [GPT-4](https://platform.openai.com/docs/models/gpt-4), [DALL·E](https://platform.openai.com/docs/models/dall-e), [Whisper](https://platform.openai.com/docs/models/whisper), [Embeddings](https://platform.openai.com/docs/models/embeddings), and [Moderation](https://platform.openai.com/docs/models/moderation).
7
15
8
16
- Creating new OpenAI applications and endpoints for this API only takes a few lines of code and is as easy as it is fun! Follow [this link](./terraform/apigateway_endpoints.tf) to see how each of these are coded.
9
17
-**Follow [this link](./doc/examples/README.md) for detailed documentation on each URL endpoint.**
10
18
11
-
## Usage
12
-
13
-
An example request and response. This endpoint inspects and corrects gramatical errors.
14
-
15
-
```console
16
-
curl --location --request PUT 'https://api.openai.yourdomain.com/examples/default-grammar' \
17
-
--header 'x-api-key: your-apigateway-api-key' \
18
-
--header 'Content-Type: application/json' \
19
-
--data '{"input_text": "She no went to the market."}'
20
-
```
21
-
22
-
return value
23
-
24
-
```json
25
-
{
26
-
"isBase64Encoded": false,
27
-
"statusCode": 200,
28
-
"headers": {
29
-
"Content-Type": "application/json"
30
-
},
31
-
"body": {
32
-
"id": "chatcmpl-7yLxpF7ZsJzF3FTUICyUKDe1Ob9nd",
33
-
"object": "chat.completion",
34
-
"created": 1694618465,
35
-
"model": "gpt-3.5-turbo-0613",
36
-
"choices": [
37
-
{
38
-
"index": 0,
39
-
"message": {
40
-
"role": "assistant",
41
-
"content": "The correct way to phrase this sentence would be: \"She did not go to the market.\""
42
-
},
43
-
"finish_reason": "stop"
44
-
}
45
-
],
46
-
"usage": {
47
-
"prompt_tokens": 36,
48
-
"completion_tokens": 10,
49
-
"total_tokens": 46
50
-
}
51
-
}
52
-
}
53
-
```
54
-
55
19
## Official YouTube Video For This Repo
56
20
57
21
[](https://www.youtube.com/watch?v=FqARAi8nS2M)
@@ -65,106 +29,6 @@ return value
65
29
-[OpenAI platform API key](https://platform.openai.com/).
66
30
*If you're new to OpenAI API then see [How to Get an OpenAI API Key](./doc/openai-api-key.md)*
67
31
68
-
## Setup
69
-
70
-
1. clone this repo and setup a Python virtual environment
2. add your OpenAI API credentials to the [.env](./.env) file in the root folder of this repo. Your organization ID and API Key should appear similar in format to these examples below.
*Windows/Powershell users: you'll need to modify [./terraform/lambda_openai.tf](./terraform/lambda_openai.tf) data "external" "env" as per instructions in this code block.*
86
-
87
-
88
-
3. Add your AWS account number and region to Terraform. Set these three values in [terraform.tfvars](./terraform/terraform.tfvars):
89
-
90
-
```terraform
91
-
account_id = "012345678912" # Required: your 12-digit AWS account number
92
-
aws_region = "us-east-1" # Optional: an AWS data center
93
-
aws_profile = "default" # Optional: for aws cli credentials
94
-
```
95
-
96
-
*see the README section **"Installation Prerequisites"** below for instructions on setting up Terraform for first-time use.*
97
-
98
-
4. Build and deploy the microservice..
99
-
100
-
```terraform
101
-
terraform init
102
-
terraform apply
103
-
```
104
-
105
-
*Note the output variables for your API Gateway root URL and API key.*
If you manage a domain name using AWS Route53 then you can optionally deploy this API using your own custom domain name. Modify the following variables in [terraform/terraform.tfvars](./terraform/terraform.tfvars) and Terraform wil take care of the rest.
1. a JSON object and custom headers are added to an HTTP request and sent to the API as a 'PUT' method.
126
-
2. API Gateway uses a [Request Mapping Template](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html) in a non-proxy Lambda integration request to combine user request text with your OpenAPI application definition, and then forward the combined data as a custom JSON object to a Lambda function.
127
-
3. Lambda parses and validates the custom JSON object and then invokes the OpenAI API, passing your api key which is stored as a Lambda environment variable.
128
-
4. OpenAI API results are returned as JSON objects.
129
-
5. Lambda creates a custom JSON response containing the http response body as well as system information for API Gateway.
130
-
6. API Gateway passes through the http response to the client.
131
-
132
-
You'll find a detailed narrative explanation of the design strategy in this article, [OpenAI API With AWS Lambda](https://blog.lawrencemcdaniel.com/openai-api-with-aws-lambda/)
133
-
134
-
### Services and Technologies Used
135
-
136
-
***[OpenAI](https://pypi.org/project/openai/)**: a PyPi package thata provides convenient access to the OpenAI API from applications written in the Python language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the OpenAI API.
137
-
***[API Gateway](https://aws.amazon.com/api-gateway/)**: an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs at any scale.
138
-
***[IAM](https://aws.amazon.com/iam/)**: a web service that helps you securely control access to AWS resources. With IAM, you can centrally manage permissions that control which AWS resources users can access. You use IAM to control who is authenticated (signed in) and authorized (has permissions) to use resources.
139
-
***[Lambda](https://aws.amazon.com/lambda/)**: an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code. It was introduced on November 13, 2014.
140
-
***[CloudWatch](https://aws.amazon.com/cloudwatch/)**: CloudWatch enables you to monitor your complete stack (applications, infrastructure, network, and services) and use alarms, logs, and events data to take automated actions and reduce mean time to resolution (MTTR).
141
-
***[Route53](https://aws.amazon.com/route53/)**: (OPTIONAL). a scalable and highly available Domain Name System service. Released on December 5, 2010.
142
-
***[Certificate Manager](https://aws.amazon.com/certificate-manager/)**: (OPTIONAL). handles the complexity of creating, storing, and renewing public and private SSL/TLS X.509 certificates and keys that protect your AWS websites and applications.
143
-
144
-
## OpenAI API
145
-
146
-
This project leverages the official [OpenAI PyPi](https://pypi.org/project/openai/) Python library. The openai library is added to the AWS Lambda installation package. You can review [terraform/lambda_openai_text.tf](./terraform/lambda_openai_text.tf) to see how this actually happens from a technical perspective.
147
-
148
-
Other reference materials on how to use this libary:
149
-
150
-
-[How to Get an OpenAI API Key](./doc/openai-api-key.md)
151
-
-[OpenAI Official Example Applications](https://platform.openai.com/examples)
152
-
-[OpenAI API Documentation](https://platform.openai.com/docs/api-reference/making-requests?lang=python)
-[OpenAI Official Cookbook](https://github.com/openai/openai-cookbook/)
156
-
157
-
Be aware that the OpenAI platform API is not free. Moreover, the costing models vary signficantly across the family of OpenAI models. GPT-4 for example cost significantly more to use than GPT-3.5. Having said that, for development purposes, the cost likely will be negligible. I spent a total of around $0.025 USD while developing and testing the initial release of this project, whereupon I invoked the openai api around 200 times (rough guess).
158
-
159
-
## Trouble Shooting and Logging
160
-
161
-
The terraform scripts will automatically create a collection of CloudWatch Log Groups. Additionally, note the Terraform global variable 'debug_mode' (defaults to 'true') which will increase the verbosity of log entries in the [Lambda functions](./terraform/python/), which are implemented with Python.
162
-
163
-
I refined the contents and formatting of each log group to suit my own needs while building this solution, and in particular while coding the Python Lambda functions.
0 commit comments