-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
36 lines (23 loc) · 789 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
31
32
33
34
35
36
import openai
openai.api_key = ''
class Chat():
def __init__(self, starters = [], model="gpt-3.5-turbo"):
self.starters = starters
self.dialogue = []
self.model = model
for s in self.starters:
self.ask(s)
self.user_prompt()
def ask(self, prompt):
self.dialogue.append({"role": "user", "content": prompt})
self.request()
def request(self):
result = openai.ChatCompletion.create(model=self.model, messages=self.dialogue)
output = result['choices'][0]['message']['content']
print(output)
self.dialogue.append({"role": "assistant", "content": output})
def user_prompt(self):
prompt = input('>>> ')
self.ask(prompt)
self.user_prompt()
Chat()