Spaces:
Sleeping
Sleeping
fixed token count (#199)
Browse files* fixed token count
* fix: amend count_token in utils
Co-authored-by: KintaMiao <caoxuanming1207@163.com>
* Update utils.py
---------
Co-authored-by: Keldos <i@keldos.me>
Co-authored-by: KintaMiao <caoxuanming1207@163.com>
Co-authored-by: Chuan Hu <51039745+GaiZhenbiao@users.noreply.github.com>
utils.py
CHANGED
@@ -53,8 +53,9 @@ def postprocess(
|
|
53 |
)
|
54 |
return y
|
55 |
|
56 |
-
def count_token(
|
57 |
encoding = tiktoken.get_encoding("cl100k_base")
|
|
|
58 |
length = len(encoding.encode(input_str))
|
59 |
return length
|
60 |
|
@@ -142,10 +143,10 @@ def stream_predict(openai_api_key, system_prompt, history, inputs, chatbot, all_
|
|
142 |
chatbot.append((parse_text(inputs), ""))
|
143 |
user_token_count = 0
|
144 |
if len(all_token_counts) == 0:
|
145 |
-
system_prompt_token_count = count_token(system_prompt)
|
146 |
-
user_token_count = count_token(inputs) + system_prompt_token_count
|
147 |
else:
|
148 |
-
user_token_count = count_token(inputs)
|
149 |
all_token_counts.append(user_token_count)
|
150 |
logging.info(f"输入token计数: {user_token_count}")
|
151 |
yield get_return_value()
|
@@ -204,7 +205,7 @@ def predict_all(openai_api_key, system_prompt, history, inputs, chatbot, all_tok
|
|
204 |
history.append(construct_user(inputs))
|
205 |
history.append(construct_assistant(""))
|
206 |
chatbot.append((parse_text(inputs), ""))
|
207 |
-
all_token_counts.append(count_token(inputs))
|
208 |
try:
|
209 |
response = get_response(openai_api_key, system_prompt, history, temperature, top_p, False, selected_model)
|
210 |
except requests.exceptions.ConnectTimeout:
|
|
|
53 |
)
|
54 |
return y
|
55 |
|
56 |
+
def count_token(message):
|
57 |
encoding = tiktoken.get_encoding("cl100k_base")
|
58 |
+
input_str = f"role: {message['role']}, content: {message['content']}"
|
59 |
length = len(encoding.encode(input_str))
|
60 |
return length
|
61 |
|
|
|
143 |
chatbot.append((parse_text(inputs), ""))
|
144 |
user_token_count = 0
|
145 |
if len(all_token_counts) == 0:
|
146 |
+
system_prompt_token_count = count_token(construct_system(system_prompt))
|
147 |
+
user_token_count = count_token(construct_user(inputs)) + system_prompt_token_count
|
148 |
else:
|
149 |
+
user_token_count = count_token(construct_user(inputs))
|
150 |
all_token_counts.append(user_token_count)
|
151 |
logging.info(f"输入token计数: {user_token_count}")
|
152 |
yield get_return_value()
|
|
|
205 |
history.append(construct_user(inputs))
|
206 |
history.append(construct_assistant(""))
|
207 |
chatbot.append((parse_text(inputs), ""))
|
208 |
+
all_token_counts.append(count_token(construct_user(inputs)))
|
209 |
try:
|
210 |
response = get_response(openai_api_key, system_prompt, history, temperature, top_p, False, selected_model)
|
211 |
except requests.exceptions.ConnectTimeout:
|