-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
55 lines (45 loc) · 1.33 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
51
52
53
54
55
from config import get_API
import openai
#API_KEY = 'replace with your API-KEY'
openai.api_key = get_API()
#Function that generates the reply from the 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
#Initializing the AI
def initializeConversation():
global conversation
conversation = []
conversation.append(
{
'role':'system',
'content' : 'How may I help you'
}
)
conversation = chatGPTResponse(conversation)
#Function that return the prompt
def getResponse(prompt):
global conversation
conversation.append({'role':'user','content' : prompt})
conversation = chatGPTResponse(conversation)
return conversation[-1]['content'].strip()
if __name__ == "__main__":
choice = 1
initializeConversation();
while (choice != 0):
prompt = input("Enter your prompt: ")
response = getResponse(prompt)
print(response)
choice = int(input())