-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
50 lines (37 loc) · 1.25 KB
/
util.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
import openai
API_KEY = "YOUR-OPEN-AI-API-KEY"
openai.api_key = API_KEY
def getResponse(prompt):
global conversation
conversation.append({"role": "user", "content": prompt})
conversation = chatGPTResponse(conversation)
return conversation[-1]["content"].strip()
# Function that generates the response from ChatGPT
def chatGPTResponse(conversation):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=conversation
)
except openai.error.APIConnectionError:
return None
conversation.append(
{
"role": response.choices[0].message.role,
"content": response.choices[0].message.content,
}
)
return conversation
def initializeConversation():
global conversation
conversation = []
conversation.append({"role": "system", "content": "How may I help you?"})
conversation = chatGPTResponse(conversation)
# Below code is just for testing purpose
if __name__ == "__main__":
choice = 1
initializeConversation()
while choice != 0:
prompt = input("Enter your prompt: ")
response = getResponse(prompt)
print(response)
choice = int(input())