Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangGraph Agentic RAG

An advanced, self-correcting Retrieval-Augmented Generation (RAG) pipeline built with LangGraph and LangChain.

Instead of a naive "retrieve → generate" flow, this project models RAG as a stateful graph where each step can grade its own output and decide what to do next: route the question, grade retrieved documents, fall back to web search, and check the final answer for hallucinations and relevance before returning it.

Workflow

Agentic RAG graph

The graph orchestrates the following logic:

  1. Route question — A question router decides whether the query should hit the local vectorstore (topics about agents, prompt engineering, and adversarial attacks) or go straight to web search for everything else.
  2. Retrieve — Relevant documents are fetched from the Chroma vectorstore.
  3. Grade documents — Each retrieved document is graded for relevance. If any document is not relevant, the graph flags the need for a web search.
  4. Web search — When documents are insufficient (or the router chose this path), Tavily fetches fresh results from the web and appends them to the context.
  5. Generate — The LLM produces an answer grounded in the collected context.
  6. Grade generation — The answer is double-checked:
    • Hallucination grader: is the generation grounded in the documents? If not (not supported), regenerate.
    • Answer grader: does the generation actually address the question? If yes (useful), finish. If not (not useful), route to web search for more context.

Project structure

langgraph-agentic-rag/
├── main.py                 # Entry point: invokes the compiled graph
├── ingestion.py            # Loads, splits, and indexes documents into Chroma
├── graph/
│   ├── graph.py            # Graph definition: nodes, edges, conditional routing
│   ├── state.py            # GraphState (question, generation, web_search, documents)
│   ├── consts.py           # Node name constants
│   ├── nodes/              # Node implementations (retrieve, grade_documents, generate, web_search)
│   └── chains/             # LLM chains
│       ├── router.py               # Routes a question to vectorstore or web search
│       ├── retrieval_grader.py     # Grades document relevance
│       ├── generation.py           # Generates the final answer
│       ├── hallucination_grader.py # Checks the answer is grounded in documents
│       ├── answer_grader.py        # Checks the answer addresses the question
│       └── tests/                  # Pytest tests for the chains
└── graph.png               # Auto-generated visualization of the graph

Knowledge base

The vectorstore is seeded (via ingestion.py) with the following blog posts by Lilian Weng:

Tech stack

  • LangGraph — stateful graph orchestration
  • LangChain — LLM chains and prompts
  • OpenAI — embeddings and chat model (gpt-5-nano)
  • Chroma — local vector store
  • Tavily — web search tool
  • uv — dependency and environment management

Setup

This project uses uv for dependency management.

uv sync

Create a .env file in the project root with your API keys:

OPENAI_API_KEY=your_openai_key
TAVILY_API_KEY=your_tavily_key
LANGCHAIN_API_KEY=your_langsmith_key   # optional, for tracing
LANGCHAIN_TRACING_V2=true              # optional

Usage

First, ingest the documents into the vectorstore (uncomment the Chroma.from_documents block in ingestion.py for the initial run):

uv run python ingestion.py

Then run the agent:

uv run python main.py

Testing

The LLM chains are covered by pytest:

uv run pytest

About

An advanced, self-correcting Retrieval-Augmented Generation (RAG) pipeline built with LangGraph and LangChain

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages