-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chatgpt-SelfTalk.py
50 lines (40 loc) · 1.31 KB
/
Chatgpt-SelfTalk.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 pprint
import openai
import time
#Insert your Key
openai.api_key = ''
# Appends the chat to the list
def update_chat(messages, role, content):
messages.append({"role": role, "content": content})
return messages
# Creates an response
def get_chatgpt_response(messages):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
return response['choices'][0]['message']['content']
# Pre defined chat
messages = [{'content': 'This is a conversation between 2 good friends talking about '
'their feelings',
'role': 'system'},
{'content': 'Anna: Hey Max! How are you', 'role': 'assistant'},
{'content': 'Max: Hey Anna. Not really good at the moment',
'role': 'assistant'}]
# Counter to break loop
c = 0
# Chat Loop
while True:
# Error handler
try:
pprint.pprint(messages)
model_response = get_chatgpt_response(messages)
messages = update_chat(messages, "assistant", model_response)
model_response = get_chatgpt_response(messages)
messages = update_chat(messages, "assistant", model_response)
# Puts programm to sleep if Tokens/min are exceeded
except (openai.error.RateLimitError):
time.sleep(60)
c += 1
if c >= 15:
break