抽象json提取风格

This commit is contained in:
zk
2026-04-23 17:49:06 +08:00
parent fa7d87e435
commit 8fbb1f6fca
6 changed files with 34 additions and 37 deletions
+15
View File
@@ -0,0 +1,15 @@
"""AI 输出 JSON 解析工具
将 LLM 返回的可能带 markdown 代码块包裹的文本解析为 Python 对象。
"""
import re
from json_repair import repair_json
def parse_llm_json(text: str):
"""解析 AI 输出的 JSON,自动去除 markdown 代码块包裹,容错处理"""
cleaned = re.sub(r"^```(?:json)?\s*\n?", "", text.strip())
cleaned = re.sub(r"\n?```\s*$", "", cleaned)
return repair_json(cleaned, return_objects=True)