fix: 修复 MiniMax 思考过程混入 JSON 响应的问题
parent
d5e64f40c4
commit
1ab84d4e9c
|
|
@ -26,6 +26,3 @@ markers = [
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["setuptools>=61.0"]
|
requires = ["setuptools>=61.0"]
|
||||||
build-backend = "setuptools.build_meta"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
|
||||||
testpaths = ["tests"]
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
from typing import Literal
|
from typing import Literal
|
||||||
|
|
@ -177,11 +178,23 @@ class PsychoScreener:
|
||||||
|
|
||||||
# 尝试解析 JSON
|
# 尝试解析 JSON
|
||||||
try:
|
try:
|
||||||
# 提取 JSON(可能模型返回带有 markdown 代码块)
|
|
||||||
content = raw_response.strip()
|
content = raw_response.strip()
|
||||||
if content.startswith("```"):
|
|
||||||
lines = content.split("\n")
|
# 策略1:查找 ```json ... ``` 代码块(Markdown 格式)
|
||||||
content = "\n".join(lines[1:-1]) # 去掉 ```json 和 ```
|
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)
|
parsed = json.loads(content)
|
||||||
return ScreeningResult(
|
return ScreeningResult(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue