Skip to content

Mohamedkhattab02/Agentic-RAG-with-LangGraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LangChain

πŸ¦œπŸ”— Advanced Adaptive RAG

A self-correcting, self-reflective RAG agent β€” built on the πŸ¦œπŸ”— LangChain & πŸ¦œπŸ•ΈοΈ LangGraph ecosystem.

RouteΒ β†’Β RetrieveΒ β†’Β GradeΒ β†’Β GenerateΒ β†’Β Self-Reflect

Python LangChain LangGraph Gemini ChromaDB LangSmith License


Note

Powered end-to-end by the 🦜 LangChain stack: LangChain for composable LCEL chains, LangGraph for the stateful agent graph, and LangSmith for full-trace observability.


✨ Overview

This project implements an Adaptive RAG pipeline that goes far beyond naive "retrieve-then-answer" systems. It combines three powerful research ideas into a single, robust LangGraph state machine:

Technique What it does
🧭 Adaptive RAG Routes each question to the right source β€” the local vector store or live web search.
πŸ›‘οΈ Corrective RAG (CRAG) Grades every retrieved document for relevance and falls back to web search when knowledge is missing.
πŸͺž Self-RAG Reflects on its own answer to detect hallucinations and verify the question is actually addressed β€” retrying when it isn't.

The result is an agent that knows what it knows, fetches what it doesn't, and never confidently makes things up.


πŸ—ΊοΈ How It Works

The agent is modeled as a graph of decision nodes. Each question flows through routing, grading, generation, and self-reflection loops until a grounded and useful answer is produced.

Adaptive RAG conceptual flow
The conceptual flow: route β†’ retrieve β†’ grade β†’ (web search) β†’ generate β†’ self-reflect.
πŸ“ˆ Compiled LangGraph diagram (auto-generated from the code)
Compiled LangGraph workflow diagram

The flow, step by step

  1. 🧭 Route Question β€” An LLM router classifies the question. Topics about agents, prompt engineering, or adversarial attacks go to the vector store; everything else goes to web search.
  2. πŸ“š Retrieve β€” Pulls the most semantically relevant chunks from the ChromaDB vector store.
  3. πŸ” Grade Documents β€” Each retrieved document is scored yes/no for relevance. Irrelevant docs are dropped, and a web_search flag is raised if anything is missing.
  4. 🌐 Web Search (conditional) β€” If knowledge gaps are detected, Tavily fetches fresh results from the web and appends them to the context.
  5. ✍️ Generate β€” Gemini produces an answer grounded in the collected context.
  6. πŸͺž Self-Reflection β€” The answer is double-checked:
    • Hallucination grader β†’ Is the answer grounded in the documents? If no, regenerate.
    • Answer grader β†’ Does the answer actually resolve the question? If no, fall back to web search.
    • If both pass β†’ βœ… return the answer.

πŸ› οΈ Tech Stack

Built on the 🦜 LangChain ecosystem, with best-in-class models and infrastructure around it.

Component Role
πŸ¦œπŸ•ΈοΈ LangGraph Orchestration β€” the stateful, cyclic agent graph
πŸ¦œπŸ”— LangChain Composable LCEL chains (routing, grading, generation)
πŸ¦œπŸ› οΈ LangSmith Full-trace observability & debugging
✨ Google Gemini 2.5 Flash LLM, via langchain-google-genai
πŸ”’ gemini-embedding-001 Document & query embeddings
πŸ—„οΈ ChromaDB Locally-persisted vector store
🌐 Tavily Real-time web search fallback
⚑ uv · pytest · black · isort Tooling, testing & formatting

πŸ“‚ Project Structure

langgraph-course/
β”œβ”€β”€ main.py                         # πŸš€ Entry point β€” invokes the compiled graph
β”œβ”€β”€ ingestion.py                    # πŸ“₯ Loads & chunks docs, builds the Chroma retriever
β”œβ”€β”€ graph/
β”‚   β”œβ”€β”€ graph.py                    # 🧩 The LangGraph state machine (nodes + edges)
β”‚   β”œβ”€β”€ state.py                    # πŸ“¦ GraphState (TypedDict) shared across nodes
β”‚   β”œβ”€β”€ consts.py                   # πŸ”– Node name constants
β”‚   β”œβ”€β”€ nodes/                      # βš™οΈ  Graph nodes
β”‚   β”‚   β”œβ”€β”€ retrieve.py             #     β”” fetch documents from the vector store
β”‚   β”‚   β”œβ”€β”€ grade_documents.py      #     β”” score document relevance (CRAG)
β”‚   β”‚   β”œβ”€β”€ generate.py             #     β”” produce the grounded answer
β”‚   β”‚   └── web_search.py           #     β”” Tavily fallback search
β”‚   └── chains/                     # πŸ”— Reusable LCEL chains
β”‚       β”œβ”€β”€ router.py               #     β”” route question β†’ vectorstore | websearch
β”‚       β”œβ”€β”€ generation.py           #     β”” RAG answer-generation chain
β”‚       β”œβ”€β”€ retrieval_grader.py     #     β”” "is this doc relevant?"
β”‚       β”œβ”€β”€ hallucination_grader.py #     β”” "is the answer grounded?"
β”‚       β”œβ”€β”€ answer_grader.py        #     β”” "does the answer resolve the question?"
β”‚       └── tests/
β”‚           └── test_chains.py      # βœ… Unit tests for every chain
└── pyproject.toml                  # πŸ“œ Dependencies & project metadata

πŸš€ Getting Started

1. Prerequisites

  • Python 3.11+
  • uv (recommended) β€” pip install uv
  • API keys for Google Gemini and Tavily (and optionally LangSmith)

2. Clone & Install

git clone https://github.com/Mohamedkhattab02/Agentic-RAG-with-LangGraph
cd langgraph-course

# Install dependencies into a virtual environment with uv
uv sync

Prefer pip? Run pip install -e . inside a virtual environment instead.

3. Configure Environment Variables

Create a .env file in the project root:

# --- Required ---
GOOGLE_API_KEY=your_google_gemini_api_key
TAVILY_API_KEY=your_tavily_api_key

# --- Optional: LangSmith tracing & observability ---
LANGSMITH_TRACING=true
LANGSMITH_ENDPOINT=https://api.smith.langchain.com
LANGSMITH_API_KEY=your_langsmith_api_key
LANGSMITH_PROJECT=advanced-rag

# --- Make local imports resolve ---
PYTHONPATH=.

πŸ”‘ Get your keys: Google AI Studio Β· Tavily Β· LangSmith

4. Ingest the Knowledge Base

The vector store is built from a set of Lilian Weng's essays on agents, prompt engineering, and adversarial attacks.

Open ingestion.py and uncomment the Chroma.from_documents(...) block, then run it once to populate ./.chroma:

uv run python ingestion.py

After the first run, re-comment that block so subsequent runs simply load the persisted store.

5. Run the Agent

uv run python main.py

You'll see the agent reason through the graph live in your terminal:

---ROUTE QUESTION---
---ROUTE QUESTION TO RAG---
---RETRIEVE---
---CHECK DOCUMENT RELEVANCE TO QUESTION---
---GRADE: DOCUMENT RELEVANT---
---ASSESS GRADED DOCUMENTS---
---DECISION: GENERATE---
---GENERATE---
---CHECK HALLUCINATIONS---
---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---
---GRADE GENERATION vs QUESTION---
---DECISION: GENERATION ADDRESSES QUESTION---

πŸ’» Usage Example

Customize the question in main.py:

from dotenv import load_dotenv
from graph.graph import app

load_dotenv()

if __name__ == "__main__":
    result = app.invoke(input={"question": "What is agent memory?"})
    print(result["generation"])

βœ… Testing

Every chain is covered by unit tests β€” routing, relevance grading, generation, and hallucination detection:

uv run pytest -s -v

⚠️ Tests make live LLM calls, so a valid GOOGLE_API_KEY and a populated vector store are required.


🧩 Key Design Patterns

  • Binary grading with structured output β€” Each grader uses Pydantic models + with_structured_output() for reliable, parseable decisions.
  • Conditional edges β€” The graph branches dynamically based on grader verdicts, enabling true self-correction loops.
  • Separation of concerns β€” Pure chains (logic) are decoupled from nodes (state I/O), keeping the graph readable and testable.

πŸ“œ License

Released under the MIT License β€” free to use, modify, and learn from.


Built with πŸ¦œπŸ”— LangChain Β· πŸ¦œπŸ•ΈοΈ LangGraph Β· ✨ Google Gemini



⭐ If this project helped you, consider giving it a star!

About

πŸ¦œπŸ”— Self-correcting Agentic RAG agent built with LangGraph β€” combines Adaptive RAG, Corrective RAG (CRAG) & Self-RAG. Routes queries to vector store or web search, grades document relevance, and detects hallucinations before answering. Powered by Gemini 2.5 Flash, ChromaDB & Tavily.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages