Skip to content
AI360Xpert
Gen AI

RAG Fusion

Instead of trusting a single search query, RAG Fusion generates multiple variations of the query, searches the database for all of them, and mathematically fuses the results together.

RAG Fusion generates multiple queries, retrieves documents for each, and uses Reciprocal Rank Fusion to bubble up the documents that consistently appear across all searches.
RAG Fusion generates multiple queries, retrieves documents for each, and uses Reciprocal Rank Fusion to bubble up the documents that consistently appear across all searches.

Why Does This Exist?

When a user submits a query to a Retrieval-Augmented Generation (RAG) system, they are pulling the slot machine lever exactly once. If they phrased the question poorly, or if they used a synonym that doesn't align well with the embedded documents, the retrieval will fail, and the LLM will hallucinate.

Query Rewriting attempts to fix this by intercepting the user's query and having an LLM rewrite it into a "better" query. But what if the LLM's single rewritten query is also slightly off?

RAG Fusion acknowledges that we cannot perfectly predict the single best search query. Instead of generating one rewritten query, RAG Fusion uses an LLM to generate multiple variations of the user's query from different angles. It executes all of these queries in parallel and then mathematically merges the results using an algorithm called Reciprocal Rank Fusion (RRF).

This ensemble approach dramatically increases the robustness of the retrieval pipeline, ensuring that a single poorly-phrased query doesn't ruin the entire RAG process.

Think of It Like This

Asking three different doctors

Imagine you have a mysterious pain in your shoulder, and you ask a doctor, "Why does my arm hurt when I lift it?" The doctor might focus on the word "arm" and retrieve information about a bicep tear. (A single query missing the mark).

Instead of asking one doctor, RAG Fusion asks an LLM to rephrase your question for three different specialists:

  1. "What causes rotator cuff pain during elevation?" (Orthopedic query)
  2. "How do pinched nerves in the neck manifest in arm pain?" (Neurological query)
  3. "Common sports injuries related to overhead lifting." (Physical therapy query)

You send these three distinct queries into the medical database. The database returns books for each query. If a specific book on "Shoulder Impingement Syndrome" appears in the top results for all three queries, that book is given the highest score. You have fused multiple perspectives to find the absolute best answer.

How It Actually Works

The RAG Fusion pipeline consists of three steps:

1. Multi-Query Generation

The user's original query is sent to a fast LLM (like GPT-4o-mini). The prompt instructs the LLM to generate NN (usually 3 to 5) different variations of the query, attempting to capture different intents, synonyms, and abstraction levels.

User: "Impact of climate change on business" LLM Variations:

  1. "Financial risks associated with global warming"
  2. "Corporate sustainability adaptations to climate shifts"
  3. "Economic consequences of extreme weather events on supply chains"

2. Parallel Retrieval

The system executes a standard vector search for the original user query plus all the generated variations. If you generate 3 variations and retrieve the top 5 documents for each, you will get 20 total documents (some of which will be duplicates).

3. Reciprocal Rank Fusion (RRF)

You cannot just sum up the cosine similarity scores from the different searches, because vector spaces can be heavily skewed. Instead, RAG Fusion uses Reciprocal Rank Fusion (RRF), a scoring algorithm that completely ignores the raw similarity scores and relies only on the rank of the document.

For every document retrieved, its RRF score is calculated as: RRF Score=qQ1k+Rankq(D)\text{RRF Score} = \sum_{q \in Q} \frac{1}{k + \text{Rank}_q(D)}

  • QQ is the set of queries.
  • Rankq(D)\text{Rank}_q(D) is the rank (1st, 2nd, 3rd) of Document DD for query qq.
  • kk is a smoothing constant (usually set to 60).

If a document is ranked #1 for Query A, #3 for Query B, and didn't appear for Query C, its score is: (1/61)+(1/63)+0=0.0322(1 / 61) + (1 / 63) + 0 = 0.0322

Documents that consistently appear near the top across multiple query variations will mathematically bubble to the top of the final fused list. This final list of highly robust documents is then fed into the LLM for generation.

Show Me the Code

This is a clean, conceptual implementation of the Reciprocal Rank Fusion algorithm in Python.

def reciprocal_rank_fusion(search_results_dict, k=60):    """    Fuses multiple ranked lists using RRF.        search_results_dict: A dictionary where keys are queries and values are                          ordered lists of document IDs (from rank 1 to N).    """    fused_scores = {}
    # Iterate through the results of each query variation    for query, doc_list in search_results_dict.items():                # Iterate through the documents, noting their rank (1-indexed)        for rank, doc_id in enumerate(doc_list, start=1):                        # Initialize the doc score if we haven't seen it yet            if doc_id not in fused_scores:                fused_scores[doc_id] = 0.0                            # Add the reciprocal rank score            fused_scores[doc_id] += 1.0 / (rank + k)
    # Sort documents by their final fused score in descending order    reranked_docs = sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)    return reranked_docs
# --- Example ---# We ran 3 queries. Doc_A appears in all 3. Doc_B is #1 in the first query, but doesn't appear again.results = {    "Original":  ["Doc_B", "Doc_A", "Doc_C"],    "Variant_1": ["Doc_A", "Doc_D", "Doc_E"],    "Variant_2": ["Doc_F", "Doc_A", "Doc_G"]}
fused_results = reciprocal_rank_fusion(results)
print("Final RRF Rankings:")for doc_id, score in fused_results:    print(f"{doc_id}: {score:.5f}")
# -> Final RRF Rankings:# -> Doc_A: 0.04838  <-- Bubbled to the top because it appeared consistently!# -> Doc_B: 0.01639  <-- Dropped because it was a one-hit wonder.# -> Doc_F: 0.01639# -> Doc_D: 0.01613# -> Doc_C: 0.01587# -> Doc_E: 0.01587# -> Doc_G: 0.01587

Watch Out For

Retrieval Latency Spike

Because RAG Fusion requires generating multiple queries and executing a vector search for every single one, it is inherently slower than standard RAG. If your vector database takes 100ms to execute a search, generating 5 query variations will take 500ms (unless you meticulously parallelize the database calls). Always execute the vector searches concurrently via threading or asyncio.

The Quick Version

  • Standard RAG relies on a single query, which is a single point of failure if the vocabulary is slightly off.
  • RAG Fusion uses an LLM to generate multiple semantic variations of the user's query.
  • It executes a vector search for every variation independently.
  • It merges the resulting document lists using Reciprocal Rank Fusion (RRF).
  • RRF ignores vector similarity scores and ranks documents based on how consistently they appear at the top of the different search variations.
  • Read Query Decomposition to see how generating sub-queries differs from generating parallel variations.
  • Read Query Rewriting for the foundational concept of intercepting user inputs.
  • Read Maximal Marginal Relevance (MMR) for a completely different reranking strategy focused on diversity rather than consensus.

Related concepts