mirror of
https://github.com/Yanyutin753/RefreshToV1Api.git
synced 2025-12-11 02:01:26 +08:00
优化非流式回复空的问题
This commit is contained in:
56
main.py
56
main.py
@@ -2368,32 +2368,36 @@ def chat_completions():
|
|||||||
ori_model_name = model_config.get('ori_name', model)
|
ori_model_name = model_config.get('ori_name', model)
|
||||||
input_tokens = count_total_input_words(messages, ori_model_name)
|
input_tokens = count_total_input_words(messages, ori_model_name)
|
||||||
comp_tokens = count_tokens(all_new_text, ori_model_name)
|
comp_tokens = count_tokens(all_new_text, ori_model_name)
|
||||||
response_json = {
|
if input_tokens >= 100 and comp_tokens <= 0:
|
||||||
"id": generate_unique_id("chatcmpl"),
|
# 返回错误消息和状态码429
|
||||||
"object": "chat.completion",
|
error_response = {"error": "空回复"}
|
||||||
"created": int(time.time()), # 使用当前时间戳
|
return jsonify(error_response), 429
|
||||||
"model": model, # 使用请求中指定的模型
|
else:
|
||||||
"choices": [
|
response_json = {
|
||||||
{
|
"id": generate_unique_id("chatcmpl"),
|
||||||
"index": 0,
|
"object": "chat.completion",
|
||||||
"message": {
|
"created": int(time.time()), # 使用当前时间戳
|
||||||
"role": "assistant",
|
"model": model, # 使用请求中指定的模型
|
||||||
"content": all_new_text # 使用累积的文本
|
"choices": [
|
||||||
},
|
{
|
||||||
"finish_reason": "stop"
|
"index": 0,
|
||||||
}
|
"message": {
|
||||||
],
|
"role": "assistant",
|
||||||
"usage": {
|
"content": all_new_text # 使用累积的文本
|
||||||
# 这里的 token 计数需要根据实际情况计算
|
},
|
||||||
"prompt_tokens": input_tokens,
|
"finish_reason": "stop"
|
||||||
"completion_tokens": comp_tokens,
|
}
|
||||||
"total_tokens": input_tokens + comp_tokens
|
],
|
||||||
},
|
"usage": {
|
||||||
"system_fingerprint": None
|
# 这里的 token 计数需要根据实际情况计算
|
||||||
}
|
"prompt_tokens": input_tokens,
|
||||||
|
"completion_tokens": comp_tokens,
|
||||||
# 返回 JSON 响应
|
"total_tokens": input_tokens + comp_tokens
|
||||||
return jsonify(response_json)
|
},
|
||||||
|
"system_fingerprint": None
|
||||||
|
}
|
||||||
|
# 返回 JSON 响应
|
||||||
|
return jsonify(response_json)
|
||||||
else:
|
else:
|
||||||
return Response(generate(), mimetype='text/event-stream')
|
return Response(generate(), mimetype='text/event-stream')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user