yangtb24 commited on
Commit
869de0e
·
verified ·
1 Parent(s): 199c193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -433,6 +433,7 @@ def handsome_chat_completions():
433
  full_response_content = ""
434
  reasoning_content_accumulated = "" # Accumulate reasoning content
435
  content_accumulated = "" # Accumulate regular content
 
436
 
437
  for chunk in response.iter_content(chunk_size=1024):
438
  if chunk:
@@ -440,25 +441,32 @@ def handsome_chat_completions():
440
  first_chunk_time = time.time()
441
  full_response_content += chunk.decode("utf-8")
442
 
443
- try:
444
- chunk_json = json.loads(chunk.decode("utf-8").lstrip("data: ").strip())
445
- if "choices" in chunk_json and len(chunk_json["choices"]) > 0:
446
- delta = chunk_json["choices"][0].get("delta", {})
447
-
448
- if delta.get("reasoning_content") is not None:
449
- reasoning_content_accumulated += delta.get("reasoning_content", "")
450
- formatted_reasoning = f"{reasoning_content_accumulated}"
451
- yield f"data: {json.dumps({'choices': [{'delta': {'content': formatted_reasoning}, 'index': 0, 'finish_reason': None}]})}\n\n"
452
- reasoning_content_accumulated = ""
453
-
454
- if delta.get("content") is not None:
455
- content_accumulated += delta.get("content", "")
456
- yield f"data: {json.dumps({'choices': [{'delta': {'content': content_accumulated}, 'index': 0, 'finish_reason': None}]})}\n\n"
457
- content_accumulated = ""
458
-
459
- except (KeyError, ValueError, json.JSONDecodeError) as e:
460
- logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {chunk.decode('utf-8')}")
461
- continue
 
 
 
 
 
 
 
462
 
463
  end_time = time.time()
464
  first_token_time = (
@@ -652,7 +660,6 @@ def handsome_chat_completions():
652
  logging.error(f"请求转发异常: {e}")
653
  return jsonify({"error": str(e)}), 500
654
 
655
-
656
  if __name__ == '__main__':
657
  logging.info(f"环境变量:{os.environ}")
658
 
@@ -673,4 +680,4 @@ if __name__ == '__main__':
673
  debug=False,
674
  host='0.0.0.0',
675
  port=int(os.environ.get('PORT', 7860))
676
- )
 
433
  full_response_content = ""
434
  reasoning_content_accumulated = "" # Accumulate reasoning content
435
  content_accumulated = "" # Accumulate regular content
436
+ first_reasoning_chunk = True # Flag to track the first reasoning chunk
437
 
438
  for chunk in response.iter_content(chunk_size=1024):
439
  if chunk:
 
441
  first_chunk_time = time.time()
442
  full_response_content += chunk.decode("utf-8")
443
 
444
+ for line in chunk.decode("utf-8").splitlines():
445
+ if line.startswith("data:"):
446
+ try:
447
+ chunk_json = json.loads(line.lstrip("data: ").strip())
448
+ if "choices" in chunk_json and len(chunk_json["choices"]) > 0:
449
+ delta = chunk_json["choices"][0].get("delta", {})
450
+
451
+ if delta.get("reasoning_content") is not None:
452
+ if first_reasoning_chunk:
453
+ reasoning_content_accumulated += "```Thinking\n"
454
+ first_reasoning_chunk = False
455
+ reasoning_content_accumulated += delta.get("reasoning_content", "")
456
+
457
+ if delta.get("content") is not None:
458
+ if not first_reasoning_chunk:
459
+ reasoning_content_accumulated += "\n```"
460
+ first_reasoning_chunk = True
461
+ content_accumulated += delta.get("content", "")
462
+ yield f"data: {json.dumps({'choices': [{'delta': {'content': (reasoning_content_accumulated if reasoning_content_accumulated else '') + content_accumulated}, 'index': 0, 'finish_reason': None}]})}\n\n"
463
+ reasoning_content_accumulated = ""
464
+ content_accumulated = ""
465
+
466
+ except (KeyError, ValueError, json.JSONDecodeError) as e:
467
+ logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
468
+ continue
469
+
470
 
471
  end_time = time.time()
472
  first_token_time = (
 
660
  logging.error(f"请求转发异常: {e}")
661
  return jsonify({"error": str(e)}), 500
662
 
 
663
  if __name__ == '__main__':
664
  logging.info(f"环境变量:{os.environ}")
665
 
 
680
  debug=False,
681
  host='0.0.0.0',
682
  port=int(os.environ.get('PORT', 7860))
683
+ )