-
Notifications
You must be signed in to change notification settings - Fork 70
/
V3.py
283 lines (261 loc) · 10.1 KB
/
V3.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""
A simple wrapper for the official ChatGPT API
"""
import json
import requests
import tiktoken
class Chatbot:
"""
Official ChatGPT API
"""
def __init__(
self,
api_key: str,
engine: str = "gpt-3.5-turbo",
proxy: str = None,
max_tokens: int = 3000,
temperature: float = 0.5,
top_p: float = 1.0,
presence_penalty: float = 0.0,
frequency_penalty: float = 0.0,
reply_count: int = 1,
system_prompt: str = "You are ChatGPT, a large language model trained by OpenAI. Respond conversationally",
) -> None:
"""
Initialize Chatbot with API key (from https://platform.openai.com/account/api-keys)
"""
self.engine = engine
self.session = requests.Session()
self.api_key = api_key
self.proxy = proxy
self.system_prompt = system_prompt
self.max_tokens = max_tokens
self.temperature = temperature
self.top_p = top_p
self.presence_penalty = presence_penalty
self.frequency_penalty = frequency_penalty
self.reply_count = reply_count
self.sentence = ""
self.temp = ""
self.has_printed = False
if self.proxy:
proxies = {
"http": self.proxy,
"https": self.proxy,
}
self.session.proxies = proxies
self.conversation: dict = {
"default": [
{
"role": "system",
"content": system_prompt,
},
],
}
if max_tokens > 4000:
raise Exception("Max tokens cannot be greater than 4000")
if self.get_token_count("default") > self.max_tokens:
raise Exception("System prompt is too long")
def add_to_conversation(
self,
message: str,
role: str,
convo_id: str = "default",
) -> None:
"""
Add a message to the conversation
"""
self.conversation[convo_id].append({"role": role, "content": message})
def __truncate_conversation(self, convo_id: str = "default") -> None:
"""
Truncate the conversation
"""
while True:
if (
self.get_token_count(convo_id) > self.max_tokens
and len(self.conversation[convo_id]) > 1
):
# Don't remove the first message
self.conversation[convo_id].pop(1)
else:
break
# https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb
def get_token_count(self, convo_id: str = "default") -> int:
"""
Get token count
"""
if self.engine not in ["gpt-3.5-turbo", "gpt-3.5-turbo-0301"]:
raise NotImplementedError("Unsupported engine {self.engine}")
encoding = tiktoken.encoding_for_model(self.engine)
num_tokens = 0
for message in self.conversation[convo_id]:
# every message follows <im_start>{role/name}\n{content}<im_end>\n
num_tokens += 4
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
def get_max_tokens(self, convo_id: str) -> int:
"""
Get max tokens
"""
return self.max_tokens - self.get_token_count(convo_id)
def ask_stream(
self,
prompt: str,
lock=None,
stop_event=None,
role: str = "user",
convo_id: str = "default",
) -> None:
"""
Ask a question
"""
self.has_printed = False
# Make conversation if it doesn't exist
if convo_id not in self.conversation:
self.reset(convo_id=convo_id, system_prompt=self.system_prompt)
self.add_to_conversation(prompt, "user", convo_id=convo_id)
self.__truncate_conversation(convo_id=convo_id)
# Get response
response = self.session.post(
"https://api.openai.com/v1/chat/completions",
headers={"Authorization": f"Bearer {self.api_key}"},
json={
"model": self.engine,
"messages": self.conversation[convo_id],
"stream": True,
"temperature": self.temperature,
"top_p": self.top_p,
"presence_penalty":
self.presence_penalty,
"frequency_penalty":
self.frequency_penalty,
"n": self.reply_count,
"user": role,
"max_tokens": self.get_max_tokens(convo_id=convo_id),
},
stream=True,
)
if response.status_code != 200:
raise Exception(
f"Error: {response.status_code} {response.reason} {response.text}",
)
response_role: str = None
full_response: str = ""
for line in response.iter_lines():
if stop_event.is_set():
self.temp = ""
return
if not line:
continue
# Remove "data: "
line = line.decode("utf-8")[6:]
if line == "[DONE]":
break
resp: dict = json.loads(line)
choices = resp.get("choices")
if not choices:
continue
delta = choices[0].get("delta")
if not delta:
continue
if "role" in delta:
response_role = delta["role"]
if "content" in delta:
content = delta["content"] # .replace(" ", "") # (只能用于汉语发音严重不连续的情况,英文会导致空格丢失)
success = lock.acquire(blocking=False)
if success:
try:
self.sentence += self.temp + content # 确保每次别的线程都能拿到完整的句子,但是会保留前面的句子
self.temp = ""
finally:
lock.release()
else:
self.temp += content
print(content, end="")
full_response += content
print()
self.has_printed = True
self.add_to_conversation(full_response, response_role, convo_id=convo_id)
def rollback(self, n: int = 1, convo_id: str = "default") -> None:
"""
Rollback the conversation
"""
for _ in range(n):
self.conversation[convo_id].pop()
def reset(self, convo_id: str = "default", system_prompt: str = None) -> None:
"""
Reset the conversation
"""
self.conversation[convo_id] = [
{"role": "system", "content": system_prompt or self.system_prompt},
]
def save(self, file: str, *convo_ids: str) -> bool:
"""
Save the conversation to a JSON file
"""
try:
with open(file, "w", encoding="utf-8") as f:
if convo_ids:
json.dump({k: self.conversation[k] for k in convo_ids}, f, indent=2)
else:
json.dump(self.conversation, f, indent=2)
except (FileNotFoundError, KeyError):
return False
return True
# print(f"Error: {file} could not be created")
def load(self, file: str, *convo_ids: str) -> bool:
"""
Load the conversation from a JSON file
"""
try:
with open(file, encoding="utf-8") as f:
if convo_ids:
convos = json.load(f)
self.conversation.update({k: convos[k] for k in convo_ids})
else:
self.conversation = json.load(f)
except (FileNotFoundError, KeyError, json.decoder.JSONDecodeError):
return False
return True
def load_config(self, file: str, no_api_key: bool = False) -> bool:
"""
Load the configuration from a JSON file
"""
try:
with open(file, encoding="utf-8") as f:
config = json.load(f)
if config is not None:
self.api_key = config.get("api_key") or self.api_key
if self.api_key is None:
# Make sure the API key is set
raise Exception("Error: API key is not set")
self.engine = config.get("engine") or self.engine
self.temperature = config.get("temperature") or self.temperature
self.top_p = config.get("top_p") or self.top_p
self.presence_penalty = (
config.get("presence_penalty") or self.presence_penalty
)
self.frequency_penalty = (
config.get("frequency_penalty") or self.frequency_penalty
)
self.reply_count = config.get("reply_count") or self.reply_count
self.max_tokens = config.get("max_tokens") or self.max_tokens
if config.get("system_prompt") is not None:
self.system_prompt = (
config.get("system_prompt") or self.system_prompt
)
self.reset(system_prompt=self.system_prompt)
if config.get("proxy") is not None:
self.proxy = config.get("proxy") or self.proxy
proxies = {
"http": self.proxy,
"https": self.proxy,
}
self.session.proxies = proxies
except (FileNotFoundError, KeyError, json.decoder.JSONDecodeError):
return False
return True