Files
chemenu-ng/src/chemenu/adapters/llm/factory.py
T
torbenandClaude Sonnet 5.5 88b8361ef3 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>
2026-12-04 06:56:48 +01:00

28 lines
1.1 KiB
Python

"""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)