20 lines
553 B
Python
20 lines
553 B
Python
|
|
import json
|
||
|
|
import re
|
||
|
|
|
||
|
|
from app.core.exceptions import LLMParseError
|
||
|
|
|
||
|
|
|
||
|
|
def extract_json(text: str) -> any:
|
||
|
|
"""Parse JSON from LLM response, stripping Markdown code fences if present."""
|
||
|
|
text = text.strip()
|
||
|
|
|
||
|
|
# Strip ```json ... ``` or ``` ... ``` fences
|
||
|
|
fence_match = re.search(r"```(?:json)?\s*([\s\S]+?)\s*```", text)
|
||
|
|
if fence_match:
|
||
|
|
text = fence_match.group(1).strip()
|
||
|
|
|
||
|
|
try:
|
||
|
|
return json.loads(text)
|
||
|
|
except json.JSONDecodeError as e:
|
||
|
|
raise LLMParseError(f"大模型返回非合法 JSON: {e}") from e
|