[MLsys2026 Best Paper]: https://arxiv.org/abs/2506.08276. RAG on Everything with LEANN. Enjoy 97% storage savings while running a fast, accurate, and 100% private RAG application on your personal device.
Python aifaissgpt-osslangchainllama-indexllmlocalstorageoffline-firstollamaprivacypythonragretrieval-augmented-generationvector-databasevector-searchvectors
fix(core): drop the build template from query embeddings on the fallback path (#442)
Serhii Zghamadb2609817e7b
diff --git a/packages/leann-core/src/leann/searcher_base.py b/packages/leann-core/src/leann/searcher_base.pyindex 0d22d40..57c059f 100644--- a/packages/leann-core/src/leann/searcher_base.py+++ b/packages/leann-core/src/leann/searcher_base.py@@ -213,6 +213,15 @@ class BaseSearcher(LeannBackendSearcherInterface, ABC):self.zmq_connection = ReusableZMQConnection()self._zmq_port: Optional[int] = None+ def _search_provider_options(self) -> dict[str, Any]:+ # compute_query_embedding applies the query template itself; a build template+ # left in here would be prepended to the query by the provider as well.+ return {+ k: v+ for k, v in self.embedding_options.items()+ if k not in ("build_prompt_template", "query_prompt_template", "prompt_template")+ }+def _load_meta(self) -> dict[str, Any]:"""Loads the metadata file associated with the index."""# This is the corrected logic for finding the meta file.@@ -239,14 +248,7 @@ class BaseSearcher(LeannBackendSearcherInterface, ABC):or "mips")- # Filter out ALL prompt templates from provider_options during search- # Templates are applied in compute_query_embedding (line 109-110) BEFORE server call- # The server should never apply templates during search to avoid double-templating- search_provider_options = {- k: v- for k, v in self.embedding_options.items()- if k not in ("build_prompt_template", "query_prompt_template", "prompt_template")- }+ search_provider_options = self._search_provider_options()server_started, actual_port = self.embedding_server_manager.start_server(port=port if port is not None else 5557,@@ -335,7 +337,7 @@ class BaseSearcher(LeannBackendSearcherInterface, ABC):[query],self.embedding_model,embedding_mode,- provider_options=self.embedding_options,+ provider_options=self._search_provider_options(),)# Cache the result (use original query before template)diff --git a/tests/test_prompt_template_persistence.py b/tests/test_prompt_template_persistence.pyindex 6bf3761..b285f4b 100644--- a/tests/test_prompt_template_persistence.py+++ b/tests/test_prompt_template_persistence.py@@ -896,3 +896,70 @@ class TestQueryTemplateApplicationInComputeEmbedding:# Empty string is falsy, so no template should be appliedassert captured_queries[0] == "vector database"++ @pytest.mark.parametrize(+ "embedding_options, query_template",+ [+ (+ {+ "build_prompt_template": "title: none | text: ",+ "query_prompt_template": "task: search result | query: ",+ },+ "task: search result | query: ",+ ),+ ({"prompt_template": "search_query: "}, "search_query: "),+ ],+ )+ def test_fallback_path_does_not_add_build_template(+ self, temp_index_with_template, embedding_options, query_template+ ):+ """The provider must see only the query template, not the build template on top."""+ from leann.searcher_base import BaseSearcher++ class TestSearcher(BaseSearcher):+ def search(+ self,+ query,+ top_k,+ complexity=64,+ beam_width=1,+ prune_ratio=0.0,+ recompute_embeddings=False,+ pruning_strategy="global",+ zmq_port=None,+ **kwargs,+ ):+ return {"labels": [], "distances": []}++ searcher = object.__new__(TestSearcher)+ searcher.index_path = Path(temp_index_with_template)+ searcher.index_dir = searcher.index_path.parent+ with open(searcher.index_dir / f"{searcher.index_path.name}.meta.json") as f:+ searcher.meta = json.load(f)+ searcher.embedding_model = searcher.meta["embedding_model"]+ searcher.embedding_mode = searcher.meta["embedding_mode"]+ searcher.embedding_options = {"api_key": "test-key", **embedding_options}+ searcher.meta["embedding_options"] = searcher.embedding_options+ searcher.query_cache = Mock()+ searcher.query_cache.get.return_value = None++ sent_inputs = []++ def create(model, input, **kwargs):+ sent_inputs.extend(input)+ return Mock(data=[Mock(embedding=[0.1] * 768) for _ in input])++ client = Mock()+ client.embeddings.create.side_effect = create++ with (+ patch("openai.OpenAI", return_value=client),+ patch("leann.embedding_compute.get_model_token_limit", return_value=8192),+ ):+ searcher.compute_query_embedding(+ query="vector database",+ use_server_if_available=False,+ query_template=query_template,+ )++ assert sent_inputs == [f"{query_template}vector database"]