|
|
Corrective RAG - Self-Evaluation and Query Reformulation
Author: Venkata Sudhakar
Corrective RAG (CRAG) improves retrieval accuracy by adding a self-evaluation step before generating an answer. ShopMax India uses this pattern to catch cases where retrieved product documents are irrelevant to a customer query - for example, when a query about a Sony camera retrieves laptop specs instead. Rather than silently returning a wrong answer, CRAG detects the mismatch and either triggers a web search fallback or reformulates the query for a second retrieval attempt.
The CRAG pipeline works in three stages. First, the retriever fetches top-k documents as usual. Second, an evaluator LLM call scores each retrieved document as 'relevant', 'partially relevant', or 'irrelevant'. Third, based on the scores: if at least one document is relevant, proceed to generation; if all are irrelevant, fall back to an alternative source or return a 'not found' response rather than hallucinating. This evaluation step adds one extra LLM call per query but dramatically reduces hallucination on out-of-distribution queries.
The following example implements a CRAG pipeline for ShopMax India product queries. The evaluator scores retrieved chunks, filters out irrelevant ones, and routes to fallback when no relevant chunks remain.
It gives the following output,
Q: What is the battery life of Sony headphones?
A: The Sony WH-1000XM5 headphones offer a 30-hour battery life and support USB-C charging.
Relevant docs used: 1
Q: What are the cooking recipes for biryani?
A: No relevant product information found for your query. Please contact ShopMax India support.
Relevant docs used: 0
For ShopMax India, use the smaller Claude Haiku model for the relevance evaluation step to keep costs low - it only needs to output one word. Reserve Claude Opus for the final answer generation. Log all queries that hit the fallback path; these reveal gaps in your product knowledge base that need new documents. Over time, corrective RAG transforms from a safety net into a data quality feedback loop that continuously improves your retrieval corpus.
|
|