Add build_llm factory for the LLM port

Builds providers and router from configuration; the caller passes env,
a missing key fails only on call.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
torbenandClaude Sonnet 5.5 committed 2026-12-04 22:06:58 +01:00
1 parent d759697aeb
commit adf8a9b290
2 files changed
+105

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
"""Builds the LLM port from configuration."""
from collections.abc import Mapping
from pathlib import Path
from chemenu.adapters.llm.anthropic_api import AnthropicProvider
from chemenu.adapters.llm.config import AnthropicConfig, LlmConfig
from chemenu.adapters.llm.provider import Provider
from chemenu.adapters.llm.recorded import RecordedProvider
from chemenu.adapters.llm.router import ProfileRouter
from chemenu.ports.llm import LlmPort
def build_llm(config: LlmConfig, env: Mapping[str, str]) -> LlmPort:
"""Build the router with one provider per configured entry.
Never reads `os.environ`; the caller passes `env`. A missing API key does not fail
here, only the call (`MissingApiKeyError`). Recording directories are relative to
the working directory.
"""
providers: dict[str, Provider] = {}
for name, cfg in config.providers.items():
if isinstance(cfg, AnthropicConfig):
providers[name] = AnthropicProvider(env.get(cfg.api_key_env))
else:
providers[name] = RecordedProvider(Path(cfg.directory))
return ProfileRouter(config, providers)