19 lines
457 B
Python
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))
|
|
|