Files
ai-daily-report/ai_daily_report/llm.py
2026-06-04 15:21:56 +08:00

19 lines
457 B
Python

from __future__ import annotations
import json
import re
from typing import Any, Callable
LlmCall = Callable[[str], str]
def parse_json_object(text: str) -> dict[str, Any]:
text = re.sub(r"^```(?:json)?\s*\n?", "", text.strip())
text = re.sub(r"\n?```\s*$", "", text)
match = re.search(r"\{.*\}\s*$", text, re.S)
if not match:
raise ValueError("LLM output does not contain a JSON object")
return json.loads(match.group(0))