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.

feat(search): cap local RAG results (default 20) + opt-in LLM relevance filter (#4765)

LearningCircuit13c211b6eea8
diff --git a/src/local_deep_research/defaults/default_settings.json b/src/local_deep_research/defaults/default_settings.json
index 03a2a441fc..8f9409f32a 100644
--- a/src/local_deep_research/defaults/default_settings.json
+++ b/src/local_deep_research/defaults/default_settings.json
@@ -1310,6 +1310,34 @@
"value": true,
"visible": true
},
+ "search.rag.max_results": {
+ "category": "search_parameters",
+ "description": "Maximum results (embedding chunks) the local document / collection (RAG) search returns per query. Capped independently of 'Max Results' because vector search returns many near-duplicate chunks that can flood the agent's context across research iterations.",
+ "editable": true,
+ "max_value": 100,
+ "min_value": 1,
+ "name": "Local RAG Max Results",
+ "options": null,
+ "step": 1,
+ "type": "SEARCH",
+ "ui_element": "number",
+ "value": 20,
+ "visible": true
+ },
+ "search.rag.enable_relevance_filter": {
+ "category": "search_parameters",
+ "description": "Apply an LLM relevance filter to local document / collection (RAG) results. Embeddings already rank by similarity, so this is optional; enabling it prunes semantically-near-but-irrelevant chunks at the cost of one extra LLM call per search.",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Local RAG Relevance Filter",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": true,
+ "visible": true
+ },
"search.journal_reputation.threshold": {
"category": "journal_quality_filter_parameters",
"description": "Minimum journal quality score required for academic results. Sources below this threshold are excluded when the filter is enabled. Scores emitted: 1 predatory, 4 default/unknown, 5-6 moderate, 7-8 strong, 10 elite (values 2, 3, 9 are reserved but never assigned). Default 2 = drop only predatory; raise to 5 to also drop default/unknown venues; raise to 7+ to keep only strong/elite journals.",
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_library.py b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
index 3a16a3d1c9..d1b2ee90ce 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_library.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_library.py
@@ -14,6 +14,7 @@ from ...research_library.services.library_rag_service import LibraryRAGService
from ...research_library.services.library_service import LibraryService
from ...config.thread_settings import get_setting_from_snapshot
from ...utilities.llm_utils import get_server_url
+from ...utilities.type_utils import to_bool
from ...database.models.library import RAGIndex, Document
from ...research_library.services.pdf_storage_manager import PDFStorageManager
from ...database.session_context import get_user_db_session
@@ -98,6 +99,45 @@ class LibraryRAGSearchEngine(BaseSearchEngine):
# Extract server URL from settings snapshot for link generation
self.server_url = get_server_url(settings_snapshot)
+ # Local RAG retrieval is vector-similarity ranked and can return many
+ # near-duplicate chunks per query — enough to flood an agent's context
+ # across research iterations. Apply a RAG-specific cap (default 20),
+ # independent of the global ``search.max_results`` (which targets web
+ # engines). This OVERRIDES the inherited max_results for local search.
+ # Default to the inherited max_results so an ABSENT setting preserves
+ # the caller's value (programmatic callers, tests); real web/CLI runs
+ # carry the shipped default of 20 via default_settings.json, so they ARE
+ # capped. A malformed value also falls back to no change.
+ rag_max_results = get_setting_from_snapshot(
+ "search.rag.max_results",
+ default=self.max_results,
+ settings_snapshot=settings_snapshot,
+ )
+ try:
+ rag_max_results = int(rag_max_results)
+ except (TypeError, ValueError):
+ rag_max_results = self.max_results
+ if rag_max_results > 0:
+ self.max_results = rag_max_results
+
+ # Embeddings already rank by cosine similarity, so the LLM relevance
+ # filter is OPT-IN here (unlike keyword engines that hardcode
+ # needs_llm_relevance_filter=True). Default on: prune
+ # semantically-near-but-irrelevant chunks. The factory only ever SETS
+ # enable_llm_relevance_filter=True (for should_filter engines) and never
+ # unsets it — and RAG engines are not should_filter (no
+ # needs_llm_relevance_filter; dynamic ``collection_<uuid>`` name) — so
+ # this assignment is authoritative. The base filter is a no-op without
+ # an llm, so it degrades gracefully.
+ self.enable_llm_relevance_filter = to_bool(
+ get_setting_from_snapshot(
+ "search.rag.enable_relevance_filter",
+ default=True,
+ settings_snapshot=settings_snapshot,
+ ),
+ default=True,
+ )
+
def search(
self,
query: str,
diff --git a/tests/settings/golden_master_settings.json b/tests/settings/golden_master_settings.json
index 03b4cca460..51282c0b49 100644
--- a/tests/settings/golden_master_settings.json
+++ b/tests/settings/golden_master_settings.json
@@ -7680,6 +7680,34 @@
"value": 1,
"visible": true
},
+ "search.rag.enable_relevance_filter": {
+ "category": "search_parameters",
+ "description": "Apply an LLM relevance filter to local document / collection (RAG) results. Embeddings already rank by similarity, so this is optional; enabling it prunes semantically-near-but-irrelevant chunks at the cost of one extra LLM call per search.",
+ "editable": true,
+ "max_value": null,
+ "min_value": null,
+ "name": "Local RAG Relevance Filter",
+ "options": null,
+ "step": null,
+ "type": "SEARCH",
+ "ui_element": "checkbox",
+ "value": true,
+ "visible": true
+ },
+ "search.rag.max_results": {
+ "category": "search_parameters",
+ "description": "Maximum results (embedding chunks) the local document / collection (RAG) search returns per query. Capped independently of 'Max Results' because vector search returns many near-duplicate chunks that can flood the agent's context across research iterations.",
+ "editable": true,
+ "max_value": 100,
+ "min_value": 1,
+ "name": "Local RAG Max Results",
+ "options": null,
+ "step": 1,
+ "type": "SEARCH",
+ "ui_element": "number",
+ "value": 20,
+ "visible": true
+ },
"search.region": {
"category": "search_parameters",
"description": "Geographic region for search results localization. Affects which results are prioritized based on regional relevance.",
diff --git a/tests/web_search_engines/test_search_engine_library_settings.py b/tests/web_search_engines/test_search_engine_library_settings.py
index 17ed1d9a12..b50c30d76e 100644
--- a/tests/web_search_engines/test_search_engine_library_settings.py
+++ b/tests/web_search_engines/test_search_engine_library_settings.py
@@ -34,3 +34,71 @@ def test_defaults_used_when_keys_missing():
assert eng.embedding_provider == "sentence_transformers"
assert eng.chunk_size == 1000
assert eng.chunk_overlap == 200
+
+
+# ---------------------------------------------------------------------------
+# search.rag.max_results — cap local RAG retrieval (vector search returns many
+# near-duplicate chunks that otherwise flood the agent's context).
+# search.rag.enable_relevance_filter — opt-in LLM relevance filter for RAG.
+# ---------------------------------------------------------------------------
+
+
+def test_rag_max_results_caps_retrieval_when_set():
+ """search.rag.max_results overrides the inherited (web) max_results so
+ local RAG search returns fewer chunks."""
+ eng = LibraryRAGSearchEngine(
+ max_results=50, # global web default
+ settings_snapshot={"_username": "tester", "search.rag.max_results": 15},
+ )
+ assert eng.max_results == 15
+
+
+def test_rag_max_results_absent_preserves_caller_value():
+ """An absent setting must NOT silently cap a caller's explicit
+ max_results; real web/CLI runs carry the shipped default of 20."""
+ eng = LibraryRAGSearchEngine(
+ max_results=42,
+ settings_snapshot={"_username": "tester"},
+ )
+ assert eng.max_results == 42
+
+
+def test_rag_relevance_filter_on_by_default():
+ eng = LibraryRAGSearchEngine(settings_snapshot={"_username": "tester"})
+ assert eng.enable_llm_relevance_filter is True
+
+
+def test_rag_relevance_filter_can_be_disabled():
+ eng = LibraryRAGSearchEngine(
+ settings_snapshot={
+ "_username": "tester",
+ "search.rag.enable_relevance_filter": False,
+ },
+ )
+ assert eng.enable_llm_relevance_filter is False
+
+
+def test_collection_engine_inherits_rag_cap_and_filter():
+ """CollectionSearchEngine (subclass) gets the cap + filter from the base
+ __init__, which runs before its DB-loading collection-settings step."""
+ from unittest.mock import patch
+
+ from local_deep_research.web_search_engines.engines.search_engine_collection import (
+ CollectionSearchEngine,
+ )
+
+ with patch.object(
+ CollectionSearchEngine, "_load_collection_embedding_settings"
+ ):
+ eng = CollectionSearchEngine(
+ collection_id="abc",
+ collection_name="My Collection",
+ max_results=50,
+ settings_snapshot={
+ "_username": "tester",
+ "search.rag.max_results": 12,
+ "search.rag.enable_relevance_filter": False,
+ },
+ )
+ assert eng.max_results == 12
+ assert eng.enable_llm_relevance_filter is False