feat(realism/llm): DB-backed LLMConfig, factory DB-first dispatch, Ollama HTTP mode

This commit is contained in:
2026-05-09 23:09:36 -04:00
parent f10201e885
commit 155ab59ee8
5 changed files with 385 additions and 15 deletions

View File

@@ -24,7 +24,14 @@ from decnet.realism.llm.base import LLMBackend
def get_llm(*, model: str | None = None, **kwargs: Any) -> LLMBackend:
"""Instantiate the LLM backend selected by environment.
"""Instantiate the LLM backend selected by DB config or environment.
Resolution order:
1. Process-level cached backend (populated by the DB config row via
:func:`decnet.realism.llm.config.apply`). Returned as-is when
*model* and *kwargs* are both absent — the common case.
2. Env-var path (``DECNET_REALISM_LLM`` / ``DECNET_REALISM_MODEL`` /
``DECNET_REALISM_TIMEOUT``) — legacy / default-install fallback.
*model* (when provided) overrides whatever the backend's own default
is — e.g. for :class:`OllamaBackend` that's ``llama3.1`` unless
@@ -32,6 +39,13 @@ def get_llm(*, model: str | None = None, **kwargs: Any) -> LLMBackend:
``decnet orchestrate --model gpt-oss`` without each backend having
to know about CLI flags.
"""
# Fast path: DB-configured cached backend.
if model is None and not kwargs:
from decnet.realism.llm.config import get_cached_backend
cached = get_cached_backend()
if cached is not None:
return cached
backend_key = os.environ.get("DECNET_REALISM_LLM", "ollama").lower()
if backend_key == "ollama":