A production-ready Retrieval-Augmented Generation (RAG) system that automatically corrects retrieval errors using LangGraph for orchestration and FAISS for vector storage.
- Self-Correcting Retrieval: Automatically detects and fixes poor retrieval quality
- Transparent Decision-Making: Clear state machine with explicit routing decisions
- Module Decoupling: Easy to swap components (Redis → Pinecone, OpenAI → Anthropic)
- Document Grading: LLM-based relevance scoring before generation
- Query Rewriting: Transforms unclear queries into search-friendly formats
The system consists of 6 key components:
- Configuration Layer: Manages environment variables and API clients
- Retrieval Module: Handles document ingestion, vectorization, and storage
- Agent Node: Decision-making entry point
- Grade Edge: Quality checkpoint for search results
- Rewrite Node: Query transformation for better results
- Generation Node: Final answer production
src/
├── config/
│ ├── settings.py # Environment variables
│ └── openai.py # Model names and API clients
├── retriever.py # Document ingestion and FAISS vector store
├── agents/
│ ├── nodes.py # Agent, rewrite, and generate functions
│ ├── edges.py # Document grading logic
│ └── graph.py # LangGraph state machine
└── main.py # Entry point
- Python 3.10+
- OpenAI API key
# Install uv if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh
# Create virtual environment
uv venv
# Activate virtual environment
source .venv/bin/activate # Unix/macOS
# or
.venv\Scripts\activate # Windows
# Install dependencies
uv pip install -e .Your .env file should already contain:
OPENAI_API_KEY=your_api_key_here
python -m src.main- Agent receives question → Decides to retrieve or answer directly
- Retrieval → Fetches relevant documents from FAISS vector store
- Grading → LLM strictly evaluates document relevance with structured output
- Decision point:
- ✅ Relevant → Generate answer
- ❌ Not relevant → Rewrite query and retry
- Generation → Produces final answer based on verified context
Start → Agent → Retrieve → Grade Documents
├─ Relevant → Generate → End
└─ Not Relevant → Rewrite → Agent
❓ Question: What are the key components of an AI agent?
🔄 Processing...
📍 Node: agent
🔧 Tool Call: retrieve_documents
📍 Node: retrieve
💬 Output: Retrieved documents...
📍 Node: generate
💬 Output: Based on the documents...
✨ FINAL ANSWER:
The key components of an AI agent include:
1. Planning and reasoning capabilities
2. Memory systems for context retention
3. Tool use and execution
4. Reflection and self-improvement mechanisms
Modify src/retriever.py:
# Replace FAISS with Pinecone, Weaviate, etc.
from langchain_pinecone import PineconeVectorStoreModify src/config/openai.py:
from langchain_anthropic import ChatAnthropic
def get_llm():
return ChatAnthropic(model="claude-3-sonnet-20240229")Modify src/retriever.py:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=500, # Smaller chunks
chunk_overlap=100
)- Self-Correction: Detects and fixes poor retrieval quality
- Transparent: Every decision is logged and traceable
- Modular: Easy to replace components without breaking the system
- Production-Ready: Handles edge cases that simpler RAG systems miss
langchain- LLM frameworklangchain-openai- OpenAI integrationlangchain-community- Community componentslanggraph- State machine orchestrationfaiss-cpu- Local vector storebeautifulsoup4- Web scrapingpython-dotenv- Environment managementtiktoken- Token counting
# Verify API key is set
echo $OPENAI_API_KEY# Reinstall dependencies
uv pip install -e . --force-reinstallBased on the article about building Agentic RAG systems with self-correcting retrieval capabilities using LangGraph and vector stores.
Feel free to submit issues and enhancement requests!