Tuchuanhuhuhu commited on
Commit
7b24389
·
1 Parent(s): a093438

修复减少token的功能

Browse files
Files changed (2) hide show
  1. presets.py +1 -1
  2. utils.py +9 -5
presets.py CHANGED
@@ -43,7 +43,7 @@ proxy_error_prompt = "代理错误,无法获取对话。" # 代理错误
43
  ssl_error_prompt = "SSL错误,无法获取对话。" # SSL 错误
44
  no_apikey_msg = "API key长度不是51位,请检查是否输入正确。" # API key 长度不足 51 位
45
 
46
- max_token_streaming = 3500 # 流式对话时的最大 token 数
47
  timeout_streaming = 15 # 流式对话时的超时时间
48
  max_token_all = 3500 # 非流式对话时的最大 token 数
49
  timeout_all = 200 # 非流式对话时的超时时间
 
43
  ssl_error_prompt = "SSL错误,无法获取对话。" # SSL 错误
44
  no_apikey_msg = "API key长度不是51位,请检查是否输入正确。" # API key 长度不足 51 位
45
 
46
+ max_token_streaming = 50 # 流式对话时的最大 token 数
47
  timeout_streaming = 15 # 流式对话时的超时时间
48
  max_token_all = 3500 # 非流式对话时的最大 token 数
49
  timeout_all = 200 # 非流式对话时的超时时间
utils.py CHANGED
@@ -217,7 +217,7 @@ def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_tok
217
  response = json.loads(response.text)
218
  content = response["choices"][0]["message"]["content"]
219
  history[-1] = construct_assistant(content)
220
- chatbot.append((parse_text(inputs), parse_text(content)))
221
  total_token_count = response["usage"]["total_tokens"]
222
  all_token_counts[-1] = total_token_count - sum(all_token_counts)
223
  status_text = construct_token_message(total_token_count)
@@ -238,7 +238,8 @@ def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_c
238
  history[-2] = construct_user(inputs)
239
  yield chatbot, history, status_text, all_token_counts
240
  return
241
- yield chatbot, history, "开始生成回答……", all_token_counts
 
242
  if stream:
243
  logging.info("使用流式传输")
244
  iter = stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
@@ -256,8 +257,10 @@ def predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_c
256
  else:
257
  max_token = max_token_all
258
  if sum(all_token_counts) > max_token and should_check_token_count:
259
- logging.info(f"精简token中{all_token_counts}/{max_token}")
260
- iter = reduce_token_size(openai_api_key, system_prompt, history, chatbot, all_token_counts, top_p, temperature, stream=False, hidden=True)
 
 
261
  for chatbot, history, status_text, all_token_counts in iter:
262
  status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
263
  yield chatbot, history, status_text, all_token_counts
@@ -277,9 +280,10 @@ def retry(openai_api_key, system_prompt, history, chatbot, token_count, top_p, t
277
  yield x
278
 
279
 
280
- def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, hidden=False, selected_model = MODELS[0]):
281
  logging.info("开始减少token数量……")
282
  iter = predict(openai_api_key, system_prompt, history, summarize_prompt, chatbot, token_count, top_p, temperature, stream=stream, selected_model = selected_model, should_check_token_count=False)
 
283
  for chatbot, history, status_text, previous_token_count in iter:
284
  history = history[-2:]
285
  token_count = previous_token_count[-1:]
 
217
  response = json.loads(response.text)
218
  content = response["choices"][0]["message"]["content"]
219
  history[-1] = construct_assistant(content)
220
+ chatbot[-1] = (parse_text(inputs), parse_text(content))
221
  total_token_count = response["usage"]["total_tokens"]
222
  all_token_counts[-1] = total_token_count - sum(all_token_counts)
223
  status_text = construct_token_message(total_token_count)
 
238
  history[-2] = construct_user(inputs)
239
  yield chatbot, history, status_text, all_token_counts
240
  return
241
+ if stream:
242
+ yield chatbot, history, "开始生成回答……", all_token_counts
243
  if stream:
244
  logging.info("使用流式传输")
245
  iter = stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_token_counts, top_p, temperature, selected_model)
 
257
  else:
258
  max_token = max_token_all
259
  if sum(all_token_counts) > max_token and should_check_token_count:
260
+ status_text = f"精简token中{all_token_counts}/{max_token}"
261
+ logging.info(status_text)
262
+ yield chatbot, history, status_text, all_token_counts
263
+ iter = reduce_token_size(openai_api_key, system_prompt, history, chatbot, all_token_counts, top_p, temperature, stream=False, selected_model=selected_model, hidden=True)
264
  for chatbot, history, status_text, all_token_counts in iter:
265
  status_text = f"Token 达到上限,已自动降低Token计数至 {status_text}"
266
  yield chatbot, history, status_text, all_token_counts
 
280
  yield x
281
 
282
 
283
+ def reduce_token_size(openai_api_key, system_prompt, history, chatbot, token_count, top_p, temperature, stream=False, selected_model = MODELS[0], hidden=False):
284
  logging.info("开始减少token数量……")
285
  iter = predict(openai_api_key, system_prompt, history, summarize_prompt, chatbot, token_count, top_p, temperature, stream=stream, selected_model = selected_model, should_check_token_count=False)
286
+ logging.info(f"chatbot: {chatbot}")
287
  for chatbot, history, status_text, previous_token_count in iter:
288
  history = history[-2:]
289
  token_count = previous_token_count[-1:]