JohnSmith9982 commited on
Commit
ee1a637
1 Parent(s): 1bd67bd

Upload 3 files

Browse files
Files changed (3) hide show
  1. ChuanhuChatbot.py +159 -0
  2. presets.py +10 -0
  3. utils.py +133 -120
ChuanhuChatbot.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ # import openai
3
+ import os
4
+ import sys
5
+ import argparse
6
+ from utils import *
7
+ from presets import *
8
+
9
+
10
+ my_api_key = "" # 在这里输入你的 API 密钥
11
+
12
+ #if we are running in Docker
13
+ if os.environ.get('dockerrun') == 'yes':
14
+ dockerflag = True
15
+ else:
16
+ dockerflag = False
17
+
18
+ authflag = False
19
+
20
+ if dockerflag:
21
+ my_api_key = os.environ.get('my_api_key')
22
+ if my_api_key == "empty":
23
+ print("Please give a api key!")
24
+ sys.exit(1)
25
+ #auth
26
+ username = os.environ.get('USERNAME')
27
+ password = os.environ.get('PASSWORD')
28
+ if not (isinstance(username, type(None)) or isinstance(password, type(None))):
29
+ authflag = True
30
+ else:
31
+ if not my_api_key and os.path.exists("api_key.txt") and os.path.getsize("api_key.txt"):
32
+ with open("api_key.txt", "r") as f:
33
+ my_api_key = f.read().strip()
34
+ if os.path.exists("auth.json"):
35
+ with open("auth.json", "r") as f:
36
+ auth = json.load(f)
37
+ username = auth["username"]
38
+ password = auth["password"]
39
+ if username != "" and password != "":
40
+ authflag = True
41
+
42
+ gr.Chatbot.postprocess = postprocess
43
+
44
+ with gr.Blocks(css=customCSS) as demo:
45
+ gr.HTML(title)
46
+ with gr.Row():
47
+ keyTxt = gr.Textbox(show_label=False, placeholder=f"在这里输入你的OpenAI API-key...",
48
+ value=my_api_key, type="password", visible=not HIDE_MY_KEY).style(container=True)
49
+ use_streaming_checkbox = gr.Checkbox(label="实时传输回答", value=True, visible=enable_streaming_option)
50
+ chatbot = gr.Chatbot() # .style(color_map=("#1D51EE", "#585A5B"))
51
+ history = gr.State([])
52
+ token_count = gr.State([])
53
+ promptTemplates = gr.State(load_template(get_template_names(plain=True)[0], mode=2))
54
+ TRUECOMSTANT = gr.State(True)
55
+ FALSECONSTANT = gr.State(False)
56
+ topic = gr.State("未命名对话历史记录")
57
+
58
+ with gr.Row():
59
+ with gr.Column(scale=12):
60
+ user_input = gr.Textbox(show_label=False, placeholder="在这里输入").style(
61
+ container=False)
62
+ with gr.Column(min_width=50, scale=1):
63
+ submitBtn = gr.Button("🚀", variant="primary")
64
+ with gr.Row():
65
+ emptyBtn = gr.Button("🧹 新的对话")
66
+ retryBtn = gr.Button("🔄 重新生成")
67
+ delLastBtn = gr.Button("🗑️ 删除最近一条对话")
68
+ reduceTokenBtn = gr.Button("♻️ 总结对话")
69
+ status_display = gr.Markdown("status: ready")
70
+ systemPromptTxt = gr.Textbox(show_label=True, placeholder=f"在这里输入System Prompt...",
71
+ label="System prompt", value=initial_prompt).style(container=True)
72
+ with gr.Accordion(label="加载Prompt模板", open=False):
73
+ with gr.Column():
74
+ with gr.Row():
75
+ with gr.Column(scale=6):
76
+ templateFileSelectDropdown = gr.Dropdown(label="选择Prompt模板集合文件", choices=get_template_names(plain=True), multiselect=False, value=get_template_names(plain=True)[0])
77
+ with gr.Column(scale=1):
78
+ templateRefreshBtn = gr.Button("🔄 刷新")
79
+ templaeFileReadBtn = gr.Button("📂 读入模板")
80
+ with gr.Row():
81
+ with gr.Column(scale=6):
82
+ templateSelectDropdown = gr.Dropdown(label="从Prompt模板中加载", choices=load_template(get_template_names(plain=True)[0], mode=1), multiselect=False, value=load_template(get_template_names(plain=True)[0], mode=1)[0])
83
+ with gr.Column(scale=1):
84
+ templateApplyBtn = gr.Button("⬇️ 应用")
85
+ with gr.Accordion(label="保存/加载对话历史记录", open=False):
86
+ with gr.Column():
87
+ with gr.Row():
88
+ with gr.Column(scale=6):
89
+ saveFileName = gr.Textbox(
90
+ show_label=True, placeholder=f"在这里输入保存的文件名...", label="设置保存文件名", value="对话历史记录").style(container=True)
91
+ with gr.Column(scale=1):
92
+ saveHistoryBtn = gr.Button("💾 保存对话")
93
+ with gr.Row():
94
+ with gr.Column(scale=6):
95
+ historyFileSelectDropdown = gr.Dropdown(label="从列表中加载对话", choices=get_history_names(plain=True), multiselect=False, value=get_history_names(plain=True)[0])
96
+ with gr.Column(scale=1):
97
+ historyRefreshBtn = gr.Button("🔄 刷新")
98
+ historyReadBtn = gr.Button("📂 读入对话")
99
+ #inputs, top_p, temperature, top_k, repetition_penalty
100
+ with gr.Accordion("参数", open=False):
101
+ top_p = gr.Slider(minimum=-0, maximum=1.0, value=1.0, step=0.05,
102
+ interactive=True, label="Top-p (nucleus sampling)",)
103
+ temperature = gr.Slider(minimum=-0, maximum=5.0, value=1.0,
104
+ step=0.1, interactive=True, label="Temperature",)
105
+ #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
106
+ #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
107
+ gr.Markdown(description)
108
+
109
+
110
+ user_input.submit(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
111
+ user_input.submit(reset_textbox, [], [user_input])
112
+
113
+ submitBtn.click(predict, [keyTxt, systemPromptTxt, history, user_input, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
114
+ submitBtn.click(reset_textbox, [], [user_input])
115
+
116
+ emptyBtn.click(reset_state, outputs=[chatbot, history, token_count, status_display], show_progress=True)
117
+
118
+ retryBtn.click(retry, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
119
+
120
+ delLastBtn.click(delete_last_conversation, [chatbot, history, token_count, use_streaming_checkbox], [
121
+ chatbot, history, token_count, status_display], show_progress=True)
122
+
123
+ reduceTokenBtn.click(reduce_token_size, [keyTxt, systemPromptTxt, history, chatbot, token_count, top_p, temperature, use_streaming_checkbox], [chatbot, history, status_display, token_count], show_progress=True)
124
+
125
+ saveHistoryBtn.click(save_chat_history, [
126
+ saveFileName, systemPromptTxt, history, chatbot], None, show_progress=True)
127
+
128
+ saveHistoryBtn.click(get_history_names, None, [historyFileSelectDropdown])
129
+
130
+ historyRefreshBtn.click(get_history_names, None, [historyFileSelectDropdown])
131
+
132
+ historyReadBtn.click(load_chat_history, [historyFileSelectDropdown, systemPromptTxt, history, chatbot], [saveFileName, systemPromptTxt, history, chatbot], show_progress=True)
133
+
134
+ templateRefreshBtn.click(get_template_names, None, [templateFileSelectDropdown])
135
+
136
+ templaeFileReadBtn.click(load_template, [templateFileSelectDropdown], [promptTemplates, templateSelectDropdown], show_progress=True)
137
+
138
+ templateApplyBtn.click(get_template_content, [promptTemplates, templateSelectDropdown, systemPromptTxt], [systemPromptTxt], show_progress=True)
139
+
140
+ print("川虎的温馨提示:访问 http://localhost:7860 查看界面")
141
+ # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
142
+ demo.title = "川虎ChatGPT 🚀"
143
+
144
+ if __name__ == "__main__":
145
+ #if running in Docker
146
+ if dockerflag:
147
+ if authflag:
148
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=(username, password))
149
+ else:
150
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False)
151
+ #if not running in Docker
152
+ else:
153
+ if authflag:
154
+ demo.queue().launch(share=False, auth=(username, password))
155
+ else:
156
+ demo.queue().launch(share=False) # 改为 share=True 可以创建公开分享链接
157
+ #demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=False) # 可自定义端口
158
+ #demo.queue().launch(server_name="0.0.0.0", server_port=7860,auth=("在这里填写用户名", "在这里填写密码")) # 可设置用户名与密码
159
+ #demo.queue().launch(auth=("在这里填写用户名", "在这里填写密码")) # 适合Nginx反向代理
presets.py CHANGED
@@ -29,3 +29,13 @@ pre code {
29
  box-shadow: inset 0px 8px 16px hsla(0, 0%, 0%, .2)
30
  }
31
  """
 
 
 
 
 
 
 
 
 
 
 
29
  box-shadow: inset 0px 8px 16px hsla(0, 0%, 0%, .2)
30
  }
31
  """
32
+
33
+ standard_error_msg = "☹️发生了错误:" # 错误信息的标准前缀
34
+ error_retrieve_prompt = "连接超时,无法获取对话。请检查网络连接,或者API-Key是否有效。" # 获取对话时发生错误
35
+ summarize_prompt = "请总结以上对话,不超过100字。" # 总结对话时的 prompt
36
+ max_token_streaming = 3000 # 流式对话时的最大 token 数
37
+ timeout_streaming = 5 # 流式对话时的超时时间
38
+ max_token_all = 3500 # 非流式对话时的最大 token 数
39
+ timeout_all = 200 # 非流式对话时的超时时间
40
+ enable_streaming_option = False # 是否启用选择选择是否实时显示回答的勾选框
41
+ HIDE_MY_KEY = False # 如果你想在UI中隐藏你的 API 密钥,将此值设置为 True
utils.py CHANGED
@@ -14,6 +14,7 @@ import requests
14
  import csv
15
  import mdtex2html
16
  from pypinyin import lazy_pinyin
 
17
 
18
  if TYPE_CHECKING:
19
  from typing import TypedDict
@@ -51,7 +52,6 @@ def parse_text(text):
51
  lines = text.split("\n")
52
  lines = [line for line in lines if line != ""]
53
  count = 0
54
- firstline = False
55
  for i, line in enumerate(lines):
56
  if "```" in line:
57
  count += 1
@@ -79,61 +79,33 @@ def parse_text(text):
79
  text = "".join(lines)
80
  return text
81
 
82
- def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[], system_prompt=initial_prompt, retry=False, summary=False, retry_on_crash = False, stream = True): # repetition_penalty, top_k
 
83
 
84
- if "猫娘" in inputs:
85
- chatbot.append((inputs, '喵~主人请点击<a href="https://www.bilibili.com/video/BV1GJ411x7h7/">这个链接</a>查看刺激内容哦~'))
86
- yield chatbot, history, "status: 喵~"
87
- print(f"########## 有人上钩了: {inputs} ##########")
88
- return
89
- print(f"====== 收到问题: {inputs} =======")
90
-
91
- if retry_on_crash:
92
- retry = True
93
 
 
 
 
 
 
94
  headers = {
95
  "Content-Type": "application/json",
96
  "Authorization": f"Bearer {openai_api_key}"
97
  }
98
 
99
- chat_counter = len(history) // 2
100
-
101
- print(f"chat_counter - {chat_counter}")
102
-
103
- messages = []
104
- if chat_counter:
105
- for index in range(0, 2*chat_counter, 2):
106
- temp1 = {}
107
- temp1["role"] = "user"
108
- temp1["content"] = history[index]
109
- temp2 = {}
110
- temp2["role"] = "assistant"
111
- temp2["content"] = history[index+1]
112
- if temp1["content"] != "":
113
- if temp2["content"] != "" or retry:
114
- messages.append(temp1)
115
- messages.append(temp2)
116
- else:
117
- messages[-1]['content'] = temp2['content']
118
- if retry and chat_counter:
119
- if retry_on_crash:
120
- messages = messages[-6:]
121
- messages.pop()
122
- elif summary:
123
- history = [*[i["content"] for i in messages[-2:]], "我们刚刚聊了什么?"]
124
- messages.append(compose_user(
125
- "请帮我总结一下上述对话的内容,实现减少字数的同时,保证对话的质量。在总结中不要加入这一句话。"))
126
- else:
127
- temp3 = {}
128
- temp3["role"] = "user"
129
- temp3["content"] = inputs
130
- messages.append(temp3)
131
- chat_counter += 1
132
- messages = [compose_system(system_prompt), *messages]
133
- # messages
134
  payload = {
135
  "model": "gpt-3.5-turbo",
136
- "messages": messages, # [{"role": "user", "content": f"{inputs}"}],
137
  "temperature": temperature, # 1.0,
138
  "top_p": top_p, # 1.0,
139
  "n": 1,
@@ -141,94 +113,129 @@ def predict(inputs, top_p, temperature, openai_api_key, chatbot=[], history=[],
141
  "presence_penalty": 0,
142
  "frequency_penalty": 0,
143
  }
144
-
145
- if not summary:
146
- history.append(inputs)
147
  else:
148
- print("精简中...")
 
 
149
 
150
- print(f"payload: {payload}")
151
- # make a POST request to the API endpoint using the requests.post method, passing in stream=True
 
 
 
 
 
 
152
  try:
153
- response = requests.post(API_URL, headers=headers, json=payload, stream=True)
154
- except:
155
- history.append("")
156
- chatbot.append((inputs, ""))
157
- yield history, chatbot, f"获取请求失败,请检查网络连接。"
158
  return
159
 
160
- token_counter = 0
161
- partial_words = ""
162
 
163
- counter = 0
164
- if stream:
165
- chatbot.append((parse_text(history[-1]), ""))
166
- for chunk in response.iter_lines():
167
- if counter == 0:
168
- counter += 1
169
- continue
170
  counter += 1
171
- # check whether each line is non-empty
172
- if chunk:
173
- # decode each line as response data is in bytes
174
- try:
175
- if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
176
- chunkjson = json.loads(chunk.decode()[6:])
177
- status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
178
- yield chatbot, history, status_text
179
- break
180
- except Exception as e:
181
- if not retry_on_crash:
182
- print("正在尝试使用缩短的context重新生成……")
183
- chatbot.pop()
184
- history.append("")
185
- yield next(predict(inputs, top_p, temperature, openai_api_key, chatbot, history, system_prompt, retry, summary=False, retry_on_crash=True, stream=False))
186
- else:
187
- msg = "☹️发生了错误:生成失败,请检查网络"
188
- print(msg)
189
- history.append(inputs, "")
190
- chatbot.append(inputs, msg)
191
- yield chatbot, history, "status: ERROR"
192
  break
193
- chunkjson = json.loads(chunk.decode()[6:])
194
- status_text = f"id: {chunkjson['id']}, finish_reason: {chunkjson['choices'][0]['finish_reason']}"
195
- partial_words = partial_words + \
196
- json.loads(chunk.decode()[6:])[
197
- 'choices'][0]["delta"]["content"]
198
  if token_counter == 0:
199
- history.append(" " + partial_words)
200
  else:
201
- history[-1] = partial_words
202
- chatbot[-1] = (parse_text(history[-2]), parse_text(history[-1]))
203
  token_counter += 1
204
- yield chatbot, history, status_text
205
- else:
206
- try:
207
- responsejson = json.loads(response.text)
208
- content = responsejson["choices"][0]["message"]["content"]
209
- history.append(content)
210
- chatbot.append((parse_text(history[-2]), parse_text(content)))
211
- status_text = "精简完成"
212
- except:
213
- chatbot.append((parse_text(history[-1]), "☹️发生了错误,请检查网络连接或者稍后再试。"))
214
- status_text = "status: ERROR"
215
- yield chatbot, history, status_text
216
 
217
 
218
-
219
- def delete_last_conversation(chatbot, history):
220
  try:
221
- if "☹️发生了错误" in chatbot[-1][1]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  chatbot.pop()
223
- print(history)
224
- return chatbot, history
 
 
 
 
 
 
225
  history.pop()
226
  history.pop()
 
227
  chatbot.pop()
228
- print(history)
229
- return chatbot, history
230
- except:
231
- return chatbot, history
232
 
233
  def save_chat_history(filename, system, history, chatbot):
234
  if filename == "":
@@ -244,10 +251,16 @@ def save_chat_history(filename, system, history, chatbot):
244
 
245
  def load_chat_history(filename, system, history, chatbot):
246
  try:
247
- print("Loading from history...")
248
  with open(os.path.join(HISTORY_DIR, filename), "r") as f:
249
  json_s = json.load(f)
250
- print(json_s)
 
 
 
 
 
 
 
251
  return filename, json_s["system"], json_s["history"], json_s["chatbot"]
252
  except FileNotFoundError:
253
  print("File not found.")
@@ -305,7 +318,7 @@ def get_template_content(templates, selection, original_system_prompt):
305
  return original_system_prompt
306
 
307
  def reset_state():
308
- return [], []
309
 
310
  def compose_system(system_prompt):
311
  return {"role": "system", "content": system_prompt}
 
14
  import csv
15
  import mdtex2html
16
  from pypinyin import lazy_pinyin
17
+ from presets import *
18
 
19
  if TYPE_CHECKING:
20
  from typing import TypedDict
 
52
  lines = text.split("\n")
53
  lines = [line for line in lines if line != ""]
54
  count = 0
 
55
  for i, line in enumerate(lines):
56
  if "```" in line:
57
  count += 1
 
79
  text = "".join(lines)
80
  return text
81
 
82
+ def construct_text(role, text):
83
+ return {"role": role, "content": text}
84
 
85
+ def construct_user(text):
86
+ return construct_text("user", text)
87
+
88
+ def construct_system(text):
89
+ return construct_text("system", text)
90
+
91
+ def construct_assistant(text):
92
+ return construct_text("assistant", text)
 
93
 
94
+ def construct_token_message(token, stream=False):
95
+ extra = "【仅包含回答的计数】 " if stream else ""
96
+ return f"{extra}Token 计数: {token}"
97
+
98
+ def get_response(openai_api_key, system_prompt, history, temperature, top_p, stream):
99
  headers = {
100
  "Content-Type": "application/json",
101
  "Authorization": f"Bearer {openai_api_key}"
102
  }
103
 
104
+ history = [construct_system(system_prompt), *history]
105
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  payload = {
107
  "model": "gpt-3.5-turbo",
108
+ "messages": history, # [{"role": "user", "content": f"{inputs}"}],
109
  "temperature": temperature, # 1.0,
110
  "top_p": top_p, # 1.0,
111
  "n": 1,
 
113
  "presence_penalty": 0,
114
  "frequency_penalty": 0,
115
  }
116
+ if stream:
117
+ timeout = timeout_streaming
 
118
  else:
119
+ timeout = timeout_all
120
+ response = requests.post(API_URL, headers=headers, json=payload, stream=True, timeout=timeout)
121
+ return response
122
 
123
+ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, previous_token_count, top_p, temperature):
124
+ def get_return_value():
125
+ return chatbot, history, status_text, [*previous_token_count, token_counter]
126
+ token_counter = 0
127
+ partial_words = ""
128
+ counter = 0
129
+ status_text = "OK"
130
+ history.append(construct_user(inputs))
131
  try:
132
+ response = get_response(openai_api_key, system_prompt, history, temperature, top_p, True)
133
+ except requests.exceptions.ConnectTimeout:
134
+ status_text = standard_error_msg + error_retrieve_prompt
135
+ yield get_return_value()
 
136
  return
137
 
138
+ chatbot.append((parse_text(inputs), ""))
139
+ yield get_return_value()
140
 
141
+ for chunk in response.iter_lines():
142
+ if counter == 0:
 
 
 
 
 
143
  counter += 1
144
+ continue
145
+ counter += 1
146
+ # check whether each line is non-empty
147
+ if chunk:
148
+ chunk = chunk.decode()
149
+ chunklength = len(chunk)
150
+ chunk = json.loads(chunk[6:])
151
+ # decode each line as response data is in bytes
152
+ if chunklength > 6 and "delta" in chunk['choices'][0]:
153
+ finish_reason = chunk['choices'][0]['finish_reason']
154
+ status_text = construct_token_message(sum(previous_token_count)+token_counter, stream=True)
155
+ if finish_reason == "stop":
156
+ yield get_return_value()
 
 
 
 
 
 
 
 
157
  break
158
+ partial_words = partial_words + chunk['choices'][0]["delta"]["content"]
 
 
 
 
159
  if token_counter == 0:
160
+ history.append(construct_assistant(" " + partial_words))
161
  else:
162
+ history[-1] = construct_assistant(partial_words)
163
+ chatbot[-1] = (parse_text(inputs), parse_text(partial_words))
164
  token_counter += 1
165
+ yield get_return_value()
 
 
 
 
 
 
 
 
 
 
 
166
 
167
 
168
+ def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, previous_token_count, top_p, temperature):
169
+ history.append(construct_user(inputs))
170
  try:
171
+ response = get_response(openai_api_key, system_prompt, history, temperature, top_p, False)
172
+ except requests.exceptions.ConnectTimeout:
173
+ status_text = standard_error_msg + error_retrieve_prompt
174
+ return chatbot, history, status_text, previous_token_count
175
+ response = json.loads(response.text)
176
+ content = response["choices"][0]["message"]["content"]
177
+ history.append(construct_assistant(content))
178
+ chatbot.append((parse_text(inputs), parse_text(content)))
179
+ total_token_count = response["usage"]["total_tokens"]
180
+ previous_token_count.append(total_token_count - sum(previous_token_count))
181
+ status_text = construct_token_message(total_token_count)
182
+ return chatbot, history, status_text, previous_token_count
183
+
184
+
185
+ def predict(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature, stream=False, should_check_token_count = True): # repetition_penalty, top_k
186
+ if stream:
187
+ iter = stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature)
188
+ for chatbot, history, status_text, token_count in iter:
189
+ yield chatbot, history, status_text, token_count
190
+ else:
191
+ chatbot, history, status_text, token_count = predict_all(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature)
192
+ yield chatbot, history, status_text, token_count
193
+ if stream:
194
+ max_token = max_token_streaming
195
+ else:
196
+ max_token = max_token_all
197
+ if sum(token_count) > max_token and should_check_token_count:
198
+ iter = reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, hidden=True)
199
+ for chatbot, history, status_text, token_count in iter:
200
+ status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
201
+ yield chatbot, history, status_text, token_count
202
+
203
+
204
+ def retry(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False):
205
+ if len(history) == 0:
206
+ yield chatbot, history, f"{standard_error_msg}上下文是空的", token_count
207
+ return
208
+ history.pop()
209
+ inputs = history.pop()["content"]
210
+ token_count.pop()
211
+ iter = predict(openai_api_key, system_prompt, history, inputs, chatbot, token_count, top_p, temperature, stream=stream)
212
+ for x in iter:
213
+ yield x
214
+
215
+
216
+ def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, hidden=False):
217
+ iter = predict(openai_api_key, system_prompt, history, summarize_prompt, chatbot, token_count, top_p, temperature, stream=stream, should_check_token_count=False)
218
+ for chatbot, history, status_text, previous_token_count in iter:
219
+ history = history[-2:]
220
+ token_count = previous_token_count[-1:]
221
+ if hidden:
222
  chatbot.pop()
223
+ yield chatbot, history, construct_token_message(sum(token_count), stream=stream), token_count
224
+
225
+
226
+ def delete_last_conversation(chatbot, history, previous_token_count, streaming):
227
+ if len(chatbot) > 0 and standard_error_msg in chatbot[-1][1]:
228
+ chatbot.pop()
229
+ return chatbot, history
230
+ if len(history) > 0:
231
  history.pop()
232
  history.pop()
233
+ if len(chatbot) > 0:
234
  chatbot.pop()
235
+ if len(previous_token_count) > 0:
236
+ previous_token_count.pop()
237
+ return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count), streaming)
238
+
239
 
240
  def save_chat_history(filename, system, history, chatbot):
241
  if filename == "":
 
251
 
252
  def load_chat_history(filename, system, history, chatbot):
253
  try:
 
254
  with open(os.path.join(HISTORY_DIR, filename), "r") as f:
255
  json_s = json.load(f)
256
+ if type(json_s["history"]) == list:
257
+ new_history = []
258
+ for index, item in enumerate(json_s["history"]):
259
+ if index % 2 == 0:
260
+ new_history.append(construct_user(item))
261
+ else:
262
+ new_history.append(construct_assistant(item))
263
+ json_s["history"] = new_history
264
  return filename, json_s["system"], json_s["history"], json_s["chatbot"]
265
  except FileNotFoundError:
266
  print("File not found.")
 
318
  return original_system_prompt
319
 
320
  def reset_state():
321
+ return [], [], [], construct_token_message(0)
322
 
323
  def compose_system(system_prompt):
324
  return {"role": "system", "content": system_prompt}