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:
666ghj 2026-03-06 00:30:31 +08:00
parent a1ff79c0ef
commit 985f89f49a

View file

@ -4,6 +4,7 @@ LLM客户端封装
"""
import json
import re
from typing import Optional, Dict, Any, List
from openai import OpenAI
@ -61,7 +62,10 @@ class LLMClient:
kwargs["response_format"] = response_format
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(
self,
@ -86,6 +90,14 @@ class LLMClient:
max_tokens=max_tokens,
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}")