A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.
π Self-Reflective Agentic RAG
A LangGraph-powered RAG system that grades its own retrieved context, rewrites the search query when needed, and only generates an answer once the context passes validation.

Overview
Standard RAG pipelines retrieve context and generate an answer immediately, with no check on whether the retrieved chunks are actually relevant or sufficient. This leads to hallucinations when the retrieval misses the mark.
This project solves that by introducing an iterative retrieval-validation loop before generation. Instead of answering right away, the system uses a LangGraph-driven state machine to:
- Retrieve context from the vector store
- Grade the retrieved context for relevance, sufficiency, and consistency
- Rewrite the query if the context doesn't pass, then re-retrieve with a better search
- Generate a grounded answer only once the context passes validation
This loop runs up to 3 times, progressively refining the search query until the LLM is confident the context is good enough, falling back gracefully to the best available context if the iteration limit is reached.
Features
- Self-Reflection Loop: LLM grades each retrieval attempt with a structured
VERDICT / REASON / REFINED_QUERYoutput before generation - Query Rewriting: On a
NOverdict, the system rewrites the search query based on what the grader says is missing, rather than re-running the same query - Iteration Cap: Hard limit of 3 retrieval loops prevents infinite loops on ambiguous or unanswerable questions
- Hallucination Reduction: Answer generation is strictly grounded because the prompt explicitly forbids the model from going beyond the validated context
- Full Reflection Log: The UI surfaces the complete per-iteration grading log so you can see exactly why the system looped or stopped
- Clean Gradio UI: Process/clear PDF controls, locked Ask button until a document is loaded, collapsible reflection accordion
Tech Stack
Frameworks & Libraries:
- LangGraph: State machine orchestration for the retrieval-validation loop
- LangChain: Document loading, text splitting, and ChromaDB integration
- ChromaDB: Local vector store for chunk storage and similarity search
- Gradio: Interactive web UI
Models (via Google AI / Gemini API):
- LLM:
gemma-4-26b-a4b-it, used for retrieval grading, query rewriting, and answer generation - Embeddings:
gemini-embedding-001withretrieval_documenttask type
How It Works
βββββββββββββββ
β retrieve βββββββββββββββββββββββ
ββββββββ¬βββββββ β
β β
ββββββββΌβββββββββββ ββββββββββ΄βββββββββββ
β grade_retrieval βββNO βββΊ β rewrite_query β
ββββββββ¬βββββββββββ βββββββββββββββββββββ
β YES (or max iterations reached)
ββββββββΌβββββββ
β generate β
ββββββββ¬βββββββ
β
END
Nodes:
retrieve: Fetches the top-4 most similar chunks using the current query (original or rewritten)grade_retrieval: Prompts Gemma to return a structuredVERDICT: YES/NO,REASON, andREFINED_QUERYrewrite_query: ParsesREFINED_QUERYfrom the grader output and updates the stategenerate: Produces the final answer, instructed to stay strictly within the validated context
Router logic:
VERDICT: YESβ go togenerateVERDICT: NO+ iterations < 3 β go torewrite_queryβretrieve- iterations β₯ 3 β force
generatewith best context available
Prerequisites
- Python 3.12 or higher
- uv (recommended for dependency management)
- A Google AI Studio API key with access to Gemma and Gemini Embedding models
Installation
1. Clone the Repository
git clone https://github.com/Sumanth077/Hands-On-AI-Engineering.git
cd Hands-On-AI-Engineering/ai_agents/agentic_rag_system
2. Set Up Environment Variables
cp .env.example .env
Open .env and add your key:
GEMINI_API_KEY=your_google_ai_studio_key_here
Get your API key at Google AI Studio.
3. Install Dependencies
uv sync
Usage
uv run main.py
Navigate to http://127.0.0.1:7860 in your browser.
- Upload a PDF using the Document panel on the left
- Click βοΈ Process; the status shows how many chunks were indexed
- Type a question in the Ask a Question panel and click π Ask
- Expand the Self-Reflection Loop accordion to see the per-iteration grading logs
- Read the grounded answer in the Answer section below
- Use ποΈ Clear to remove the current document and load a new one
Project Structure
agentic_rag_system/
βββ main.py # Gradio UI and PDF processing logic
βββ rag_graph.py # LangGraph state machine with nodes, edges, and router
βββ assets/
β βββ demo.png # App screenshot
βββ .env.example # API key template
βββ pyproject.toml # Project dependencies (uv)
βββ uv.lock # Locked dependency versions
βββ README.md
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License. See the LICENSE file for details.