Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

6.1 KB
Raw Blame

πŸ”„ 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.

Self-Reflective Agentic RAG Demo

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:

  1. Retrieve context from the vector store
  2. Grade the retrieved context for relevance, sufficiency, and consistency
  3. Rewrite the query if the context doesn't pass, then re-retrieve with a better search
  4. 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_QUERY output before generation
  • Query Rewriting: On a NO verdict, 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):

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 structured VERDICT: YES/NO, REASON, and REFINED_QUERY
  • rewrite_query: Parses REFINED_QUERY from the grader output and updates the state
  • generate: Produces the final answer, instructed to stay strictly within the validated context

Router logic:

  • VERDICT: YES β†’ go to generate
  • VERDICT: NO + iterations < 3 β†’ go to rewrite_query β†’ retrieve
  • iterations β‰₯ 3 β†’ force generate with 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.

  1. Upload a PDF using the Document panel on the left
  2. Click βš™οΈ Process; the status shows how many chunks were indexed
  3. Type a question in the Ask a Question panel and click πŸ” Ask
  4. Expand the Self-Reflection Loop accordion to see the per-iteration grading logs
  5. Read the grounded answer in the Answer section below
  6. 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.


⬆ Back to Top