666ghj commited on
Commit
bdf6eea
·
1 Parent(s): 57ab2d1

fix: resolve 500 error caused by <think> tags and markdown code fences in content field from reasoning models like MiniMax/GLM

Browse files
Files changed (1) hide show
  1. backend/app/utils/llm_client.py +15 -3
backend/app/utils/llm_client.py CHANGED
@@ -4,6 +4,7 @@ LLM客户端封装
4
  """
5
 
6
  import json
 
7
  from typing import Optional, Dict, Any, List
8
  from openai import OpenAI
9
 
@@ -61,7 +62,10 @@ class LLMClient:
61
  kwargs["response_format"] = response_format
62
 
63
  response = self.client.chat.completions.create(**kwargs)
64
- return response.choices[0].message.content
 
 
 
65
 
66
  def chat_json(
67
  self,
@@ -86,6 +90,14 @@ class LLMClient:
86
  max_tokens=max_tokens,
87
  response_format={"type": "json_object"}
88
  )
89
-
90
- return json.loads(response)
 
 
 
 
 
 
 
 
91
 
 
4
  """
5
 
6
  import json
7
+ import re
8
  from typing import Optional, Dict, Any, List
9
  from openai import OpenAI
10
 
 
62
  kwargs["response_format"] = response_format
63
 
64
  response = self.client.chat.completions.create(**kwargs)
65
+ content = response.choices[0].message.content
66
+ # 部分模型(如MiniMax M2.5)会在content中包含<think>思考内容,需要移除
67
+ content = re.sub(r'<think>[\s\S]*?</think>', '', content).strip()
68
+ return content
69
 
70
  def chat_json(
71
  self,
 
90
  max_tokens=max_tokens,
91
  response_format={"type": "json_object"}
92
  )
93
+ # 清理markdown代码块标记
94
+ cleaned_response = response.strip()
95
+ cleaned_response = re.sub(r'^```(?:json)?\s*\n?', '', cleaned_response, flags=re.IGNORECASE)
96
+ cleaned_response = re.sub(r'\n?```\s*$', '', cleaned_response)
97
+ cleaned_response = cleaned_response.strip()
98
+
99
+ try:
100
+ return json.loads(cleaned_response)
101
+ except json.JSONDecodeError:
102
+ raise ValueError(f"LLM返回的JSON格式无效: {cleaned_response}")
103