yangtb24 commited on
Commit
2a0d3ff
·
verified ·
1 Parent(s): 44634fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -403,13 +403,15 @@ def handsome_chat_completions():
403
  content_accumulated = ""
404
  first_reasoning_chunk = True
405
 
406
- for chunk in response.iter_content(chunk_size=1024):
407
  if chunk:
408
  if first_chunk_time is None:
409
  first_chunk_time = time.time()
410
  full_response_content += chunk.decode("utf-8")
411
 
412
  for line in chunk.decode("utf-8").splitlines():
 
 
413
  if line.startswith("data:"):
414
  try:
415
  chunk_json = json.loads(line.lstrip("data: ").strip())
@@ -418,14 +420,17 @@ def handsome_chat_completions():
418
 
419
  if delta.get("reasoning_content") is not None:
420
  reasoning_chunk = delta["reasoning_content"]
 
421
  if first_reasoning_chunk:
422
- reasoning_chunk = "```Thinking\n" + reasoning_chunk
423
  first_reasoning_chunk = False
424
  yield f"data: {json.dumps({'choices': [{'delta': {'content': reasoning_chunk}, 'index': 0}]})}\n\n"
425
 
426
  if delta.get("content") is not None:
427
  if not first_reasoning_chunk:
428
- yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n```\n'}, 'index': 0}]})}\n\n"
 
 
429
  first_reasoning_chunk = True
430
  yield f"data: {json.dumps({'choices': [{'delta': {'content': delta["content"]}, 'index': 0}]})}\n\n"
431
 
@@ -433,7 +438,6 @@ def handsome_chat_completions():
433
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
434
  continue
435
 
436
-
437
  end_time = time.time()
438
  first_token_time = (
439
  first_chunk_time - start_time
 
403
  content_accumulated = ""
404
  first_reasoning_chunk = True
405
 
406
+ for chunk in response.iter_content(chunk_size=100000):
407
  if chunk:
408
  if first_chunk_time is None:
409
  first_chunk_time = time.time()
410
  full_response_content += chunk.decode("utf-8")
411
 
412
  for line in chunk.decode("utf-8").splitlines():
413
+ # print(line)
414
+
415
  if line.startswith("data:"):
416
  try:
417
  chunk_json = json.loads(line.lstrip("data: ").strip())
 
420
 
421
  if delta.get("reasoning_content") is not None:
422
  reasoning_chunk = delta["reasoning_content"]
423
+ reasoning_chunk = reasoning_chunk.replace('\n', '\n> ')
424
  if first_reasoning_chunk:
425
+ reasoning_chunk = "> " + reasoning_chunk
426
  first_reasoning_chunk = False
427
  yield f"data: {json.dumps({'choices': [{'delta': {'content': reasoning_chunk}, 'index': 0}]})}\n\n"
428
 
429
  if delta.get("content") is not None:
430
  if not first_reasoning_chunk:
431
+ # yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n```\n'}, 'index': 0}]})}\n\n"
432
+ # yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n\n---\n\n### 结果输出\n'}, 'index': 0}]})}\n\n"
433
+ yield f"data: {json.dumps({'choices': [{'delta': {'content': '\n'}, 'index': 0}]})}\n\n"
434
  first_reasoning_chunk = True
435
  yield f"data: {json.dumps({'choices': [{'delta': {'content': delta["content"]}, 'index': 0}]})}\n\n"
436
 
 
438
  logging.error(f"解析流式响应单行 JSON 失败: {e}, 行内容: {line}")
439
  continue
440
 
 
441
  end_time = time.time()
442
  first_token_time = (
443
  first_chunk_time - start_time