yangtb24 commited on
Commit
79bb4ee
1 Parent(s): af677b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import time
3
  import logging
4
  import requests
 
5
  import concurrent.futures
6
  from datetime import datetime, timedelta
7
  from apscheduler.schedulers.background import BackgroundScheduler
@@ -308,8 +309,6 @@ def check_tokens():
308
 
309
  return jsonify(results)
310
 
311
- import json
312
-
313
  @app.route('/handsome/v1/chat/completions', methods=['POST'])
314
  def handsome_chat_completions():
315
  if not check_authorization(request):
@@ -369,28 +368,28 @@ def handsome_chat_completions():
369
  continue
370
  try:
371
  response_json = json.loads(line)
372
-
373
  if "usage" in response_json and "completion_tokens" in response_json["usage"]:
374
  completion_tokens = response_json["usage"]["completion_tokens"]
375
-
376
  if "choices" in response_json and len(response_json["choices"]) > 0 and "delta" in response_json["choices"][0] and "content" in response_json["choices"][0]["delta"]:
377
  response_content += response_json["choices"][0]["delta"]["content"]
378
-
379
  if "usage" in response_json and "prompt_tokens" in response_json["usage"]:
380
  prompt_tokens = response_json["usage"]["prompt_tokens"]
381
 
382
  except (KeyError, ValueError, IndexError) as e:
383
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
384
-
385
  user_content = ""
386
  messages = data.get("messages", [])
387
  for message in messages:
388
- if message["role"] == "user":
389
  user_content += message["content"] + " "
390
  user_content = user_content.strip()
391
 
392
  logging.info(
393
- f"使用的key: {api_key}, 提示token: {prompt_tokens}, 输出token: {completion_tokens}, 首字用时: {first_token_time:.4f}秒, 总共用时: {total_time:.4f}秒, 使用的模型: {model_name}, 用户的内容: {user_content}, 输出的内容: {response_content}"
394
  )
395
  return Response(stream_with_context(generate()), content_type=response.headers['Content-Type'])
396
  else:
@@ -412,12 +411,12 @@ def handsome_chat_completions():
412
  user_content = ""
413
  messages = data.get("messages", [])
414
  for message in messages:
415
- if message["role"] == "user":
416
  user_content += message["content"] + " "
417
  user_content = user_content.strip()
418
 
419
  logging.info(
420
- f"使用的key: {api_key}, 提示token: {prompt_tokens}, 输出token: {completion_tokens}, 首字用时: 0, 总共用时: {total_time:.4f}秒, 使用的模型: {model_name}, 用户的内容: {user_content}, 输出的内容: {response_content}"
421
  )
422
  return jsonify(response_json)
423
 
 
2
  import time
3
  import logging
4
  import requests
5
+ import json
6
  import concurrent.futures
7
  from datetime import datetime, timedelta
8
  from apscheduler.schedulers.background import BackgroundScheduler
 
309
 
310
  return jsonify(results)
311
 
 
 
312
  @app.route('/handsome/v1/chat/completions', methods=['POST'])
313
  def handsome_chat_completions():
314
  if not check_authorization(request):
 
368
  continue
369
  try:
370
  response_json = json.loads(line)
371
+
372
  if "usage" in response_json and "completion_tokens" in response_json["usage"]:
373
  completion_tokens = response_json["usage"]["completion_tokens"]
374
+
375
  if "choices" in response_json and len(response_json["choices"]) > 0 and "delta" in response_json["choices"][0] and "content" in response_json["choices"][0]["delta"]:
376
  response_content += response_json["choices"][0]["delta"]["content"]
377
+
378
  if "usage" in response_json and "prompt_tokens" in response_json["usage"]:
379
  prompt_tokens = response_json["usage"]["prompt_tokens"]
380
 
381
  except (KeyError, ValueError, IndexError) as e:
382
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
383
+
384
  user_content = ""
385
  messages = data.get("messages", [])
386
  for message in messages:
387
+ if message["role"] == "user" and isinstance(message["content"], str):
388
  user_content += message["content"] + " "
389
  user_content = user_content.strip()
390
 
391
  logging.info(
392
+ f"使用的key: {api_key}, 提示token: {prompt_tokens}, 输出token: {completion_tokens}, 首字用时: {first_token_time:.4f}秒, 总共用时: {total_time:.4f}秒, 使用的模型: {model_name}, 用户的内容: {user_content.replace(chr(10), '\\n').replace(chr(13), '\\n')}, 输出的内容: {response_content.replace(chr(10), '\\n').replace(chr(13), '\\n')}"
393
  )
394
  return Response(stream_with_context(generate()), content_type=response.headers['Content-Type'])
395
  else:
 
411
  user_content = ""
412
  messages = data.get("messages", [])
413
  for message in messages:
414
+ if message["role"] == "user" and isinstance(message["content"], str):
415
  user_content += message["content"] + " "
416
  user_content = user_content.strip()
417
 
418
  logging.info(
419
+ f"使用的key: {api_key}, 提示token: {prompt_tokens}, 输出token: {completion_tokens}, 首字用时: 0, 总共用时: {total_time:.4f}秒, 使用的模型: {model_name}, 用户的内容: {user_content.replace(chr(10), '\\n').replace(chr(13), '\\n')}, 输出的内容: {response_content.replace(chr(10), '\\n').replace(chr(13), '\\n')}"
420
  )
421
  return jsonify(response_json)
422