Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
app.py
CHANGED
@@ -1123,44 +1123,23 @@ def index():
|
|
1123 |
|
1124 |
|
1125 |
def num_tokens_from_string(string, model=""):
|
1126 |
-
"""计算字符串的token数量"""
|
1127 |
try:
|
1128 |
-
# 准备请求数据
|
1129 |
-
request_data = {
|
1130 |
-
"model": model,
|
1131 |
-
"messages": [{"role": "user", "content": string}]
|
1132 |
-
}
|
1133 |
-
|
1134 |
-
# 发送POST请求到token计算服务
|
1135 |
response = requests.post(
|
1136 |
TOKENIZER_SERVICE_URL,
|
1137 |
-
json=
|
1138 |
timeout=10
|
1139 |
)
|
1140 |
|
1141 |
-
# 解析响应
|
1142 |
if response.status_code == 200:
|
1143 |
result = response.json()
|
1144 |
-
# 检查响应中是否包含warning字段,有则表示是估算值
|
1145 |
-
calculation_method = "estimate" if "warning" in result else "api"
|
1146 |
input_tokens = result.get("input_tokens", 0)
|
1147 |
-
return input_tokens,
|
1148 |
-
elif response.status_code == 400:
|
1149 |
-
# 服务返回400错误,但可能提供了估算值
|
1150 |
-
result = response.json()
|
1151 |
-
if "input_tokens" in result:
|
1152 |
-
print(f"使用估算token值: {result.get('input_tokens')}")
|
1153 |
-
return result.get("input_tokens", 0), "估算"
|
1154 |
-
# 如果没有提供估算值,使用字符数/4作为预估
|
1155 |
-
return len(string) // 4, "估算"
|
1156 |
else:
|
1157 |
-
# 如果服务返回其他错误,记录错误并返回字符串长度/4作为预估值
|
1158 |
print(f"Tokenizer服务错误: {response.status_code} - {response.text}")
|
1159 |
-
return len(string) // 4, "估算"
|
1160 |
except Exception as e:
|
1161 |
-
# 如果发生其他错误,记录错误并返回字符串长度/4作为预估值
|
1162 |
print(f"计算token错误: {e}")
|
1163 |
-
return len(string) // 4, "估算"
|
1164 |
|
1165 |
|
1166 |
# 更新模型使用统计
|
@@ -1179,7 +1158,7 @@ def update_model_stats(model, prompt_tokens, completion_tokens, calculation_meth
|
|
1179 |
"call_time": call_time,
|
1180 |
"prompt_tokens": prompt_tokens,
|
1181 |
"completion_tokens": completion_tokens,
|
1182 |
-
"calculation_method":
|
1183 |
"compute_points": compute_points
|
1184 |
}
|
1185 |
model_usage_records.append(record)
|
|
|
1123 |
|
1124 |
|
1125 |
def num_tokens_from_string(string, model=""):
|
|
|
1126 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1127 |
response = requests.post(
|
1128 |
TOKENIZER_SERVICE_URL,
|
1129 |
+
json={"model": model, "messages": [{"role": "user", "content": string}]},
|
1130 |
timeout=10
|
1131 |
)
|
1132 |
|
|
|
1133 |
if response.status_code == 200:
|
1134 |
result = response.json()
|
|
|
|
|
1135 |
input_tokens = result.get("input_tokens", 0)
|
1136 |
+
return input_tokens, "精确" # 直接返回精确
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1137 |
else:
|
|
|
1138 |
print(f"Tokenizer服务错误: {response.status_code} - {response.text}")
|
1139 |
+
return len(string) // 4, "估算" # 直接返回估算
|
1140 |
except Exception as e:
|
|
|
1141 |
print(f"计算token错误: {e}")
|
1142 |
+
return len(string) // 4, "估算" # 直接返回估算
|
1143 |
|
1144 |
|
1145 |
# 更新模型使用统计
|
|
|
1158 |
"call_time": call_time,
|
1159 |
"prompt_tokens": prompt_tokens,
|
1160 |
"completion_tokens": completion_tokens,
|
1161 |
+
"calculation_method": calculation_method, # 直接使用传入的值
|
1162 |
"compute_points": compute_points
|
1163 |
}
|
1164 |
model_usage_records.append(record)
|