-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
30 lines (22 loc) · 828 Bytes
/
chat.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
import openai
import requests
import config
def grab_ai_message(response, conversation_history):
ai_message = response.choices[0].message.content
conversation_history.append({"role": "assistant", "content": ai_message})
return ai_message
def chat_with_openai(user_input, conversation_history):
# Add the user's message to the conversation history
conversation_history.append({"role": "user", "content": user_input})
# Call the OpenAI API amd send conversation history
try:
response = openai.chat.completions.create(
model=config.chat_model,
messages=conversation_history
)
return response
except requests.Timeout:
print("Open AI did not respond...")
return None
except Exception as e:
print("OpenAI Error: ", e)