From 1ab84d4e9c3b335d7feac3066782aa6fb24e3231 Mon Sep 17 00:00:00 2001 From: Sileya Date: Sat, 4 Apr 2026 17:11:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20MiniMax=20=E6=80=9D?= =?UTF-8?q?=E8=80=83=E8=BF=87=E7=A8=8B=E6=B7=B7=E5=85=A5=20JSON=20?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 3 --- src/psycho_screener/screener.py | 23 ++++++++++++++++++----- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6e07c4c..09e0fed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,3 @@ markers = [ [build-system] requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" - -[tool.pytest.ini_options] -testpaths = ["tests"] diff --git a/src/psycho_screener/screener.py b/src/psycho_screener/screener.py index 9f3d149..03d9087 100644 --- a/src/psycho_screener/screener.py +++ b/src/psycho_screener/screener.py @@ -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),