Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

8.4 KB
Raw Blame
"""Base class for LLM providers."""

from ...security.egress.classification import Exposure
from ...settings.exceptions import SettingsValueError

# Single placeholder for ChatOpenAI(api_key=...) when no real key is
# configured (unauthenticated local LM Studio, llama.cpp, etc.). Subclasses
# MUST NOT override this constant; per-provider drift here was the reason
# we previously had three different placeholder strings.
OPTIONAL_API_KEY_PLACEHOLDER = "not-required"  # noqa: S105


def normalize_provider(provider):
    """Normalize provider name to lowercase canonical form.

    All provider comparisons in route/service code should use
    this function to ensure consistent casing. Surrounding whitespace and
    surrounding quote characters are stripped too (a hand-edited,
    env-supplied or settings round-tripped ``" qwen "`` / ``'"ollama"'``),
    so route checks resolve a provider exactly as ``get_llm`` does. A
    value that is empty after stripping (``""``, ``"   "``, ``'""'``)
    normalises to None like any other empty value.

    Only ``str`` input is stripped. Any other truthy value keeps the
    pre-strip contract exactly: it is duck-typed through ``.lower()``, so a
    list/int still raises ``AttributeError`` (callers and the contract
    tests rely on that), and an object that only implements ``lower()``
    passes through as before instead of failing on a missing ``strip()``.
    """
    if not provider:
        return None
    if isinstance(provider, str):
        return provider.strip().strip("\"'").strip().lower() or None
    return provider.lower()


class BaseLLMProvider:
    """Base class for all LLM providers.

    Defines the minimum interface that all providers must satisfy.
    Subclasses should override the methods below as needed.

    **Class attribute contract:**

    - ``api_key_setting`` — settings key for the provider's API key
      (e.g. ``"llm.openai.api_key"``), or ``None`` for providers that have
      no API key concept at all.
    - ``api_key_optional`` — when ``True``, a missing/whitespace-only key
      returns ``None`` from ``resolve_api_key()`` instead of raising.
      Construction-time callers can then substitute
      ``OPTIONAL_API_KEY_PLACEHOLDER`` (typical for local providers like
      LM Studio that may or may not have auth enabled).
    - ``provider_name`` — display name used in error messages and logs.
      Subclasses MUST override this away from the ``"unknown"`` default
      or auto-discovery will skip them.

    **Helper methods (use these instead of reading settings directly):**

    - ``resolve_api_key(snapshot)`` → ``str | None``. Returns the
      stripped key, ``None`` for optional providers when missing, or
      raises ``ValueError`` for required providers when missing.
    - ``resolve_api_key_or_placeholder(snapshot)`` → ``str``. Same as
      above but substitutes the placeholder when optional+missing — use
      this when constructing ``ChatOpenAI(api_key=...)``.
    - ``build_bearer_header(snapshot)`` → ``dict``. Returns
      ``{"Authorization": "Bearer <key>"}`` or ``{}``. Use this when
      sending Authorization headers to a provider's REST API.
    - ``has_api_key(snapshot)`` → ``bool``. Use in ``is_available()``
      overrides to fail-closed when a required key is missing.

    **`create_llm()` contract:** subclass implementations return a *bare*
    LangChain ``BaseChatModel``. Wrapping (rate limiting, token counting,
    think-tag stripping) is applied by ``llm_config.get_llm()`` after the
    provider class returns. Do NOT wrap inside ``create_llm()``.
    """

    # Settings key for this provider's API key. None means the provider
    # has no API key concept at all.
    api_key_setting: str | None = None

    # Display name used in error messages and logs. Subclasses set this.
    provider_name: str = "unknown"

    # When True, a missing/whitespace-only key returns None (callers may
    # substitute OPTIONAL_API_KEY_PLACEHOLDER) instead of raising.
    api_key_optional: bool = False

    # Egress exposure (ADR-0007): declares whether this is an exposing
    # inference sink (cloud = exposing, local = contained). Declarative default
    # only — the egress resolver classifies the provider's ACTUAL endpoint at
    # run time (run_classification.llm_label via evaluate_llm_endpoint), so a
    # URL-configurable provider is refined then; this attribute documents intent
    # and is asserted consistent by test.
    egress_exposure = Exposure.EXPOSING

    @classmethod
    def create_llm(cls, model_name=None, temperature=0.7, **kwargs):
        """Create and return a LangChain chat model instance.

        Subclasses MUST override this method. The returned LLM is BARE —
        wrapping (rate limiting, token counting, think-tag stripping) is
        applied by ``llm_config.get_llm()`` after this returns.

        Args:
            model_name: Name of the model to use
            temperature: Model temperature (0.0-1.0)
            **kwargs: Additional arguments including settings_snapshot

        Returns:
            A configured BaseChatModel instance

        Raises:
            NotImplementedError: If not overridden by subclass
        """
        raise NotImplementedError(f"{cls.__name__} must implement create_llm()")

    @classmethod
    def is_available(cls, settings_snapshot=None):
        """Check if this provider is available.

        Returns False by default (fail-closed). Subclasses MUST override
        this method to implement their own availability logic.
        """
        return False

    @classmethod
    def requires_auth_for_models(cls):
        """Whether auth is needed to list models.

        Returns True by default. Override in subclasses that allow
        unauthenticated model listing (e.g., local providers).
        """
        return True

    @classmethod
    def resolve_api_key(cls, settings_snapshot=None) -> str | None:
        """Read this provider's API key from settings, normalized.

        Returns:
            The stripped key if non-empty; None if optional and missing
            or whitespace-only, or if api_key_setting is None.
        Raises:
            ValueError if required (api_key_optional=False) and missing.
        """
        from ...config.thread_settings import get_setting_from_snapshot

        if not cls.api_key_setting:
            return None
        raw = get_setting_from_snapshot(
            cls.api_key_setting, "", settings_snapshot=settings_snapshot
        )
        key = str(raw or "").strip()
        if key:
            return key
        if cls.api_key_optional:
            return None
        raise ValueError(
            f"{cls.provider_name} API key not configured. "
            f"Please set {cls.api_key_setting} in settings."
        )

    @classmethod
    def resolve_api_key_or_placeholder(cls, settings_snapshot=None) -> str:
        """For ``ChatOpenAI(api_key=...)``-style construction.

        Always returns a string. Real key when set; otherwise the unified
        ``OPTIONAL_API_KEY_PLACEHOLDER`` so the langchain client doesn't
        reject the construction call.
        """
        key = cls.resolve_api_key(settings_snapshot)
        return key if key else OPTIONAL_API_KEY_PLACEHOLDER

    @classmethod
    def build_bearer_header(cls, settings_snapshot=None) -> dict[str, str]:
        """Build an ``Authorization: Bearer`` header from the resolved key.

        Returns an empty dict when no real key is configured (so callers
        can spread it conditionally). A missing required key is treated as
        "no header" here too; its construction-time error happens elsewhere.
        Invalid configured values must propagate rather than remove auth.
        """
        try:
            key = cls.resolve_api_key(settings_snapshot)
        except SettingsValueError:
            raise
        except ValueError:
            return {}
        return {"Authorization": f"Bearer {key}"} if key else {}

    @classmethod
    def has_api_key(cls, settings_snapshot=None) -> bool:
        """True if a real key is configured.

        Returns False for missing/whitespace keys (required providers raise
        ValueError; we swallow it). Also returns False on broader settings
        infrastructure errors so callers like ``is_available()`` fail closed.
        Invalid configured values propagate instead of appearing absent.
        """
        try:
            return cls.resolve_api_key(settings_snapshot) is not None
        except SettingsValueError:
            raise
        except Exception:
            return False