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.

9.4 KB
Raw Blame
"""Registry for custom LangChain LLMs.

This module provides a registry for registering and managing custom LangChain
LLMs that can be used with Local Deep Research.

Entries are keyed per user. Built-in providers (auto-discovered at import via
``discover_providers()``) and SDK/MCP/benchmark registrations that carry no
username live in a shared namespace and resolve for everyone. A registration
made with a ``username`` lives in that user's namespace and is resolved for
that user first, so one user's custom LLM can neither shadow a built-in
provider for anyone else nor leak into another user's registry listing.
"""

import threading
from typing import Callable, Dict, Optional, Union

from langchain.chat_models.base import BaseChatModel
from loguru import logger

# Sentinel key for the shared/global namespace (see module docstring).
_SHARED_NAMESPACE: Optional[str] = None

_LLMType = Union[BaseChatModel, Callable[..., BaseChatModel]]


class LLMRegistry:
    """Thread-safe, per-user registry for custom LangChain LLMs."""

    def __init__(self):
        # {namespace: {normalized_name: llm}}. ``namespace`` is a username
        # or ``_SHARED_NAMESPACE`` for shared/global entries (built-ins +
        # username-less registrations).
        self._llms: Dict[Optional[str], Dict[str, _LLMType]] = {}
        self._lock = threading.Lock()

    @staticmethod
    def _ns(username: Optional[str]) -> Optional[str]:
        """Normalize a username to a namespace key.

        A falsy username maps to the shared namespace, so legacy/global
        callers keep a single shared namespace and never crash for want of
        a username.
        """
        return username or _SHARED_NAMESPACE

    def register(
        self,
        name: str,
        llm: _LLMType,
        username: Optional[str] = None,
    ) -> None:
        """Register a custom LLM.

        Args:
            name: Unique name for the LLM (case-insensitive)
            llm: Either a BaseChatModel instance or a factory function that returns one
            username: Owner of the registration. When omitted the entry is
                stored in the shared namespace (visible to all callers).

        Raises:
            ValueError: ``llm`` is None. A stored ``None`` would silently
                miss in ``get()``'s ``found is not None`` check and fall
                through to the shared namespace, letting a misregistered
                entry resolve to an unrelated (possibly built-in) provider.
        """
        if llm is None:
            raise ValueError(f"Cannot register LLM '{name}': value is None")
        ns = self._ns(username)
        with self._lock:
            # Normalize name to lowercase for case-insensitive storage
            normalized_name = name.lower()
            bucket = self._llms.setdefault(ns, {})
            if normalized_name in bucket:
                logger.warning(f"Overwriting existing LLM: {name}")
            bucket[normalized_name] = llm
            logger.info(
                f"Registered custom LLM: {name} (normalized: {normalized_name})"
            )

    # Completes the CRUD API surface for the registry.
    # Used in tests to verify cleanup behavior.
    def unregister(self, name: str, username: Optional[str] = None) -> None:
        """Unregister a custom LLM.

        Args:
            name: Name of the LLM to unregister (case-insensitive)
            username: Owner namespace to remove from (None = shared)
        """
        ns = self._ns(username)
        with self._lock:
            normalized_name = name.lower()
            bucket = self._llms.get(ns)
            if bucket and normalized_name in bucket:
                del bucket[normalized_name]
                logger.info(f"Unregistered custom LLM: {name}")

    def get(
        self, name: str, username: Optional[str] = None
    ) -> Optional[_LLMType]:
        """Get a registered LLM.

        Resolution order: the caller's own namespace, then the shared
        namespace (which holds built-in providers). A user's registration
        never resolves for another user.

        Args:
            name: Name of the LLM to retrieve (case-insensitive)
            username: Requesting user (None resolves the shared namespace only)

        Returns:
            The LLM instance/factory or None if not found
        """
        ns = self._ns(username)
        with self._lock:
            normalized_name = name.lower()
            if ns is not _SHARED_NAMESPACE:
                found = self._llms.get(ns, {}).get(normalized_name)
                if found is not None:
                    return found
            return self._llms.get(_SHARED_NAMESPACE, {}).get(normalized_name)

    def is_registered(self, name: str, username: Optional[str] = None) -> bool:
        """Check if an LLM is registered and resolvable for the caller.

        Args:
            name: Name to check (case-insensitive)
            username: Requesting user (None resolves the shared namespace only)

        Returns:
            True if registered, False otherwise
        """
        return self.get(name, username=username) is not None

    # Used in test assertions to verify registry state;
    # part of public API for plugin authors.
    def list_registered(self, username: Optional[str] = None) -> list[str]:
        """Get list of LLM names visible to the caller.

        Returns the caller's own registrations plus shared ones (built-in
        providers); never another user's names.

        Order is DETERMINISTIC: the caller's own entries first (insertion
        order), then shared entries not shadowed by an own-namespace name.
        This mirrors ``get()``'s own-namespace-first resolution (a same-named
        own entry shadows the shared built-in, so it is listed once, under
        own) and replaces the previous ``set``-union, whose ``list(set(...))``
        order was nondeterministic run to run.

        Args:
            username: Requesting user (None lists the shared namespace only)

        Returns:
            List of registered LLM names
        """
        ns = self._ns(username)
        with self._lock:
            shared = self._llms.get(_SHARED_NAMESPACE, {})
            if ns is _SHARED_NAMESPACE:
                return list(shared.keys())
            own = self._llms.get(ns, {})
            return list(own.keys()) + [
                name for name in shared if name not in own
            ]

    # Used in 7+ test files' autouse fixtures for test isolation
    # (64+ tests depend on this to reset global state between runs).
    def clear(self, username: Optional[str] = None) -> None:
        """Clear registered LLMs.

        With no ``username`` every namespace is cleared — including the
        shared built-in providers — matching the historic reset behavior
        the test-isolation fixtures depend on. With a ``username`` only
        that user's namespace is cleared.
        """
        # TODO: hook clear(username=<deleted user>) into a user-deletion
        # flow so a removed user's registered LLMs are evicted from this
        # in-process registry. No user-deletion hook exists yet, so
        # eviction-on-deletion is out of scope here; this is the seam.
        with self._lock:
            if username is None:
                self._llms.clear()
                logger.info("Cleared all registered custom LLMs")
            else:
                self._llms.pop(self._ns(username), None)
                logger.info("Cleared registered custom LLMs for one user")


# Global registry instance
_llm_registry = LLMRegistry()


# Public API functions
def register_llm(
    name: str,
    llm: _LLMType,
    username: Optional[str] = None,
) -> None:
    """Register a custom LLM in the registry.

    Args:
        name: Unique name for the LLM
        llm: Either a BaseChatModel instance or a factory function
        username: Owner of the registration (None = shared namespace)
    """
    _llm_registry.register(name, llm, username=username)


def unregister_llm(name: str, username: Optional[str] = None) -> None:
    """Unregister a custom LLM from the registry.

    Args:
        name: Name of the LLM to unregister
        username: Owner namespace to remove from (None = shared)
    """
    _llm_registry.unregister(name, username=username)


def get_llm_from_registry(
    name: str,
    username: Optional[str] = None,
) -> Optional[_LLMType]:
    """Get a registered LLM from the registry.

    Args:
        name: Name of the LLM to retrieve
        username: Requesting user (None resolves the shared namespace only)

    Returns:
        The LLM instance/factory or None if not found
    """
    return _llm_registry.get(name, username=username)


def is_llm_registered(name: str, username: Optional[str] = None) -> bool:
    """Check if an LLM is registered in the registry.

    Args:
        name: Name to check
        username: Requesting user (None resolves the shared namespace only)

    Returns:
        True if registered, False otherwise
    """
    return _llm_registry.is_registered(name, username=username)


def list_registered_llms(username: Optional[str] = None) -> list[str]:
    """Get list of registered LLM names visible to the caller.

    Args:
        username: Requesting user (None lists the shared namespace only)

    Returns:
        List of registered LLM names
    """
    return _llm_registry.list_registered(username=username)


def clear_llm_registry(username: Optional[str] = None) -> None:
    """Clear registered LLMs (all namespaces when no username is given)."""
    _llm_registry.clear(username=username)