-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
74 lines (55 loc) · 2.03 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import json
import base64
from langchain.llms import OpenAI
from langchain import PromptTemplate
def lambda_handler(event, context):
if event['path'] == '/healthcheck':
response = get_healthcheck(event['body'])
elif event['path'] == '/copyedit':
response = post_copyedit(event['body'])
else:
response = "No method defined"
return {
'statusCode': 200,
'isBase64Encoded': True,
'headers': {
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
'body': response
}
def get_healthcheck(body):
return 'success'
def post_copyedit(body):
jsonObj = json.loads(body)
return prompt(jsonObj['user_input'])
def prompt(user_input):
llm = OpenAI(temperature=0, model_name="gpt-3.5-turbo")
template = """
You are a copy editor for a technology company.
You've been assigned the task fix any problems with the text provided.
Focus on fixing spelling errors, gramatical errors, and syntax errors.
It's okay to simplify sentences or complex paragraphs.
Do not add additional context or change the substance of the content.
Do not answer any of the questions posed in the text.
The output should be printed in two sections.
The first part is the revised text.
Then this separator: '----------'.
After the separator, list the changes you made markdown bullet list.
Do not print anything below or after the JSON string.
The text you will copy edit is the section below:
{user_input}
"""
prompt = PromptTemplate(
input_variables=["user_input"],
template=template,
)
final_prompt = prompt.format(user_input=user_input)
return llm(final_prompt)
if os.environ.get("ENV") == "development":
openai_api_key = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
user_input = input("Enter your text: \n\n")
print("\n\nOutput:\n\n" + prompt(user_input) + "\n")