yangtb24 commited on
Commit
be80fad
·
verified ·
1 Parent(s): fb1ddc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
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:
@@ -446,11 +447,13 @@ def handsome_chat_completions():
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"```Thinking\n{reasoning_content_accumulated}\n```"
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"
@@ -460,6 +463,10 @@ def handsome_chat_completions():
460
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {chunk.decode('utf-8')}")
461
  continue
462
 
 
 
 
 
463
  end_time = time.time()
464
  first_token_time = (
465
  first_chunk_time - start_time
@@ -524,7 +531,7 @@ def handsome_chat_completions():
524
  user_content_replaced = user_content.replace(
525
  '\n', '\\n'
526
  ).replace('\r', '\\n')
527
- response_content_replaced = (f"```Thinking\n{reasoning_content_accumulated}\n```\n" if reasoning_content_accumulated else "") + content_accumulated
528
  response_content_replaced = response_content_replaced.replace(
529
  '\n', '\\n'
530
  ).replace('\r', '\\n')
 
433
  full_response_content = ""
434
  reasoning_content_accumulated = "" # Accumulate reasoning content
435
  content_accumulated = "" # Accumulate regular content
436
+ is_first_reasoning = True
437
 
438
  for chunk in response.iter_content(chunk_size=1024):
439
  if chunk:
 
447
  delta = chunk_json["choices"][0].get("delta", {})
448
 
449
  if delta.get("reasoning_content") is not None:
450
+ if is_first_reasoning:
451
+ reasoning_content_accumulated += f"```Thinking\n{delta.get('reasoning_content', '')}"
452
+ is_first_reasoning = False
453
+ else:
454
+ reasoning_content_accumulated += delta.get("reasoning_content", "")
455
+
456
+
457
  if delta.get("content") is not None:
458
  content_accumulated += delta.get("content", "")
459
  yield f"data: {json.dumps({'choices': [{'delta': {'content': content_accumulated}, 'index': 0, 'finish_reason': None}]})}\n\n"
 
463
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {chunk.decode('utf-8')}")
464
  continue
465
 
466
+ if reasoning_content_accumulated:
467
+ reasoning_content_accumulated += "\n```"
468
+ yield f"data: {json.dumps({'choices': [{'delta': {'content': reasoning_content_accumulated}, 'index': 0, 'finish_reason': None}]})}\n\n"
469
+
470
  end_time = time.time()
471
  first_token_time = (
472
  first_chunk_time - start_time
 
531
  user_content_replaced = user_content.replace(
532
  '\n', '\\n'
533
  ).replace('\r', '\\n')
534
+ response_content_replaced = (f"{reasoning_content_accumulated}\n" if reasoning_content_accumulated else "") + content_accumulated
535
  response_content_replaced = response_content_replaced.replace(
536
  '\n', '\\n'
537
  ).replace('\r', '\\n')