-
Notifications
You must be signed in to change notification settings - Fork 19
/
lambda_function.py
48 lines (39 loc) · 1.29 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import openai
import json
import datetime
def query_completion(prompt: str, engine: str = 'text-davinci-003', temperature: float = 0.5, max_tokens: int = 1500, top_p: int = 1, frequency_penalty: int = 0.5, presence_penalty: int = 0.2) -> object:
"""
Function for querying GPT-3.
"""
estimated_prompt_tokens = int(len(prompt.split()) * 1.6)
estimated_answer_tokens = 2049 - estimated_prompt_tokens
response = openai.Completion.create(
engine=engine,
prompt=prompt,
temperature=temperature,
max_tokens=min(4096-estimated_prompt_tokens, max_tokens),
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty
)
return response
def lambda_handler(event, context):
'''Provide an event that contains the following keys:
- prompt: text of an open ai prompt
'''
openai.api_key = "YOUR_KEY_HERE"
print("Init:")
print(datetime.datetime.now())
print("Event:")
print(event)
body = json.loads(event['body'])
prompt = body['prompt']
max_tokens = 1500
response = query_completion(prompt)
response_text = response['choices'][0]['text'].strip()
response = {
"statusCode": 200,
"headers": {},
"body": response_text
}
return response