Tuchuanhuhuhu commited on
Commit
d794500
·
1 Parent(s): b91e1d8

增加了现实累计token消耗的功能(用于评估开销)

Browse files
Files changed (2) hide show
  1. modules/chat_func.py +2 -4
  2. modules/utils.py +8 -5
modules/chat_func.py CHANGED
@@ -165,9 +165,7 @@ def stream_predict(
165
  # decode each line as response data is in bytes
166
  if chunklength > 6 and "delta" in chunk["choices"][0]:
167
  finish_reason = chunk["choices"][0]["finish_reason"]
168
- status_text = construct_token_message(
169
- sum(all_token_counts), stream=True
170
- )
171
  if finish_reason == "stop":
172
  yield get_return_value()
173
  break
@@ -493,7 +491,7 @@ def reduce_token_size(
493
  token_count = previous_token_count[-num_chat:] if num_chat > 0 else []
494
  msg = f"保留了最近{num_chat}轮对话"
495
  yield chatbot, history, msg + "," + construct_token_message(
496
- sum(token_count) if len(token_count) > 0 else 0,
497
  ), token_count
498
  logging.info(msg)
499
  logging.info("减少token数量完毕")
 
165
  # decode each line as response data is in bytes
166
  if chunklength > 6 and "delta" in chunk["choices"][0]:
167
  finish_reason = chunk["choices"][0]["finish_reason"]
168
+ status_text = construct_token_message(all_token_counts)
 
 
169
  if finish_reason == "stop":
170
  yield get_return_value()
171
  break
 
491
  token_count = previous_token_count[-num_chat:] if num_chat > 0 else []
492
  msg = f"保留了最近{num_chat}轮对话"
493
  yield chatbot, history, msg + "," + construct_token_message(
494
+ token_count if len(token_count) > 0 else [0],
495
  ), token_count
496
  logging.info(msg)
497
  logging.info("减少token数量完毕")
modules/utils.py CHANGED
@@ -153,8 +153,11 @@ def construct_assistant(text):
153
  return construct_text("assistant", text)
154
 
155
 
156
- def construct_token_message(token, stream=False):
157
- return f"Token 计数: {token}"
 
 
 
158
 
159
 
160
  def delete_first_conversation(history, previous_token_count):
@@ -164,7 +167,7 @@ def delete_first_conversation(history, previous_token_count):
164
  return (
165
  history,
166
  previous_token_count,
167
- construct_token_message(sum(previous_token_count)),
168
  )
169
 
170
 
@@ -187,7 +190,7 @@ def delete_last_conversation(chatbot, history, previous_token_count):
187
  chatbot,
188
  history,
189
  previous_token_count,
190
- construct_token_message(sum(previous_token_count)),
191
  )
192
 
193
 
@@ -321,7 +324,7 @@ def get_template_content(templates, selection, original_system_prompt):
321
 
322
  def reset_state():
323
  logging.info("重置状态")
324
- return [], [], [], construct_token_message(0)
325
 
326
 
327
  def reset_textbox():
 
153
  return construct_text("assistant", text)
154
 
155
 
156
+ def construct_token_message(tokens: List[int]):
157
+ token_sum = 0
158
+ for i in range(len(tokens)):
159
+ token_sum += sum(tokens[: i + 1])
160
+ return f"Token 计数: {sum(tokens)},本次对话累计消耗了 {token_sum} tokens"
161
 
162
 
163
  def delete_first_conversation(history, previous_token_count):
 
167
  return (
168
  history,
169
  previous_token_count,
170
+ construct_token_message(previous_token_count),
171
  )
172
 
173
 
 
190
  chatbot,
191
  history,
192
  previous_token_count,
193
+ construct_token_message(previous_token_count),
194
  )
195
 
196
 
 
324
 
325
  def reset_state():
326
  logging.info("重置状态")
327
+ return [], [], [], construct_token_message([0])
328
 
329
 
330
  def reset_textbox():