Construct the configured LLM provider, falling back to mock on missing credentials.
(config: Optional[ProviderConfig] = None)
| 373 | |
| 374 | |
| 375 | def build_llm_provider(config: Optional[ProviderConfig] = None) -> LLMProvider: |
| 376 | """Construct the configured LLM provider, falling back to mock on missing credentials.""" |
| 377 | |
| 378 | cfg = config or load_provider_config() |
| 379 | |
| 380 | if cfg.llm_provider == "openai": |
| 381 | api_key = os.environ.get("OPENAI_API_KEY", "") |
| 382 | if _is_placeholder(api_key): |
| 383 | return MockLLM() |
| 384 | base_url = os.environ.get("OPENAI_BASE_URL") or None |
| 385 | timeout = float(os.environ.get("OPENAI_API_TIMEOUT", "60") or 60) |
| 386 | return OpenAILLM(model=cfg.llm_model, api_key=api_key, base_url=base_url, timeout=timeout) |
| 387 | |
| 388 | if cfg.llm_provider == "anthropic": |
| 389 | api_key = os.environ.get("ANTHROPIC_API_KEY", "") |
| 390 | if _is_placeholder(api_key): |
| 391 | return MockLLM() |
| 392 | base_url = os.environ.get("ANTHROPIC_BASE_URL") or None |
| 393 | timeout = float(os.environ.get("ANTHROPIC_API_TIMEOUT", "60") or 60) |
| 394 | return AnthropicLLM(model=cfg.llm_model, api_key=api_key, base_url=base_url, timeout=timeout) |
| 395 | |
| 396 | if cfg.llm_provider == "ollama": |
| 397 | base_url = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434") |
| 398 | timeout = float(os.environ.get("OLLAMA_API_TIMEOUT", "120") or 120) |
| 399 | return OllamaLLM(model=cfg.llm_model, base_url=base_url, timeout=timeout) |
| 400 | |
| 401 | return MockLLM(model=cfg.llm_model) |
| 402 | |
| 403 | |
| 404 | __all__ = [ |