Update app.py
Browse files
app.py
CHANGED
|
@@ -310,6 +310,9 @@ def check_tokens():
|
|
| 310 |
|
| 311 |
@app.route('/handsome/v1/chat/completions', methods=['POST'])
|
| 312 |
def handsome_chat_completions():
|
|
|
|
|
|
|
|
|
|
| 313 |
if not check_authorization(request):
|
| 314 |
return jsonify({"error": "Unauthorized"}), 401
|
| 315 |
|
|
@@ -363,7 +366,8 @@ def handsome_chat_completions():
|
|
| 363 |
prompt_tokens = response_json["usage"]["prompt_tokens"]
|
| 364 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
| 365 |
response_content = response_json["choices"][0]["message"]["content"]
|
| 366 |
-
except (KeyError, ValueError, IndexError):
|
|
|
|
| 367 |
prompt_tokens = 0
|
| 368 |
completion_tokens = 0
|
| 369 |
response_content = ""
|
|
@@ -392,11 +396,12 @@ def handsome_chat_completions():
|
|
| 392 |
prompt_tokens = response_json["usage"]["prompt_tokens"]
|
| 393 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
| 394 |
response_content = response_json["choices"][0]["message"]["content"]
|
| 395 |
-
except (KeyError, ValueError, IndexError):
|
|
|
|
| 396 |
prompt_tokens = 0
|
| 397 |
completion_tokens = 0
|
| 398 |
response_content = ""
|
| 399 |
-
|
| 400 |
# 提取用户输入的内容
|
| 401 |
user_content = ""
|
| 402 |
messages = data.get("messages", [])
|
|
|
|
| 310 |
|
| 311 |
@app.route('/handsome/v1/chat/completions', methods=['POST'])
|
| 312 |
def handsome_chat_completions():
|
| 313 |
+
"""
|
| 314 |
+
处理 /handsome/v1/chat/completions 路由的请求,添加鉴权,实现 KEY 的轮询和重试机制,并记录日志。
|
| 315 |
+
"""
|
| 316 |
if not check_authorization(request):
|
| 317 |
return jsonify({"error": "Unauthorized"}), 401
|
| 318 |
|
|
|
|
| 366 |
prompt_tokens = response_json["usage"]["prompt_tokens"]
|
| 367 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
| 368 |
response_content = response_json["choices"][0]["message"]["content"]
|
| 369 |
+
except (KeyError, ValueError, IndexError) as e:
|
| 370 |
+
logging.error(f"解析流式响应 JSON 失败: {e}, 完整响应内容: {full_response_content}")
|
| 371 |
prompt_tokens = 0
|
| 372 |
completion_tokens = 0
|
| 373 |
response_content = ""
|
|
|
|
| 396 |
prompt_tokens = response_json["usage"]["prompt_tokens"]
|
| 397 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
| 398 |
response_content = response_json["choices"][0]["message"]["content"]
|
| 399 |
+
except (KeyError, ValueError, IndexError) as e:
|
| 400 |
+
logging.error(f"解析非流式响应 JSON 失败: {e}")
|
| 401 |
prompt_tokens = 0
|
| 402 |
completion_tokens = 0
|
| 403 |
response_content = ""
|
| 404 |
+
|
| 405 |
# 提取用户输入的内容
|
| 406 |
user_content = ""
|
| 407 |
messages = data.get("messages", [])
|