CRAG: Corrective RAG
Author: Jaeho Kim
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
OverView
The Corrective RAG (CRAG) strategy focuses on improving RAG-based systems.CRAG is an approach that refines the search-generation pipeline by incorporating self-reflection and self-evaluation phases for the retrieved documents.

What is CRAG ?
Corrective-RAG (CRAG) is a methodology in the RAG (Retrieval-Augmented Generation) strategy that adds a step to evaluate the retrieved documents and refine the knowledge. This approach involves reviewing the search results before generation, conducting supplementary searches if necessary, and ultimately includes a series of processes to produce high-quality responses.
The core ideas of CRAG are as follows:
Link to the paper (Corrective Retrieval-Augmented Generation)
If one or more of the retrieved documents exceed the predefined relevance threshold (retrieval validation score), the process proceeds to the generation stage.
A knowledge refinement step is performed before generation.
Documents are divided into "knowledge strips" (where
krefers to the number of document retrieval results).Each knowledge strip is evaluated, and its relevance is scored (evaluations are conducted at the document chunk level).
If all documents fall below the relevance threshold or the evaluation results have low confidence, additional data sources (e.g., web searches) are used for supplementation.
When supplementing through web searches, query results are optimized using
Query-Rewrite.
Key Points
This tutorial demonstrates implementing some of the ideas from the CRAG approach using LangGraph.
Here, the knowledge refinement step is omitted but is designed to be added as a node if necessary.
Additionally, if no relevant documents are found, web searches will be used to supplement the retrieval.
For web searches, Tavily Search will be utilized, and Question Rewrite will be introduced to optimize the search process.
Overview of Key Steps
Retrieval Grader: Evaluate the relevance of the retrieved documents.
Generate: Generate answers using LLM.
Question Re-writer: Optimize search queries by rewriting the question.
Web Search Tool: Utilize Tavily Search for web searches.
Create Graph: Create a CRAG strategy graph using LangGraph.
Use the graph: Learn how to utilize the generated graph.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
Creating a Basic PDF-Based Retrieval Chain
We will create a Retrieval Chain based on PDF documents. This represents the simplest structure of a Retrieval Chain.
Note that in LangGraph, the Retriever and Chain are created separately. This allows for detailed processing at each node.
Note
This was covered in a previous tutorial, so detailed explanations are omitted.
Relevance Evaluation of Retrieved Documents (Question-Retrieval Evaluation)
The relevance evaluation of retrieved documents is the step where the retrieved documents are assessed for their relevance to the question.
First, create an evaluator (retrieval-grader) to assess the retrieved documents.
Using retrieval_grader, documents are evaluated.
Here, the evaluation is performed on a single document rather than a set of documents.
The result returns the relevance of the single document as either yes or no .
Answer Generation Chain
The answer generation chain is a chain that generates answers based on the retrieved documents.
It is a typical Naive RAG chain that we are familiar with.
Note
RAG Prompt from LangChain PromptHub: https://smith.langchain.com/hub/rlm/rag-prompt
Question Re-write
Query rewriting is a step where the question is rewritten to optimize web searches.
Rewrite Question Using question_rewriter.
Web search tool
Web search tool is used to supplement the context.
Need for Web Search: When all documents fail to meet the relevance threshold or the evaluator lacks confidence, additional data is retrieved through web searches.
Using Tavily Search: Perform web searches using Tavily Search, which optimizes search queries and provides more relevant results.
Question Rewrite: Improve search queries by rewriting the question to optimize web searches.
State
Define the state for the CRAG graph.
Web_search represents the state indicating whether to use web search.
It is expressed as yes or no (yes: web search required, no: not required).
Node
Define the nodes to be used in the CRAG graph.
Function for Conditional Edges
The decide_to_generate function routes to the next node based on whether web search is required after relevance evaluation.
If web_search is Yes, it rewrites the query at the query_rewrite node and performs a web search.
If web_search is No, it proceeds to generate to create the final answer.
Graph Creation
Now, define the nodes and connect the edges to complete the graph.
Visualize the graph.

Graph Execution
Now, execute the graph and check the results. This explains how to stream results in a RAG application.
There are two streaming output modes for the graph: messages and updates. These can be controlled by specifying the stream_mode parameter.
stream_mode="updates": Typically streams during the call steps, meaning updates to the state of individual nodes. In this case, each node simply adds a new key to the state.stream_mode="messages": Streams tokens from a chat model call.
The following explains the output method for stream_mode="updates"
The following explains the output method for stream_mode="messages"
In the output above, since the question was relevant to the document, it directly connects to the generation node.
This time, the question is not relevant to the document. Therefore, it connects to the query_rewrite node and then to the web_search node.
In other words, if the grade_documents node determines that no document is relevant, it connects to the web_search node.
Here is the output for stream_mode="messages".
Last updated