A self-correcting retrieval-augmented generation (RAG) system built using LangGraph that dynamically decides when to retrieve, how to refine queries, and how to generate grounded responses.
Traditional RAG systems:
- Always retrieve (even when unnecessary)
- Retrieve irrelevant data
- Lack self-correction
- Fail when initial retrieval is weak
This system introduces an agentic decision layer that:
- Decides whether retrieval is needed
- Filters irrelevant documents
- Rewrites queries if retrieval fails
- Falls back to web search
- Generates answers using only validated context
-
Decide Retrieval
- Determine if external data is required
-
Direct Generation (if no retrieval)
- Answer using model knowledge
-
Retrieve Documents
- From PDF / vector database
-
Relevance Filtering
- Keep only useful documents
-
If not relevant
- Rewrite query
- Perform web search
- Retry retrieval
-
Generate Answer
- Use only validated context
- Self-RAG decision system
- Query rewriting loop
- Relevance filtering
- Hybrid retrieval (PDF + Web)
- Agentic control flow using LangGraph
- Reduced hallucination via structured prompts
- Python
- LangGraph
- LangChain
- FAISS (vector DB)
- Sentence Transformers (embeddings)
- Tavily (web search)
- Ollama / OpenAI
git clone https://github.com/your-username/chat-with-pdf.git
cd chat-with-pdfpip install -r requirements.txt
Create .env file:
OPENAI_API_KEY=your_key_here
TAVILY_API_KEY=your_key_here
Run the notebook:
jupyter notebook askmypdf.ipynb
Example query:
"What is recursion in programming?"
Input:
"Latest AI news in 2025"
System will:
- Detect need for fresh data
- Rewrite query
- Perform web search
- Filter relevant sources
- Generate grounded answer
- Streaming responses
- UI (Streamlit / Web app)
- Multi-document support
- Evaluation pipeline
- Fine-tuned retrieval decision model
This is not just a RAG system.
It is a self-correcting, agentic retrieval system that improves its own search and reasoning loop.
Feel free to:
- Open issues
- Suggest improvements
- Extend modules
MIT License
