- LLMProvider: complete(messages, schema) -> LLMResult (текст, разобранный JSON, токены) - openai_compat: /chat/completions, structured output через response_format json_schema, JSON из ```-обёртки тоже разбирается; ретраи на 429/5xx и сетевые ошибки, 4xx — сразу ошибка - mock: детерминированные правила для тестов и демо без сети - LLMSettings (LLM_PROVIDER/BASE_URL/MODEL/API_KEY…), CLI python -m app.llm.cli Refs #17 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
19 lines
520 B
Python
19 lines
520 B
Python
import json
|
|
import re
|
|
from typing import Any
|
|
|
|
_FENCE = re.compile(r"^```(?:json)?\s*(.*?)\s*```$", re.DOTALL)
|
|
|
|
|
|
def parse_json_object(text: str) -> dict[str, Any] | None:
|
|
"""Best effort: models sometimes wrap JSON in ``` fences even when asked not to."""
|
|
candidate = text.strip()
|
|
fenced = _FENCE.match(candidate)
|
|
if fenced:
|
|
candidate = fenced.group(1)
|
|
try:
|
|
value = json.loads(candidate)
|
|
except ValueError:
|
|
return None
|
|
return value if isinstance(value, dict) else None
|