Learning LangChain, Agentic AI & RAGs because me want full control over these clankers.
It covers different topics in LangChain such as Introduction, Prompts, Runnables, Models, Document Processing, Retrievers, Chains, Output Parsers and Callbacks.
It covers different topics in LangGraph such as State Management, Nodes & Edges, Conditional Routing, Checkpointing, Human-in-the-Loop, Multi-Agent Systems, Agent Architectures, Subgraphs, Streaming and Error Handling.
CRAG (Corrective Retrieval Augmented Generation) is an advanced RAG technique that improves the reliability and accuracy of retrieval-augmented language models by adding a correction mechanism to evaluate and refine retrieved documents before generation.
Traditional RAG systems retrieve documents and use them directly for generation, which can lead to:
- Irrelevant retrieved documents being used in generation
- Inaccurate or outdated information affecting output quality
- No mechanism to verify retrieval quality
- Hallucinations when retrieved context is poor
CRAG introduces a retrieval evaluator that assesses the quality of retrieved documents and triggers corrective actions when needed. Architecture Components
Query → Retrieval → Evaluation → Correction (if needed) → Generation
- Retrieval: Standard document retrieval from knowledge base
- Evaluation: Lightweight evaluator model assesses retrieval quality
- Correction: Triggered based on evaluation confidence
- Generation: Final response using corrected/verified context
Based on the retrieval evaluator's confidence assessment:
- Correct (High Confidence)
- Retrieved documents are relevant and accurate
- Proceed directly to generation
- Use retrieved knowledge as-is
- Incorrect (Low Confidence)
- Retrieved documents are irrelevant or poor quality
- Discard retrieved documents
- Use web search or alternative knowledge sources
- Prevents hallucination from bad retrieval
- Ambiguous (Medium Confidence)
- Retrieved documents are partially relevant
- Apply knowledge refinement
- Decompose documents into knowledge strips
- Filter and rerank at granular level
- Use only most relevant segments
# Pseudocode
def crag_pipeline(query):
# 1. Retrieve documents
documents = retriever.retrieve(query)
# 2. Evaluate retrieval quality
confidence = evaluator.evaluate(query, documents)
# 3. Corrective action
if confidence == "CORRECT":
context = documents
elif confidence == "INCORRECT":
context = web_search(query)
else: # AMBIGUOUS
strips = decompose_to_strips(documents)
relevant_strips = filter_and_rerank(query, strips)
context = relevant_strips
# 4. Generate response
response = generator.generate(query, context)
return response