fix: 修复 MiniMax 思考过程混入 JSON 响应的问题

main
Sileya 2026-04-04 17:11:01 +08:00
parent d5e64f40c4
commit 1ab84d4e9c
2 changed files with 18 additions and 8 deletions

View File

@ -26,6 +26,3 @@ markers = [
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
[tool.pytest.ini_options]
testpaths = ["tests"]

View File

@ -6,6 +6,7 @@
from __future__ import annotations
import os
import re
import json
import requests
from typing import Literal
@ -177,12 +178,24 @@ class PsychoScreener:
# 尝试解析 JSON
try:
# 提取 JSON可能模型返回带有 markdown 代码块)
content = raw_response.strip()
if content.startswith("```"):
lines = content.split("\n")
content = "\n".join(lines[1:-1]) # 去掉 ```json 和 ```
# 策略1查找 ```json ... ``` 代码块Markdown 格式)
md_match = re.search(r"```json\s*(.*?)\s*```", content, re.DOTALL)
if md_match:
content = md_match.group(1).strip()
else:
# 策略2查找原始 JSON 对象 { ... }
json_start = content.find('{"')
json_end = content.rfind('"}')
if json_start != -1 and json_end != -1 and json_end > json_start:
content = content[json_start:json_end + 2]
else:
# 策略3去掉思考过程标记取最后一个 { 之后的内容
last_brace = content.rfind('{')
if last_brace != -1:
content = content[last_brace:]
parsed = json.loads(content)
return ScreeningResult(
detected=parsed.get("detected", False),