Agent Retrieval#
The Retrieval node is a canvas component in RAGFlow agent workflows that connects one or more knowledge bases to a running agent graph. Its implementation lives in agent/tools/retrieval.py and, like every other retrieval entry point, converges on the shared engine Dealer.retrieval() in rag/nlp/search.py .
Configuration Parameters#
The RetrievalParam class defines all configurable fields:
| Parameter | Default | Notes |
|---|---|---|
dataset_ids / kb_ids | [] | Primary dataset list; kb_ids is the legacy alias |
similarity_threshold | 0.2 | Minimum score to include a chunk |
keywords_similarity_weight | 0.5 | Weight for keyword (BM25) vs. vector scoring |
top_n | 8 | Chunks returned to the downstream node |
top_k | 1024 | Candidate pool size before reranking |
rerank_id | "" | Optional reranker model |
use_kg | false | Enable Knowledge Graph injection |
cross_languages | [] | Language codes for query expansion |
toc_enhance | false | Table-of-contents re-ranking |
meta_data_filter | {} | Document-level filter (auto/semi_auto/manual) |
empty_response | "" | Fallback text when retrieval returns nothing |
memory_ids | [] | Alternative: retrieve from memory nodes instead of KBs |
The single runtime input is a Query line field .
Dataset Selection#
Datasets are configured via dataset_ids (with kb_ids as a backward-compatible alias) . Each entry in the list can be:
- A literal KB UUID — added directly to the retrieval call .
- A
{{cpn_id@var}}variable reference — resolved at runtime against canvas outputs, then looked up by name or ID viaKnowledgebaseService.get_by_name()/get_by_id(). Variables may resolve to a single value or a list .
Before retrieval executes, the system validates that all selected datasets share the same embedding model .
Query Preprocessing Pipeline#
Inside _retrieve_kb(), the query passes through this fixed sequence before hitting Dealer.retrieval() :
- Variable substitution —
get_input_elements_from_text()+string_format()resolve{{...}}tokens in the query text . "user:"prefix stripping —re.sub(r"^user[::\s]*", "", query, re.IGNORECASE)removes any prefix a prior LLM step may have prepended .- Metadata filtering —
apply_meta_data_filter()computes adoc_idswhitelist in one of three modes:auto(LLM-generated),semi_auto(LLM-restricted to specified keys), ormanual(explicit conditions). A sentinel"-999"is passed when no docs match to prevent unfiltered fallback . - Cross-language expansion —
cross_languages()translates the query into target languages and concatenates all variants into a single retrieval call .
Knowledge Graph Integration#
When use_kg=true, after the standard vector retrieval completes, settings.kg_retriever.retrieval() is called with the tenant ID, chat LLM config, embedding model, and KB IDs. The returned KG chunk is inserted at index 0 of the results list — meaning it always appears first regardless of its vector similarity score .
Prerequisites for KG to work :
- Build the KB's knowledge graph first (parse documents → Generate → Knowledge graph).
- In the Retrieval node's Advanced settings, enable Use knowledge graph.
- Use
{retrieval_node_id@formalized_content}downstream as usual.
Note: the KG generation workflow changed in September 2025 — the "Knowledge Graph" chunk method was removed from dataset creation. KG is now a separate post-parsing step .
Divergence from Knowledge Retrieval Test#
The Knowledge Retrieval Test UI (the /api/v1/retrieval endpoint, handled by retrieval_test()) and the Agent Retrieval node both call Dealer.retrieval(), but with different hardcoded defaults :
| Parameter | KB Retrieval Test | Agent Retrieval |
|---|---|---|
vector_similarity_weight | 0.3 | 0.5 |
top_n | 30 | 8 |
Additional non-configurable differences:
- The Agent strips
"user:"prefixes; the KB test does not. - The Agent applies metadata filtering and variable substitution automatically; the KB test requires explicit
document_ids. - Rerank batching uses a dynamic window (~64 chunks) in the agent path vs. a single pass in the KB test, which can change score normalization and final ranking order even with a matched reranker .
Practical impact: The Retrieval Test is not a faithful replica of Agent Retrieval behavior. To minimize divergence when tuning, explicitly set vector_similarity_weight=0.5 and page_size=8 in the test, or match both sides manually .
Multi-KB Behavior and Known Issues#
When multiple datasets are selected, all are searched in a single Dealer.retrieval() call. A known historical issue (v0.17.x) caused the effective candidate pool (RERANK_LIMIT) to be insufficient when many KBs were selected, causing higher-relevance chunks from some KBs to be dropped before reranking . Subsequent releases addressed this.
Key Source Files#
| File | Purpose |
|---|---|
agent/tools/retrieval.py | Main Retrieval node — RetrievalParam, _retrieve_kb() |
rag/nlp/search.py | Dealer.retrieval() — shared retrieval engine |
agent/component/base.py | get_input_elements_from_text(), string_format() — variable resolution |
common/metadata_utils.py | apply_meta_data_filter() — metadata filtering |
api/apps/restful_apis/chunk_api.py | retrieval_test() — KB Retrieval Test endpoint |