retrieval.mdx


Overview

The Retrieval component is a critical module within the RAGFlow system designed to extract relevant information from one or more configured knowledge bases (datasets). It plays a central role in Retrieval-Augmented Generation (RAG) workflows by querying datasets to gather contextually relevant data that is then passed to large language models (LLMs) for generating responses or content.

This component can function in two distinct modes:


Detailed Explanation of Key Concepts and Configuration

Purpose and Functionality

Core Configuration Parameters

Configuration

Description

Default / Notes

Query variables

Specifies the input query variable(s) used to search knowledge bases.

Defaults to sys.query.

Knowledge bases

One or multiple datasets selected as retrieval targets. Must use the same embedding model if multiple are selected.

Mandatory unless empty response is blank.

Similarity threshold

Minimum combined similarity score (keyword + vector) for chunks to be considered relevant.

0.2

Keyword similarity weight

Weight assigned to the keyword similarity component in the combined similarity score. Must sum to 1 with the vector similarity weight.

0.7 (implying vector similarity weight is 0.3)

Top N

Number of top chunks to return to the LLM after retrieval.

8

Rerank model

Optional model to rerank retrieved chunks, replacing vector similarity with reranking scores. Improves quality but increases latency.

Disabled by default.

Empty response

Response to return if no relevant chunks are found. If blank, the LLM will improvise. Must be blank if no knowledge bases are selected.

Blank by default.

Cross-language search

Target languages for translating queries to ensure semantic matching across languages.

None by default.

Use knowledge graph

Enables multi-hop retrieval using pre-constructed knowledge graphs for complex queries. Significantly increases retrieval time.

Disabled by default.

Output

Global variable name for storing retrieval results, accessible by subsequent components.

User-defined.


Usage and Workflow

Typical Workflow

  1. Input Query: The component receives user input query variables (sys.query by default).

  2. Select Knowledge Bases: The system queries one or more datasets.

  3. Compute Similarity Scores: A weighted combination of keyword similarity and vector similarity scores is calculated for each chunk.

  4. Optional Reranking: If a rerank model is configured, chunks are reranked based on the reranking score combined with keyword similarity.

  5. Filter by Threshold: Chunks below the similarity threshold are excluded.

  6. Select Top N: The top N chunks are selected and returned.

  7. Optional Cross-language Query Translation: Queries can be translated if cross-language search is enabled.

  8. Optional Knowledge Graph Retrieval: Multi-hop retrieval is performed using knowledge graphs if enabled.

  9. Output: Retrieved chunks are output to a global variable for downstream processing.


Important Implementation Details


Interaction with Other System Components


Examples

Example 1: Standalone Retrieval Module

components:
  - name: Begin
    type: Begin
  - name: Retrieval
    type: Retrieval
    input_variables: ["sys.query"]
    knowledge_bases: ["kb_finance"]
    similarity_threshold: 0.25
    keyword_similarity_weight: 0.6
    top_n: 5
    output: "retrieved_chunks"

In this example, the Retrieval component queries the kb_finance knowledge base using the user query from sys.query, applying a similarity threshold of 0.25 and combining keyword similarity at 0.6 weight, returning the top 5 chunks to retrieved_chunks.

Example 2: Retrieval as a Tool for Agent

components:
  - name: Agent
    type: Agent
    user_prompt: "sys.query"
    tools:
      - Retrieval

Here, the Agent component autonomously invokes the Retrieval component with the user prompt as a query to gather information before generating a response.


Visual Diagram

flowchart TD
    A[Start: Receive Query Variables] --> B[Select Knowledge Bases to Query]
    B --> C[Translate Query (if Cross-language Search enabled)]
    C --> D[Compute Keyword Similarity]
    D --> E[Compute Vector Cosine Similarity]
    E --> F{Rerank Model Configured?}
    F -- Yes --> G[Compute Reranking Score]
    G --> H[Calculate Combined Score (Keyword + Rerank)]
    F -- No --> I[Calculate Combined Score (Keyword + Vector)]
    H --> J[Filter Chunks by Similarity Threshold]
    I --> J
    J --> K[Select Top N Chunks]
    K --> L{Use Knowledge Graph?}
    L -- Yes --> M[Perform Multi-hop Retrieval via Knowledge Graph]
    L -- No --> N[Output Retrieved Chunks to Global Variable]
    M --> N

Frequently Asked Questions

How to reduce response time?


References


Summary

The Retrieval component is a foundational element in RAGFlow workflows, enabling precise and flexible querying of knowledge bases to provide relevant context for LLM-driven tasks. Its configurability for similarity scoring, reranking, cross-language search, and knowledge graph usage makes it adaptable to various use cases, from simple retrieval to complex multi-hop question answering. Proper configuration and understanding of its parameters are essential for balancing retrieval quality and system performance.