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.

10.2 KB
Raw Blame
"""A key passed in the same Python API call as ``openai_endpoint_url`` reaches it.

``quick_summary(provider="openai_endpoint", api_key=K,
openai_endpoint_url=X)`` names both the endpoint and its key. The
``custom_endpoint`` overlay must not treat that ``K`` as the stored key of a
different endpoint and withhold it. A key that only comes from
``settings_snapshot`` (or stored settings) is still withheld on a different
origin, and when both exist the call's ``api_key`` wins.

The langchain client class, the search engine and the search system are
patched; every endpoint is a loopback URL.
"""

from unittest.mock import MagicMock, patch

import pytest
from langchain_core.language_models.fake_chat_models import FakeListChatModel

from local_deep_research.api import research_functions
from local_deep_research.config.thread_settings import settings_context
from local_deep_research.llm.providers.base import OPTIONAL_API_KEY_PLACEHOLDER
from local_deep_research.settings.manager import SnapshotSettingsContext

OPENAI_BASE_CHAT = "local_deep_research.llm.providers.openai_base.ChatOpenAI"
VLLM_URL = "http://localhost:8000/v1"
SNAPSHOT_KEY = "sk-from-the-snapshot"
CALL_KEY = "sk-passed-with-the-url"


@pytest.fixture(autouse=True, scope="module")
def _ensure_providers_registered():
    from local_deep_research.llm.providers import discover_providers

    discover_providers(force_refresh=True)


def _snapshot(key=SNAPSHOT_KEY):
    # The stored endpoint is elsewhere: OpenRouter, the shipped default.
    return {
        "llm.provider": {"value": "openai_endpoint", "ui_element": "select"},
        "llm.model": {"value": "m", "ui_element": "text"},
        "llm.supports_max_tokens": {"value": True, "ui_element": "checkbox"},
        "llm.max_tokens": {"value": 4096, "ui_element": "number"},
        "rate_limiting.llm_enabled": {"value": False, "ui_element": "checkbox"},
        "search.tool": {"value": "searxng", "ui_element": "select"},
        "llm.openai_endpoint.url": {
            "value": "https://openrouter.ai/api/v1",
            "ui_element": "text",
        },
        "llm.openai_endpoint.api_key": {"value": key, "ui_element": "password"},
    }


def _client_kwargs(**init_kwargs):
    with (
        patch(
            OPENAI_BASE_CHAT, return_value=FakeListChatModel(responses=["ok"])
        ) as mock_chat,
        patch.object(research_functions, "get_search", MagicMock()),
        patch.object(research_functions, "AdvancedSearchSystem", MagicMock()),
    ):
        research_functions._init_search_system(
            model_name="m",
            provider="openai_endpoint",
            openai_endpoint_url=VLLM_URL,
            **init_kwargs,
        )
    return mock_chat.call_args.kwargs


class TestInitSearchSystemCallKey:
    @pytest.mark.parametrize("provider", ["openai_endpoint", None])
    def test_call_endpoint_is_shared_with_later_snapshot_consumers(
        self, monkeypatch, provider
    ):
        from local_deep_research.config.llm_config import get_llm

        monkeypatch.setenv(
            "LDR_LLM_OPENAI_ENDPOINT_URL", "http://localhost:9100/v1"
        )
        with (
            patch(
                OPENAI_BASE_CHAT,
                return_value=FakeListChatModel(responses=["ok"]),
            ) as mock_chat,
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(research_functions, "AdvancedSearchSystem") as system,
        ):
            research_functions._init_search_system(
                model_name="m",
                provider=provider,
                openai_endpoint_url=VLLM_URL,
                api_key=CALL_KEY,
                settings_snapshot=_snapshot(),
            )
            downstream_snapshot = system.call_args.kwargs["settings_snapshot"]
            get_llm(
                provider="openai_endpoint",
                model_name="m",
                settings_snapshot=downstream_snapshot,
            )

        assert [
            (call.kwargs["base_url"], call.kwargs["api_key"])
            for call in mock_chat.call_args_list
        ] == [(VLLM_URL, CALL_KEY), (VLLM_URL, CALL_KEY)]

    def test_key_passed_with_the_url_reaches_that_url(self):
        kwargs = _client_kwargs(
            settings_snapshot=_snapshot(key=CALL_KEY), api_key=CALL_KEY
        )
        assert kwargs["base_url"] == VLLM_URL
        assert kwargs["api_key"] == CALL_KEY

    def test_snapshot_only_key_is_still_withheld_on_another_origin(self):
        kwargs = _client_kwargs(settings_snapshot=_snapshot())
        assert kwargs["base_url"] == VLLM_URL
        assert kwargs["api_key"] == OPTIONAL_API_KEY_PLACEHOLDER

    def test_call_key_wins_over_a_snapshot_key(self):
        kwargs = _client_kwargs(settings_snapshot=_snapshot(), api_key=CALL_KEY)
        assert kwargs["api_key"] == CALL_KEY

    def test_call_key_is_not_bound_for_another_provider(self):
        """api_key belongs to the named provider; for a non-endpoint
        provider it must not land in llm.openai_endpoint.api_key."""
        snapshot = _snapshot()
        snapshot["llm.provider"] = {"value": "ollama", "ui_element": "select"}
        captured = {}

        def fake_get_llm(**kwargs):
            captured.update(kwargs)
            return MagicMock()

        with (
            patch.object(research_functions, "get_llm", fake_get_llm),
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions._init_search_system(
                model_name="m",
                openai_endpoint_url=VLLM_URL,
                api_key=CALL_KEY,
                settings_snapshot=snapshot,
            )
        assert captured["openai_endpoint_api_key"] is None
        bound = captured["settings_snapshot"]["llm.openai_endpoint.api_key"]
        assert bound != CALL_KEY


class TestInitSearchSystemCallKeyLateProvider:
    """The call key must survive when the provider resolves only later.

    Two reachable shapes used the call URL while dropping ``api_key``:
    ``settings_snapshot=None`` (the provider resolves from the thread
    settings context or get_llm's own defaults) and a snapshot without
    ``llm.provider`` (the drop left the overlay writing the call URL into
    the snapshot keyless).
    """

    def test_call_key_is_forwarded_when_the_provider_is_unresolved(self):
        captured = {}

        def fake_get_llm(**kwargs):
            captured.update(kwargs)
            return MagicMock()

        with (
            patch.object(research_functions, "get_llm", fake_get_llm),
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions._init_search_system(
                model_name="m",
                openai_endpoint_url=VLLM_URL,
                api_key=CALL_KEY,
                settings_snapshot=None,
            )
        assert captured["openai_endpoint_api_key"] == CALL_KEY

    def test_call_key_reaches_the_url_without_a_settings_snapshot(self):
        ctx = SnapshotSettingsContext(
            {
                "llm.provider": {
                    "value": "openai_endpoint",
                    "ui_element": "select",
                }
            }
        )
        with (
            settings_context(ctx),
            patch(
                OPENAI_BASE_CHAT,
                return_value=FakeListChatModel(responses=["ok"]),
            ) as mock_chat,
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions._init_search_system(
                model_name="m",
                openai_endpoint_url=VLLM_URL,
                api_key=CALL_KEY,
                settings_snapshot=None,
            )
        assert [
            (call.kwargs["base_url"], call.kwargs["api_key"])
            for call in mock_chat.call_args_list
        ] == [(VLLM_URL, CALL_KEY)]

    def test_call_key_is_bound_when_the_snapshot_lacks_a_provider(self):
        snapshot = _snapshot()
        del snapshot["llm.provider"]
        captured = {}

        def fake_get_llm(**kwargs):
            captured.update(kwargs)
            return MagicMock()

        with (
            patch.object(research_functions, "get_llm", fake_get_llm),
            patch.object(research_functions, "get_search", MagicMock()),
            patch.object(
                research_functions, "AdvancedSearchSystem", MagicMock()
            ),
        ):
            research_functions._init_search_system(
                model_name="m",
                openai_endpoint_url=VLLM_URL,
                api_key=CALL_KEY,
                settings_snapshot=snapshot,
            )
        assert captured["openai_endpoint_api_key"] == CALL_KEY
        bound = captured["settings_snapshot"]
        assert bound["llm.openai_endpoint.url"] == VLLM_URL
        assert bound["llm.openai_endpoint.api_key"] == CALL_KEY


class _Stop(Exception):
    pass


@pytest.mark.parametrize("entry", ["quick_summary", "generate_report"])
def test_public_entry_points_forward_the_call_key(entry):
    seen = {}

    def fake_init(**kwargs):
        seen.update(kwargs)
        raise _Stop

    with patch.object(research_functions, "_init_search_system", fake_init):
        with pytest.raises(_Stop):
            getattr(research_functions, entry)(
                "q",
                api_key=CALL_KEY,
                openai_endpoint_url=VLLM_URL,
                settings_snapshot=_snapshot(),
            )
    assert seen["api_key"] == CALL_KEY
    assert seen["openai_endpoint_url"] == VLLM_URL


def test_detailed_research_forwards_the_call_key():
    seen = {}

    def fake_init(**kwargs):
        seen.update(kwargs)
        raise _Stop

    with patch.object(research_functions, "_init_search_system", fake_init):
        with pytest.raises(_Stop):
            research_functions.detailed_research(
                "q",
                api_key=CALL_KEY,
                openai_endpoint_url=VLLM_URL,
                settings_snapshot=_snapshot(),
            )
    assert seen["api_key"] == CALL_KEY