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.

3.0 KB
Raw Blame
"""The research worker's ``custom_endpoint`` does not carry the stored key away.

``run_research_process`` overlays ``custom_endpoint`` onto the run's
snapshot (``overlay_runtime_settings``) and then calls
``get_llm(openai_endpoint_url=custom_endpoint, settings_snapshot=<that
snapshot>)``. The stored ``llm.openai_endpoint.api_key`` must be cleared in
that snapshot when ``custom_endpoint`` is on a different origin, and kept on
the same origin.

Network: ``get_llm`` and ``AdvancedSearchSystem`` are mocked, so nothing is
contacted.
"""

from contextlib import ExitStack
from unittest.mock import MagicMock, patch

from local_deep_research.web.services import research_service
from tests.web.services.helpers import (
    MODULE,
    _base_run_patches,
    _egress_and_search_patches,
    _get_raw_run_research_process,
)

RESEARCH_KEY = "sk-research-gateway-key"
STORED_URL = "https://llm-gateway.corp.example/v1"


def _snapshot_given_to_get_llm(custom_endpoint):
    system = MagicMock()
    system.analyze_topic.side_effect = RuntimeError("stop after get_llm")
    get_llm = MagicMock(return_value=MagicMock())

    patches = _base_run_patches()
    assert MODULE == research_service.__name__
    patches[f"{MODULE}.get_llm"] = get_llm
    patches[f"{MODULE}.AdvancedSearchSystem"] = MagicMock(return_value=system)

    snapshot = {
        "llm.provider": {"value": "openai_endpoint", "ui_element": "select"},
        "llm.model": {"value": "m", "ui_element": "text"},
        "search.tool": {"value": "searxng", "ui_element": "select"},
        "llm.openai_endpoint.url": {"value": STORED_URL, "ui_element": "text"},
        "llm.openai_endpoint.api_key": {
            "value": RESEARCH_KEY,
            "ui_element": "password",
        },
    }
    with ExitStack() as stack:
        for cm in _egress_and_search_patches():
            stack.enter_context(cm)
        for target, mock_obj in patches.items():
            stack.enter_context(patch(target, mock_obj))
        _get_raw_run_research_process()(
            1,
            "test query",
            "quick",
            username="user1",
            settings_snapshot=snapshot,
            search_engine="searxng",
            model_provider="openai_endpoint",
            model="m",
            custom_endpoint=custom_endpoint,
        )

    assert get_llm.called, "the worker never built its LLM"
    kwargs = get_llm.call_args.kwargs
    assert kwargs["openai_endpoint_url"] == custom_endpoint
    return kwargs["settings_snapshot"]


def _key(snapshot):
    raw = snapshot["llm.openai_endpoint.api_key"]
    return raw["value"] if isinstance(raw, dict) else raw


def test_custom_endpoint_on_another_origin_clears_the_stored_key():
    snapshot = _snapshot_given_to_get_llm("https://elsewhere.example/v1")
    assert _key(snapshot) == ""


def test_custom_endpoint_on_the_same_origin_keeps_the_stored_key():
    snapshot = _snapshot_given_to_get_llm(
        "https://LLM-Gateway.corp.example/v1/"
    )
    assert _key(snapshot) == RESEARCH_KEY