Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -310,9 +310,6 @@ def check_tokens():
|
|
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 |
|
@@ -355,11 +352,11 @@ def handsome_chat_completions():
|
|
355 |
first_chunk_time = time.time()
|
356 |
full_response_content += chunk.decode("utf-8")
|
357 |
yield chunk
|
358 |
-
|
359 |
end_time = time.time()
|
360 |
first_token_time = first_chunk_time - start_time if first_chunk_time else 0
|
361 |
total_time = end_time - start_time
|
362 |
-
|
363 |
# 从完整响应中提取信息
|
364 |
try:
|
365 |
response_json = json.loads(full_response_content.split("data: ")[-1].strip())
|
@@ -367,11 +364,11 @@ def handsome_chat_completions():
|
|
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},
|
371 |
prompt_tokens = 0
|
372 |
completion_tokens = 0
|
373 |
response_content = ""
|
374 |
-
|
375 |
# 提取用户输入的内容
|
376 |
user_content = ""
|
377 |
messages = data.get("messages", [])
|
@@ -397,7 +394,7 @@ def handsome_chat_completions():
|
|
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 = ""
|
@@ -419,6 +416,7 @@ def handsome_chat_completions():
|
|
419 |
except requests.exceptions.RequestException as e:
|
420 |
return jsonify({"error": str(e)}), 500
|
421 |
|
|
|
422 |
@app.route('/handsome/v1/models', methods=['GET'])
|
423 |
def list_models():
|
424 |
if not check_authorization(request):
|
|
|
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 |
|
|
|
352 |
first_chunk_time = time.time()
|
353 |
full_response_content += chunk.decode("utf-8")
|
354 |
yield chunk
|
355 |
+
|
356 |
end_time = time.time()
|
357 |
first_token_time = first_chunk_time - start_time if first_chunk_time else 0
|
358 |
total_time = end_time - start_time
|
359 |
+
|
360 |
# 从完整响应中提取信息
|
361 |
try:
|
362 |
response_json = json.loads(full_response_content.split("data: ")[-1].strip())
|
|
|
364 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
365 |
response_content = response_json["choices"][0]["message"]["content"]
|
366 |
except (KeyError, ValueError, IndexError) as e:
|
367 |
+
logging.error(f"解析流式响应 JSON 失败: {e}, 完整内容: {full_response_content}")
|
368 |
prompt_tokens = 0
|
369 |
completion_tokens = 0
|
370 |
response_content = ""
|
371 |
+
|
372 |
# 提取用户输入的内容
|
373 |
user_content = ""
|
374 |
messages = data.get("messages", [])
|
|
|
394 |
completion_tokens = response_json["usage"]["completion_tokens"]
|
395 |
response_content = response_json["choices"][0]["message"]["content"]
|
396 |
except (KeyError, ValueError, IndexError) as e:
|
397 |
+
logging.error(f"解析非流式响应 JSON 失败: {e}, 完整内容: {response_json}")
|
398 |
prompt_tokens = 0
|
399 |
completion_tokens = 0
|
400 |
response_content = ""
|
|
|
416 |
except requests.exceptions.RequestException as e:
|
417 |
return jsonify({"error": str(e)}), 500
|
418 |
|
419 |
+
|
420 |
@app.route('/handsome/v1/models', methods=['GET'])
|
421 |
def list_models():
|
422 |
if not check_authorization(request):
|