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.3 KB
Raw Blame
"""Run the real ASGI app with only model and search egress stubbed for UI CI.

The browser test creates ``release/<research_id>`` after it has subscribed or
enabled polling.  The model writes ``entered/<research_id>`` and waits, so the
worker cannot finish before the browser has observed the transport state.
This module is executed only by the release browser shard, never by ldr-web.
"""

from __future__ import annotations

import os
import time
import uuid
from pathlib import Path

import uvicorn
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration, ChatResult

from local_deep_research.llm import register_llm
from local_deep_research.utilities.thread_context import get_search_context
from local_deep_research.web.fastapi_app import app
from local_deep_research.web_search_engines.engines.search_engine_wikipedia import (
    WikipediaSearchEngine,
)


SMOKE_DIR = Path(os.environ["LDR_RESEARCH_SMOKE_DIR"])
ENTERED = SMOKE_DIR / "entered"
RELEASE = SMOKE_DIR / "release"
ANSWER = "RELEASEBROWSERSYNTHESISMARKER"
SOURCE_URL = "https://research-smoke.invalid/source"


class _GatedChatModel(BaseChatModel):
    @property
    def _llm_type(self) -> str:
        return "release-browser-stub"

    def _generate(self, messages, stop=None, run_manager=None, **kwargs):
        context = get_search_context() or {}
        research_id = str(uuid.UUID(str(context["research_id"])))
        (ENTERED / research_id).touch()

        deadline = time.monotonic() + 90
        while not (RELEASE / research_id).exists():
            if time.monotonic() >= deadline:
                raise TimeoutError(
                    f"browser did not release research {research_id}"
                )
            time.sleep(0.05)  # allow: unmarked-sleep -- test-only gate

        return ChatResult(
            generations=[ChatGeneration(message=AIMessage(content=ANSWER))]
        )


def _make_llm(model_name=None, temperature=None, settings_snapshot=None):
    return _GatedChatModel()


def _previews(self, query):
    return [
        {
            "id": "release-browser-source",
            "title": "Release Browser Source",
            "link": SOURCE_URL,
            "snippet": "RELEASEBROWSERSEARCHMARKER",
        }
    ]


def _full_content(self, relevant_items):
    return [
        dict(
            item,
            content="RELEASEBROWSERSEARCHMARKER body",
            full_content="RELEASEBROWSERSEARCHMARKER body",
        )
        for item in relevant_items
    ]


def main() -> None:
    ENTERED.mkdir(parents=True, exist_ok=True)
    RELEASE.mkdir(parents=True, exist_ok=True)
    # fastapi_app has already imported llm_config and discovered built-ins.
    # Override only the test server's Ollama boundary; the browser still
    # selects the normal Ollama provider and submits through production code.
    register_llm("ollama", _make_llm)
    WikipediaSearchEngine._get_previews = _previews
    WikipediaSearchEngine._get_full_content = _full_content
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=5000,
        workers=1,
        server_header=False,
        access_log=False,
        # Mirrors MAX_INBOUND_MESSAGE_BYTES in web/services/socketio_asgi.py.
        ws_max_size=65536,
    )


if __name__ == "__main__":
    main()