yangtb24 commited on
Commit
bf0d834
1 Parent(s): d91f35e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -2
app.py CHANGED
@@ -358,6 +358,7 @@ def handsome_chat_completions():
358
  first_token_time = first_chunk_time - start_time if first_chunk_time else 0
359
  total_time = end_time - start_time
360
 
 
361
  prompt_tokens = 0
362
  completion_tokens = 0
363
  response_content = ""
@@ -369,6 +370,7 @@ def handsome_chat_completions():
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
 
@@ -381,6 +383,7 @@ def handsome_chat_completions():
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:
@@ -388,16 +391,19 @@ def handsome_chat_completions():
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}, 首字用时: 0, 总共用时: {total_time:.4f}秒, 使用的模型: {model_name}, 用户的内容: {user_content.replace(chr(10), r'\\n').replace(chr(13), r'\\n')}, 输出的内容: {response_content.replace(chr(10), r'\\n').replace(chr(13), r'\\n')}"
393
  )
394
  return Response(stream_with_context(generate()), content_type=response.headers['Content-Type'])
395
  else:
 
396
  response.raise_for_status()
397
  end_time = time.time()
398
  response_json = response.json()
399
  total_time = end_time - start_time
400
 
 
401
  try:
402
  prompt_tokens = response_json["usage"]["prompt_tokens"]
403
  completion_tokens = response_json["usage"]["completion_tokens"]
@@ -408,6 +414,7 @@ def handsome_chat_completions():
408
  completion_tokens = 0
409
  response_content = ""
410
 
 
411
  user_content = ""
412
  messages = data.get("messages", [])
413
  for message in messages:
@@ -415,8 +422,9 @@ def handsome_chat_completions():
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), r'\\n').replace(chr(13), r'\\n')}, 输出的内容: {response_content.replace(chr(10), r'\\n').replace(chr(13), r'\\n')}"
420
  )
421
  return jsonify(response_json)
422
 
 
358
  first_token_time = first_chunk_time - start_time if first_chunk_time else 0
359
  total_time = end_time - start_time
360
 
361
+ # 处理流式响应,逐行解析 JSON
362
  prompt_tokens = 0
363
  completion_tokens = 0
364
  response_content = ""
 
370
  try:
371
  response_json = json.loads(line)
372
 
373
+ # 提取信息,这里只累加 completion_tokens 和 content
374
  if "usage" in response_json and "completion_tokens" in response_json["usage"]:
375
  completion_tokens = response_json["usage"]["completion_tokens"]
376
 
 
383
  except (KeyError, ValueError, IndexError) as e:
384
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
385
 
386
+ # 提取用户输入的内容,忽略非文本内容
387
  user_content = ""
388
  messages = data.get("messages", [])
389
  for message in messages:
 
391
  user_content += message["content"] + " "
392
  user_content = user_content.strip()
393
 
394
+ # 记录日志,将换行符替换为 \n
395
  logging.info(
396
+ 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')}"
397
  )
398
  return Response(stream_with_context(generate()), content_type=response.headers['Content-Type'])
399
  else:
400
+ # 非流式响应处理... (保持原样)
401
  response.raise_for_status()
402
  end_time = time.time()
403
  response_json = response.json()
404
  total_time = end_time - start_time
405
 
406
+ # 从响应中提取信息
407
  try:
408
  prompt_tokens = response_json["usage"]["prompt_tokens"]
409
  completion_tokens = response_json["usage"]["completion_tokens"]
 
414
  completion_tokens = 0
415
  response_content = ""
416
 
417
+ # 提取用户输入的内容,忽略非文本内容
418
  user_content = ""
419
  messages = data.get("messages", [])
420
  for message in messages:
 
422
  user_content += message["content"] + " "
423
  user_content = user_content.strip()
424
 
425
+ # 记录日志,将换行符替换为 \n
426
  logging.info(
427
+ 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')}"
428
  )
429
  return jsonify(response_json)
430