-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenaiwrapper.py
More file actions
34 lines (28 loc) · 1.95 KB
/
openaiwrapper.py
File metadata and controls
34 lines (28 loc) · 1.95 KB
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
import json
import openai
import logging
class OpenAI:
def __init__(self, logger):
self.logger = logger
self.auth()
def auth(self):
with open("secrets.json") as f:
secrets = json.load(f)
openai.api_key = secrets["openai"]
self.logger.info("Completed OpenAI authentication flow")
def write_draft(self, thread, update, myself, other, reply):
if reply:
user_content = f"Below is a chain of messages between myself, {myself}, and {other}. I would like to draft a response to {other} based on this interaction and the following update. Please incorporate this new information and formulate a response to {other} that I can send. Only write the body of the response, do not include a subject. Make sure you use the previous thread as context for your response. \n\nPAST MESSAGES:\n###\n{thread}\n###\n\nNEW INFORMATION TO RELAY:\n###\n{update}\n###"
else:
user_content = f"Below is the most recent thread of messages between myself, {myself}, and {other}. I would like to draft a new message to {other} based on the following update. Please use the past messages as context to our relationship, but don't necessarily refer back to these messages. Please formulate a new message to {other} that I can send. Only write the body of the email, do not include a subject. Please be formal but also friendly. \n\nPAST MESSAGES:\n###\n{thread}\n###\n\nNEW INFORMATION TO RELAY:\n###\n{update}\n###"
self.logger.info(f'Chat Completion prompt is: "{user_content}"')
model = "gpt-3.5-turbo"
# model = "gpt-4-1106-preview" # Use gpt-4 if you have access
self.logger.info(f"Chat Completion model is: {model}")
gpt_response = openai.ChatCompletion.create(
model=model, messages=[{"role": "user", "content": user_content}]
)
response = gpt_response.choices[0].message.content
return response
if __name__ == "__main__":
pass