yangtb24 commited on
Commit
529b23d
·
verified ·
1 Parent(s): 7008444

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -495,17 +495,14 @@ def handsome_chat_completions():
495
  "usage"
496
  ]["completion_tokens"]
497
 
498
- if (
499
- "choices" in response_json and
500
- len(response_json["choices"]) > 0 and
501
- "delta" in response_json["choices"][0] and
502
- "content" in response_json[
503
- "choices"
504
- ][0]["delta"]
505
- ):
506
- response_content += response_json[
507
- "choices"
508
- ][0]["delta"]["content"]
509
 
510
  if (
511
  "usage" in response_json and
@@ -581,9 +578,15 @@ def handsome_chat_completions():
581
  completion_tokens = response_json[
582
  "usage"
583
  ]["completion_tokens"]
584
- response_content = response_json[
585
- "choices"
586
- ][0]["message"]["content"]
 
 
 
 
 
 
587
  except (KeyError, ValueError, IndexError) as e:
588
  logging.error(
589
  f"解析非流式响应 JSON 失败: {e}, "
@@ -634,8 +637,31 @@ def handsome_chat_completions():
634
  token_counts.append(response_json["usage"]["prompt_tokens"] + response_json["usage"]["completion_tokens"])
635
  else:
636
  token_counts.append(0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
637
 
638
- return jsonify(response_json)
639
 
640
  except requests.exceptions.RequestException as e:
641
  logging.error(f"请求转发异常: {e}")
 
495
  "usage"
496
  ]["completion_tokens"]
497
 
498
+ if "choices" in response_json and len(response_json["choices"]) > 0:
499
+ delta = response_json["choices"][0].get("delta", {})
500
+ if "reasoning_content" in delta and delta["reasoning_content"]:
501
+ reasoning_lines = delta["reasoning_content"].splitlines()
502
+ formatted_reasoning = "\n".join(f"> {line}" for line in reasoning_lines)
503
+ response_content += formatted_reasoning + "\n" # Add a newline after reasoning
504
+ if "content" in delta and delta["content"]:
505
+ response_content += delta["content"]
 
 
 
506
 
507
  if (
508
  "usage" in response_json and
 
578
  completion_tokens = response_json[
579
  "usage"
580
  ]["completion_tokens"]
581
+ response_content = ""
582
+ if "choices" in response_json and len(response_json["choices"]) > 0:
583
+ choice = response_json["choices"][0]
584
+ if "reasoning_content" in choice:
585
+ reasoning_lines = choice["reasoning_content"].splitlines()
586
+ formatted_reasoning = "\n".join(f"> {line}" for line in reasoning_lines)
587
+ response_content += formatted_reasoning + "\n"
588
+ if "message" in choice and "content" in choice["message"]:
589
+ response_content += choice["message"]["content"]
590
  except (KeyError, ValueError, IndexError) as e:
591
  logging.error(
592
  f"解析非流式响应 JSON 失败: {e}, "
 
637
  token_counts.append(response_json["usage"]["prompt_tokens"] + response_json["usage"]["completion_tokens"])
638
  else:
639
  token_counts.append(0)
640
+
641
+ # Reformat the response to standard OpenAI format for non-streaming responses
642
+ formatted_response = {
643
+ "id": response_json.get("id", ""),
644
+ "object": "chat.completion",
645
+ "created": response_json.get("created", int(time.time())),
646
+ "model": model_name,
647
+ "choices": [
648
+ {
649
+ "index": 0,
650
+ "message": {
651
+ "role": "assistant",
652
+ "content": response_content
653
+ },
654
+ "finish_reason": "stop"
655
+ }
656
+ ],
657
+ "usage": {
658
+ "prompt_tokens": prompt_tokens,
659
+ "completion_tokens": completion_tokens,
660
+ "total_tokens": prompt_tokens + completion_tokens
661
+ }
662
+ }
663
 
664
+ return jsonify(formatted_response)
665
 
666
  except requests.exceptions.RequestException as e:
667
  logging.error(f"请求转发异常: {e}")