đź”— RAG Patterns: Teaching Your AI to Find and Use Knowledge
🎠The Story: Your AI’s Super Library System
Imagine you have a magical library with millions of books. Your AI friend wants to answer questions, but it can’t read every book! So we build a smart librarian system that:
- Finds the right books quickly
- Reads just the important pages
- Answers your question using what it found
This is called RAG — Retrieval Augmented Generation. Let’s learn the five magic patterns that make this work!
📚 What is RAG?
Simple Explanation:
- You ask: “What’s the tallest mountain?”
- AI searches its library → finds pages about mountains
- AI reads those pages → answers “Mount Everest!”
Without RAG: AI guesses from memory (might be wrong!) With RAG: AI checks real documents first (much better!)
graph TD A["📝 Your Question"] --> B["🔍 Search Library"] B --> C["📄 Find Documents"] C --> D["🤖 AI Reads Them"] D --> E["✨ Smart Answer!"]
đź§© Pattern 1: create_retrieval_chain
The “Question → Find → Answer” Pipeline
Think of this like a vending machine:
- Put in your question (coin)
- Machine finds the right snack (documents)
- Out comes your answer!
How It Works
from langchain.chains import (
create_retrieval_chain
)
# Your retriever finds documents
retriever = vectorstore.as_retriever()
# Your chain answers questions
chain = create_retrieval_chain(
retriever,
combine_docs_chain
)
# Ask anything!
result = chain.invoke({
"input": "What is Python?"
})
What Happens Inside
graph TD A["🎤 User Question"] --> B["📦 Retriever"] B --> C["📄 Found Documents"] C --> D["🧠LLM Processes"] D --> E["💬 Final Answer"]
Real Example
Question: “How do I make cookies?”
Retriever finds:
- Recipe card #1: “Mix flour and sugar…”
- Recipe card #2: “Preheat oven to 350°…”
AI combines and answers:
“To make cookies, mix flour and sugar, then bake at 350°!”
đź§© Pattern 2: create_stuff_documents_chain
The “Stuff Everything In” Method
Imagine making a sandwich:
- Take ALL your ingredients
- Stuff them between bread
- Eat the whole thing!
This chain takes ALL found documents and stuffs them into one prompt.
How It Works
from langchain.chains.combine_documents import (
create_stuff_documents_chain
)
from langchain.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(
"""Answer based on context:
Context: {context}
Question: {input}"""
)
chain = create_stuff_documents_chain(
llm,
prompt
)
Visual Flow
graph TD A["📄 Doc 1"] --> D["📦 STUFF Together"] B["📄 Doc 2"] --> D C["📄 Doc 3"] --> D D --> E["🧠One Big Prompt"] E --> F["💬 Answer"]
When to Use
✅ Good for: Small amounts of text ✅ Good for: Quick answers ❌ Not good for: Thousands of documents
đź§© Pattern 3: create_history_aware_retriever
The “Remember Our Chat” Pattern
Have you ever asked a friend:
- “What’s your favorite color?” → “Blue!”
- “Why do you like it?” → Friend knows you mean blue!
This pattern helps AI remember previous questions!
The Problem Without It
You: "Tell me about dogs"
AI: "Dogs are loyal pets..."
You: "What do they eat?"
AI: "Who eats what?" ❌ (AI forgot!)
The Solution With It
You: "Tell me about dogs"
AI: "Dogs are loyal pets..."
You: "What do they eat?"
AI: "Dogs eat kibble and meat!" âś…
How It Works
from langchain.chains import (
create_history_aware_retriever
)
retriever = create_history_aware_retriever(
llm,
base_retriever,
contextualize_prompt
)
# Now it remembers chat history!
Visual Flow
graph TD A["💬 Chat History"] --> C["🔄 Reformulate"] B["❓ New Question"] --> C C --> D["🔍 Smart Search"] D --> E["📄 Better Results!"]
Example
Chat History:
- User: “Tell me about pandas”
- AI: “Pandas are bears from China…”
New Question: “What do they eat?”
AI thinks: “They = pandas from our chat” AI searches for: “What do pandas eat?” AI answers: “Pandas eat bamboo!”
đź§© Pattern 4: Agentic RAG
The “Smart Detective” Pattern
Regular RAG is like asking one librarian one question.
Agentic RAG is like having a detective who:
- Asks multiple questions
- Searches different libraries
- Decides what to do next
- Combines all clues!
How It’s Different
| Regular RAG | Agentic RAG |
|---|---|
| One search | Many searches |
| One answer | Thinks step by step |
| Simple questions | Complex problems |
How It Works
from langchain.agents import (
create_tool_calling_agent,
AgentExecutor
)
# Create retriever tool
retriever_tool = create_retriever_tool(
retriever,
"search_docs",
"Search the knowledge base"
)
# Agent can use tools
agent = create_tool_calling_agent(
llm,
[retriever_tool],
prompt
)
executor = AgentExecutor(
agent=agent,
tools=[retriever_tool]
)
Visual Flow
graph TD A["❓ Complex Question"] --> B["🤖 Agent Thinks"] B --> C{Need More Info?} C -->|Yes| D["🔍 Search Again"] D --> B C -->|No| E["💬 Final Answer"]
Example
Question: “Compare Python and JavaScript for web development”
Agent Actions:
- 🔍 Search “Python web development”
- 🔍 Search “JavaScript web development”
- 🤔 Think about differences
- đź’¬ Give comparison answer!
đź§© Pattern 5: Advanced RAG Techniques
Level Up Your RAG!
Think of these as power-ups for your RAG system:
🎯 Technique 1: Hybrid Search
Combine two search methods:
- Keyword search: Find exact words
- Semantic search: Find similar meanings
from langchain.retrievers import (
EnsembleRetriever
)
# Combine both methods
hybrid = EnsembleRetriever(
retrievers=[
keyword_retriever,
semantic_retriever
],
weights=[0.5, 0.5]
)
🎯 Technique 2: Re-ranking
Find documents, then sort by relevance:
graph TD A["🔍 Find 20 Docs"] --> B["📊 Score Each"] B --> C["🏆 Pick Top 5"] C --> D["💬 Answer"]
🎯 Technique 3: Query Expansion
One question becomes many:
Original: “Best programming language?”
Expanded:
- “Top programming languages 2024”
- “Most popular coding language”
- “Best language for beginners”
🎯 Technique 4: Parent Document Retrieval
Find small chunk → Get the whole page:
graph TD A["🔍 Find Small Chunk"] --> B["📄 Get Parent Doc"] B --> C["📖 More Context!"] C --> D["💬 Better Answer"]
🎯 Technique 5: Self-Query
AI writes its own search:
User: “Find cheap hotels in Paris”
AI creates filter:
- Location = “Paris”
- Price = “under $100”
🎓 Putting It All Together
Here’s how all five patterns work together:
graph TD A["❓ Question"] --> B["📜 History Aware"] B --> C["🔍 Retrieval Chain"] C --> D["📄 Stuff Documents"] D --> E{Complex?} E -->|Yes| F["🤖 Agentic RAG"] E -->|No| G["💬 Answer"] F --> G H["⚡ Advanced Techniques"] --> C
When to Use Each Pattern
| Pattern | Best For |
|---|---|
create_retrieval_chain |
Basic Q&A |
create_stuff_documents_chain |
Combining docs |
create_history_aware_retriever |
Chat apps |
| Agentic RAG | Complex research |
| Advanced Techniques | High accuracy |
🌟 Summary
You learned 5 powerful RAG patterns:
- create_retrieval_chain → The basic pipeline
- create_stuff_documents_chain → Combine all docs
- create_history_aware_retriever → Remember chat
- Agentic RAG → Smart detective mode
- Advanced Techniques → Power-ups!
Now your AI can find knowledge like a pro librarian and answer questions like a genius! 🎉
đź’ˇ Quick Tips
- Start with create_retrieval_chain for simple apps
- Add history awareness for chatbots
- Use Agentic RAG when questions need multiple searches
- Apply advanced techniques to improve accuracy
You’ve got this! 🚀
