44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""LLM clients — ONE place to build them.
|
|
|
|
Two backends, by capability:
|
|
- **Gateway** (OpenAI-compatible, e.g. opencode/OpenRouter) → chat for the curator.
|
|
- **Embeddings** → direct OpenAI or an OpenAI-compatible embeddings API via OPENAI_API_BASE.
|
|
|
|
Both keys come from config/.env (git-ignored). Each builder returns `None` when unconfigured so the
|
|
engine degrades gracefully (white-box sift + templated cards — the safety net).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from functools import lru_cache
|
|
|
|
from app.config import get_settings
|
|
|
|
|
|
@lru_cache
|
|
def gateway_client():
|
|
"""OpenAI-compatible client for the opencode.ai/zen gateway — chat (Opus). None if unconfigured."""
|
|
s = get_settings()
|
|
if not (s.DSPY_API_BASE and s.DSPY_API_KEY):
|
|
return None
|
|
from openai import OpenAI
|
|
return OpenAI(base_url=s.DSPY_API_BASE, api_key=s.DSPY_API_KEY, timeout=60, max_retries=2)
|
|
|
|
|
|
@lru_cache
|
|
def embed_client():
|
|
"""OpenAI-compatible client for embeddings. None if no key."""
|
|
s = get_settings()
|
|
if not s.OPENAI_API_KEY:
|
|
return None
|
|
from openai import OpenAI
|
|
kwargs = {"api_key": s.OPENAI_API_KEY, "timeout": 30, "max_retries": 2}
|
|
if s.OPENAI_API_BASE:
|
|
kwargs["base_url"] = s.OPENAI_API_BASE
|
|
return OpenAI(**kwargs)
|
|
|
|
|
|
def curate_enabled() -> bool:
|
|
"""Is the Opus curator usable? (flag on + gateway key present)."""
|
|
s = get_settings()
|
|
return bool(s.ENGINE_LLM_ENABLED and s.DSPY_API_BASE and s.DSPY_API_KEY)
|