-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.py
123 lines (90 loc) · 3.8 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
from dataclasses import dataclass
import openai as openai
import rich
from halo import Halo
from rich.prompt import Prompt
from common import BOT_PREFIX
api_key = os.environ.get('OPENAI_API_KEY')
if not api_key:
print('OPENAI_API_KEY is not set!')
exit(1)
openai.api_key = api_key
@dataclass
class ChatMessage:
role: str
content: str
@classmethod
def of_system(cls, content):
return cls('system', content)
@classmethod
def of_assistant(cls, content):
return cls('assistant', content)
@classmethod
def of_user(cls, content):
return cls('user', content)
def to_dict(self):
return {
'role': self.role,
'content': self.content
}
class ChatHistory:
def __init__(self, messages: list[ChatMessage]):
self.messages = messages
def append_message(self, message: ChatMessage):
self.messages.append(message)
def to_array_of_dicts(self) -> list[dict[str, str]]:
return [
message.to_dict() for message in self.messages
]
def last_assistant_reply(self):
if len(self.messages) != 0 and self.messages[-1].role == 'assistant':
return self.messages[-1].content
else:
return None
class Chat:
def __init__(self, messages: list[ChatMessage], model='gpt-3.5-turbo'):
self.model = model
self.chat_history = ChatHistory(messages)
def run(self) -> ChatHistory:
with Halo(text='Thinking...', spinner='dots'):
result = openai.ChatCompletion.create(model=self.model, messages=self.chat_history.to_array_of_dicts())
content = result['choices'][0]['message']['content']
self.chat_history.append_message(ChatMessage.of_assistant(content))
return self.chat_history
class ChatWithHumanFeedback:
def __init__(self, messages: list[ChatMessage], model='gpt-3.5-turbo'):
self.model = model
self.chat_history = ChatHistory(messages)
def run(self) -> ChatHistory:
while True:
with Halo(text='Thinking...', spinner='dots'):
result = openai.ChatCompletion.create(model=self.model, messages=self.chat_history.to_array_of_dicts())
content = result['choices'][0]['message']['content']
self.chat_history.append_message(ChatMessage.of_assistant(content))
print(content)
user_input = input("Do you have any comments? If not just press Enter.")
if not user_input:
return self.chat_history
self.chat_history.append_message(ChatMessage.of_assistant(content))
self.chat_history.append_message(ChatMessage.of_user(user_input))
class ChatWithCallback:
def __init__(self, callback, messages: list[ChatMessage], model='gpt-3.5-turbo'):
self.model = model
self.chat_history = ChatHistory(messages)
self.callback = callback
def run(self):
while True:
with Halo(text='Thinking...', spinner='dots'):
response = openai.ChatCompletion.create(model=self.model,
messages=self.chat_history.to_array_of_dicts())
content = response['choices'][0]['message']['content']
self.chat_history.append_message(ChatMessage.of_assistant(content))
result = self.callback(content)
user_input = Prompt.ask(f"{BOT_PREFIX} Do you have any comments? If not just press Enter")
if not user_input:
rich.print(f'{BOT_PREFIX} OK, I will continue')
return result
rich.print(f'{BOT_PREFIX} OK, I will try again')
self.chat_history.append_message(ChatMessage.of_assistant(content))
self.chat_history.append_message(ChatMessage.of_user(user_input))