Enhance RAG with Self-Correction and Web Search
LangGraph Corrective RAG (CRAG) chain that grades retrieved documents and falls back to Tavily web search when relevance is low.
Why it matters
Improve retrieval-augmented generation (RAG) by implementing a self-correcting mechanism that evaluates retrieved documents for relevance and supplements with web search when necessary.
Outcomes
What it gets done
Implement a RAG pipeline with self-reflection capabilities.
Integrate web search to augment retrieval when initial documents are insufficient.
Utilize query rewriting to optimize search queries.
Filter irrelevant retrieved documents before generation.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/lg-langgraphcrag | bash Steps
Steps in the chain
Overview
Corrective RAG (CRAG)
A LangGraph chain implementing Corrective RAG: grades retrieved documents for relevance and falls back to query-rewritten Tavily web search when retrieval quality is low. Use when a RAG pipeline needs a relevance-grading and web-search-fallback step rather than trusting a single retrieval pass.
What it does
Corrective RAG (CRAG) is a LangGraph prompt chain implementing the CRAG strategy, which adds self-reflection and self-grading of retrieved documents to a standard RAG pipeline. The source paper's approach: if at least one retrieved document exceeds a relevance threshold, proceed straight to generation, but first run knowledge refinement - partitioning each document into "knowledge strips," grading each strip, and filtering out irrelevant ones. If all documents fall below the relevance threshold, or the grader is unsure, the framework falls back to an additional data source via web search.
When to use - and when NOT to
Use it when a RAG pipeline needs a self-correction step - grading whether retrieved context is actually relevant before generating an answer, rather than trusting retrieval blindly. This implementation deliberately simplifies the paper: it skips the knowledge-refinement/strip-grading phase on a first pass (noting it can be added back as a graph node later), and triggers the web-search fallback if any retrieved document is graded irrelevant, rather than only when all of them are. It is not meant for pipelines that already trust a single, high-recall retrieval source and don't need a relevance-grading or fallback step.
Inputs and outputs
Input is a user question over an indexed corpus (the example indexes three blog posts, skipping the paper's knowledge strips refinement step on this first pass). The chain builds a LangGraph graph: it creates the index, sets up the LLMs and a web search tool, defines graph state, and compiles a graph that follows the grade-then-generate-or-fallback flow. When retrieval quality is graded as poor, it uses query re-writing to optimize the original question specifically for web search before calling out to Tavily Search. Output is the generated answer, sourced from the index alone or supplemented with live web search results depending on the grading outcome.
Integrations
Web search fallback is implemented via Tavily Search. The chain is built with LangGraph, and LangSmith is recommended for tracing, debugging, and monitoring the resulting LangGraph application during development.
Who it's for
Developers building RAG applications who want a self-grading, self-correcting retrieval step - triggered once any document falls below the relevance threshold - with a web-search fallback for low-relevance results, implemented as a LangGraph graph rather than a single-pass retrieve-and-generate chain.
Source README
This directory is retained purely for archival purposes and is no longer updated. Please see the newly consolidated LangChain documentation for the most current information and resources.
Corrective RAG (CRAG)
Corrective-RAG (CRAG) is a strategy for RAG that incorporates self-reflection / self-grading on retrieved documents.
In the paper here, a few steps are taken:
- If at least one document exceeds the threshold for relevance, then it proceeds to generation
- Before generation, it performs knowledge refinement
- This partitions the document into "knowledge strips"
- It grades each strip, and filters our irrelevant ones
- If all documents fall below the relevance threshold or if the grader is unsure, then the framework seeks an additional datasource
- It will use web search to supplement retrieval
We will implement some of these ideas from scratch using LangGraph:
- Let's skip the knowledge refinement phase as a first pass. This can be added back as a node, if desired.
- If any documents are irrelevant, let's opt to supplement retrieval with web search.
- We'll use Tavily Search for web search.
- Let's use query re-writing to optimize the query for web search.
Setup
First, let's download our required packages and set our API keys
Set up LangSmith for LangGraph development
Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph - read more about how to get started here.
Create Index
Let's index 3 blog posts.
LLMs
Web Search Tool
Create Graph
Now let's create our graph that will use CRAG
Define Graph State
Compile Graph
The just follows the flow we outlined in the figure above.
Use the graph
LangSmith Traces -
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.