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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -13
app.py CHANGED
@@ -433,7 +433,6 @@ def handsome_chat_completions():
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,13 +446,11 @@ def handsome_chat_completions():
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,10 +460,6 @@ def handsome_chat_completions():
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,7 +524,7 @@ def handsome_chat_completions():
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')
 
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
  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
  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
  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')