GenAIHub
← Back to RAG

Corrective RAG (CRAG)

Retrieval-Augmented Generation with Dynamic Correction

🎧

Listen to this Explanation

Enjoy a clear, AI-narrated audio version (Solo Mode).

Overview

Corrective RAG (CRAG) is an advanced information retrieval and response generation system. It extends the standard RAG approach by dynamically evaluating and correcting the retrieval process, combining the power of vector databases, web search, and language models to provide accurate and context-aware responses to user queries.

"CRAG overcomes limitations of traditional RAG systems by intelligently evaluating retrieval quality and dynamically correcting the information sourcing strategy based on relevance scores."

CRAG Process Flowchart

CRAG Process Flow Diagram

Flowchart showing the Corrective RAG decision process based on relevance scores

Motivation

While traditional RAG systems have improved information retrieval and response generation, they can still fall short when the retrieved information is irrelevant or outdated. CRAG addresses these limitations by:

Pre-existing Knowledge

Leveraging structured knowledge bases

Relevance Evaluation

Assessing retrieved information quality

Dynamic Web Search

Searching web when needed

Knowledge Refinement

Refining and combining sources

Human-like Responses

Generating contextual answers

Key Components

FAISS Index

A vector database for efficient similarity search of pre-existing knowledge. Enables fast retrieval of relevant documents based on semantic similarity.

Retrieval Evaluator

Assesses the relevance of retrieved documents to the query using a 0-1 scoring system. Critical for determining the next action in the CRAG pipeline.

Knowledge Refinement

Extracts key information from documents when necessary, condensing large documents into actionable bullet points.

Web Search Query Rewriter

Optimizes queries for web searches when local knowledge is insufficient. Transforms user queries into more effective web search terms.

Response Generator

Creates human-like responses based on the accumulated knowledge, including source attribution for transparency.

CRAG Process Flow

1

Document Retrieval

Performs similarity search in the FAISS index to find relevant documents. Retrieves top-k documents (default k=3).

docs = faiss_index.similarity_search(query, k=3)
2

Document Evaluation

Calculates relevance scores for each retrieved document using the Retrieval Evaluator. Determines the best course of action based on the highest relevance score.

eval_scores = [retrieval_evaluator(query, doc) for doc in retrieved_docs]
3

Corrective Knowledge Acquisition

Based on the evaluation scores, CRAG determines the appropriate action:

CORRECT

Score > 0.7: Uses the most relevant document as-is. High confidence in local knowledge.

INCORRECT

Score < 0.3: Performs web search with rewritten query. Local knowledge is insufficient.

AMBIGUOUS

0.3 ≀ Score ≀ 0.7: Combines document with web search results for comprehensive answer.

4

Adaptive Knowledge Processing

For web search results: Refines the knowledge to extract key points. For ambiguous cases: Combines raw document content with refined web search results.

5

Response Generation

Uses a language model to generate a human-like response based on the query and acquired knowledge. Includes source information in the response for transparency.

Implementation Example

# CRAG Process Implementation
def crag_process(query: str, faiss_index: FAISS) -> str:
    """Process query with dynamic correction"""
    
    # Step 1: Retrieve documents
    retrieved_docs = retrieve_documents(query, faiss_index, k=3)
    
    # Step 2: Evaluate relevance
    eval_scores = evaluate_documents(query, retrieved_docs)
    max_score = max(eval_scores)
    
    # Step 3: Corrective knowledge acquisition
    if max_score > 0.7:
        # CORRECT: Use retrieved document
        best_doc = retrieved_docs[eval_scores.index(max_score)]
        final_knowledge = best_doc
        sources = [("Retrieved document", "")]
        
    elif max_score < 0.3:
        # INCORRECT: Perform web search
        final_knowledge, sources = perform_web_search(query)
        
    else:
        # AMBIGUOUS: Combine both sources
        best_doc = retrieved_docs[eval_scores.index(max_score)]
        retrieved_knowledge = knowledge_refinement(best_doc)
        web_knowledge, web_sources = perform_web_search(query)
        final_knowledge = "\n".join(retrieved_knowledge + web_knowledge)
        sources = [("Retrieved document", "")] + web_sources

    # Step 4: Generate response
    response = generate_response(query, final_knowledge, sources)
    return response

Benefits of CRAG

Dynamic Correction

Adapts to the quality of retrieved information, ensuring relevance and accuracy.

Flexibility

Leverages both pre-existing knowledge and web search as needed.

Accuracy

Evaluates the relevance of information before using it, ensuring high-quality responses.

Transparency

Provides source information, allowing users to verify the origin of the information.

Efficiency

Uses vector search for quick retrieval from large knowledge bases.

Up-to-date Information

Can supplement or replace outdated local knowledge with current web information.

Contextual Understanding

Combines multiple sources of information when necessary to provide comprehensive responses.

Ideal Use Cases

Research Assistance

When accuracy and up-to-date information are critical for research tasks.

Dynamic Knowledge Bases

Systems that need to stay current while leveraging historical knowledge.

Advanced Q&A Systems

Question-answering that requires high accuracy and current information.

Related Topics

Test Your Knowledge

Score 8/10 or higher to pass