~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Cancellation of in-flight collection-indexing embeddings.
``index_documents_parallel`` polls ``is_cancelled`` between documents, but
a single document's ``embed_documents`` call used to run to completion no
matter when Stop arrived. ``_embed_texts_cancellable`` embeds in bounded
batches with a cancel check between them, so the uncancellable tail is
one batch instead of the whole document.
"""
from types import SimpleNamespace
def _stub_service(calls):
from local_deep_research.research_library.services.library_rag_service import (
LibraryRAGService,
)
class _FakeEmbeddings:
def embed_documents(self, texts):
calls.append(list(texts))
return [[float(len(t))] for t in texts]
stub = SimpleNamespace(
embedding_manager=SimpleNamespace(embeddings=_FakeEmbeddings())
)
return LibraryRAGService, stub
class TestNoScopeKeepsSingleCall:
def test_large_doc_without_scope_is_one_call(self):
"""Background/sequential paths (no cancel scope) behave as before."""
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(70)]
vectors = LibraryRAGService._embed_texts_cancellable(stub, texts, None)
assert vectors is not None and len(vectors) == 70
assert calls == [texts]
# Order preserved.
assert [v[0] for v in vectors] == [float(len(t)) for t in texts]
class TestBatchedScope:
def test_batches_concatenate_in_order(self):
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(70)]
vectors = LibraryRAGService._embed_texts_cancellable(
stub, texts, lambda: False
)
assert vectors is not None and len(vectors) == 70
assert [len(batch) for batch in calls] == [32, 32, 6]
assert [v[0] for v in vectors] == [float(len(t)) for t in texts]
def test_small_doc_is_one_call_with_scope(self):
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(10)]
vectors = LibraryRAGService._embed_texts_cancellable(
stub, texts, lambda: False
)
assert vectors is not None and len(vectors) == 10
assert calls == [texts]
class TestCancelAbortsEmbedding:
def test_pre_cancelled_makes_no_calls(self):
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(70)]
assert (
LibraryRAGService._embed_texts_cancellable(
stub, texts, lambda: True
)
is None
)
assert calls == []
def test_cancel_mid_batches_aborts_after_current_batch(self):
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(70)]
# Cancel lands after the first batch finished.
state = {"batches": 0}
def _is_cancelled():
return state["batches"] >= 1
orig = stub.embedding_manager.embeddings.embed_documents
def _counting(texts_batch):
try:
return orig(texts_batch)
finally:
state["batches"] += 1
stub.embedding_manager.embeddings.embed_documents = _counting
try:
assert (
LibraryRAGService._embed_texts_cancellable(
stub, texts, _is_cancelled
)
is None
)
finally:
stub.embedding_manager.embeddings.embed_documents = orig
assert len(calls) == 1
assert len(calls[0]) == 32
def test_cancel_after_last_batch_discards_vectors(self):
"""A cancel racing the final batch must not persist vectors."""
LibraryRAGService, stub = _stub_service(calls := [])
texts = [f"chunk-{i}" for i in range(70)]
state = {"calls": 0}
def _is_cancelled():
# False between batches, True at the trailing check.
return state["calls"] >= 3
orig = stub.embedding_manager.embeddings.embed_documents
def _counting(texts_batch):
try:
return orig(texts_batch)
finally:
state["calls"] += 1
stub.embedding_manager.embeddings.embed_documents = _counting
try:
assert (
LibraryRAGService._embed_texts_cancellable(
stub, texts, _is_cancelled
)
is None
)
finally:
stub.embedding_manager.embeddings.embed_documents = orig
assert len(calls) == 3