修改除了 json解析可能出错
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
"""简历诊断 AI 引擎:并行诊断 + 汇总评价"""
|
"""简历诊断 AI 引擎:并行诊断 + 汇总评价"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from json_repair import repair_json
|
||||||
|
|
||||||
from langchain_core.output_parsers import StrOutputParser
|
from langchain_core.output_parsers import StrOutputParser
|
||||||
from langchain_core.prompts import ChatPromptTemplate
|
from langchain_core.prompts import ChatPromptTemplate
|
||||||
|
|
||||||
@@ -16,14 +17,7 @@ def _parse_json(text: str) -> dict:
|
|||||||
"""解析 AI 输出的 JSON,自动去除 markdown 代码块包裹,容错处理"""
|
"""解析 AI 输出的 JSON,自动去除 markdown 代码块包裹,容错处理"""
|
||||||
cleaned = re.sub(r"^```(?:json)?\s*\n?", "", text.strip())
|
cleaned = re.sub(r"^```(?:json)?\s*\n?", "", text.strip())
|
||||||
cleaned = re.sub(r"\n?```\s*$", "", cleaned)
|
cleaned = re.sub(r"\n?```\s*$", "", cleaned)
|
||||||
try:
|
return repair_json(cleaned, return_objects=True)
|
||||||
return json.loads(cleaned)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# AI 可能在 JSON 字符串值中嵌入了未转义的引号,尝试提取最外层 { }
|
|
||||||
match = re.search(r"\{[\s\S]*\}", cleaned)
|
|
||||||
if match:
|
|
||||||
return json.loads(match.group())
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
# 诊断链(StrOutputParser 拿原始文本,再手动解析 JSON,避免 markdown 代码块导致解析失败)
|
# 诊断链(StrOutputParser 拿原始文本,再手动解析 JSON,避免 markdown 代码块导致解析失败)
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ loguru>=0.7.2,<1.0.0
|
|||||||
shortuuid>=1.0.11,<2.0.0
|
shortuuid>=1.0.11,<2.0.0
|
||||||
PyJWT>=2.8.0
|
PyJWT>=2.8.0
|
||||||
|
|
||||||
|
# JSON 容错解析
|
||||||
|
json-repair>=0.30.0
|
||||||
|
|
||||||
# 数据处理
|
# 数据处理
|
||||||
pandas>=2.2.0
|
pandas>=2.2.0
|
||||||
numpy>=1.26.0
|
numpy>=1.26.0
|
||||||
|
|||||||
Reference in New Issue
Block a user