fix: 修复 MiniMax 思考过程混入 JSON 响应的问题
parent
d5e64f40c4
commit
1ab84d4e9c
|
|
@ -26,6 +26,3 @@ markers = [
|
|||
[build-system]
|
||||
requires = ["setuptools>=61.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import requests
|
||||
from typing import Literal
|
||||
|
|
@ -177,11 +178,23 @@ 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(
|
||||
|
|
|
|||
Loading…
Reference in New Issue