fix: resolve 500 error caused by <think> tags and markdown code fences in content field from reasoning models like MiniMax/GLM
This commit is contained in:
parent
a1ff79c0ef
commit
985f89f49a
1 changed files with 15 additions and 3 deletions
|
|
@ -4,6 +4,7 @@ LLM客户端封装
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import re
|
||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
|
|
@ -61,7 +62,10 @@ class LLMClient:
|
||||||
kwargs["response_format"] = response_format
|
kwargs["response_format"] = response_format
|
||||||
|
|
||||||
response = self.client.chat.completions.create(**kwargs)
|
response = self.client.chat.completions.create(**kwargs)
|
||||||
return response.choices[0].message.content
|
content = response.choices[0].message.content
|
||||||
|
# 部分模型(如MiniMax M2.5)会在content中包含<think>思考内容,需要移除
|
||||||
|
content = re.sub(r'<think>[\s\S]*?</think>', '', content).strip()
|
||||||
|
return content
|
||||||
|
|
||||||
def chat_json(
|
def chat_json(
|
||||||
self,
|
self,
|
||||||
|
|
@ -86,6 +90,14 @@ class LLMClient:
|
||||||
max_tokens=max_tokens,
|
max_tokens=max_tokens,
|
||||||
response_format={"type": "json_object"}
|
response_format={"type": "json_object"}
|
||||||
)
|
)
|
||||||
|
# 清理markdown代码块标记
|
||||||
|
cleaned_response = response.strip()
|
||||||
|
cleaned_response = re.sub(r'^```(?:json)?\s*\n?', '', cleaned_response, flags=re.IGNORECASE)
|
||||||
|
cleaned_response = re.sub(r'\n?```\s*$', '', cleaned_response)
|
||||||
|
cleaned_response = cleaned_response.strip()
|
||||||
|
|
||||||
return json.loads(response)
|
try:
|
||||||
|
return json.loads(cleaned_response)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise ValueError(f"LLM返回的JSON格式无效: {cleaned_response}")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue