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.

2.7 KB
Raw Blame
"""Parallel search execution helper.

Provides :func:`run_parallel_searches`, a small helper that runs a sequence
of search queries concurrently using :class:`~concurrent.futures.ThreadPoolExecutor`
while preserving the research context expected by worker threads.

The helper is intentionally generic:

* ``search_fn`` is a callable accepting the query string and returning
  whatever per-query payload the caller needs (a list of results, a dict
  with metadata, etc.).  Callers wrap it with context-preserving
  decorators (e.g. :func:`preserve_research_context`) before passing it
  in; this helper stays focused on the concurrency concern.

Returns a list of ``(query, payload)`` tuples in completion order.
Callers that need a question-keyed dict build it from this list; callers
that only need a flat list of results flatten it directly.
"""

from __future__ import annotations

import concurrent.futures
from typing import Callable, List, Optional, Tuple, TypeVar

from loguru import logger

T = TypeVar("T")


def run_parallel_searches(
    queries: List[str],
    search_fn: Callable[[str], T],
    max_workers: Optional[int] = None,
) -> List[Tuple[str, T]]:
    """Run ``search_fn`` for each query in parallel.

    Args:
        queries: Queries to search.  If empty, returns an empty list
            immediately (and logs a warning).
        search_fn: Callable invoked as ``search_fn(query)`` inside a worker
            thread.  Callers are responsible for wrapping it with any
            context-preserving decorators (e.g.
            :func:`preserve_research_context`) before passing it in, and
            for their own error handling (the callable should never raise
            — return an empty payload on failure instead, matching the
            pre-existing contract of the strategies this was extracted
            from).
        max_workers: Size of the thread pool.  Defaults to ``len(queries)``
            when ``None``, matching the historical behavior of the
            source-based, focused-iteration, and progressive strategies.

    Returns:
        A list of ``(query, payload)`` tuples in completion order.  Each
        ``payload`` is whatever ``search_fn`` returned for that query.
    """
    if not queries:
        logger.warning("No queries provided for parallel search")
        return []

    if max_workers is None:
        max_workers = len(queries)

    def _worker(query: str) -> Tuple[str, T]:
        return (query, search_fn(query))

    with concurrent.futures.ThreadPoolExecutor(
        max_workers=max_workers
    ) as executor:
        futures = [executor.submit(_worker, q) for q in queries]
        return [
            future.result()
            for future in concurrent.futures.as_completed(futures)
        ]