Skip to content

An adaptive Retrieval-Augmented Generation (RAG) system that dynamically routes queries between vector search and web search for accurate, grounded answers.

License

Notifications You must be signed in to change notification settings

vivek34561/Adaptive-RAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

39 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

title emoji colorFrom colorTo sdk app_file pinned
Adaptive RAG Chat
๐Ÿง 
yellow
red
streamlit
app.py
false

๐Ÿš€ Adaptive-RAG โ€“ Intelligent Retrieval-Augmented Generation System

Python LangGraph LangChain Streamlit License

An adaptive Retrieval-Augmented Generation (RAG) system that dynamically routes queries between vector search and web search for accurate, grounded answers.

โœจ Features โ€ข ๐Ÿ—๏ธ Architecture โ€ข ๐Ÿš€ Quick Start โ€ข ๐Ÿ”ฌ How It Works โ€ข ๐Ÿ“‚ Project Structure


๐ŸŽฏ Overview

Adaptive-RAG is a production-oriented RAG pipeline built using LangChain and LangGraph that intelligently decides how to retrieve information before generating an answer.

Instead of relying on a single data source, the system:

  • Routes questions to a local FAISS vectorstore when internal knowledge is sufficient
  • Falls back to web search (Tavily) when information is missing or outdated
  • Grades retrieved documents and generated answers
  • Automatically rewrites queries when retrieval quality is poor

This results in more accurate, less hallucinated, and context-aware answers.


๐Ÿ’ก Why I Built This

The Problem: Traditional RAG systems blindly retrieve documents from a vectorstore, even when:

  • The question is out of domain
  • Knowledge is outdated
  • Retrieval quality is poor

This leads to hallucinations or incomplete answers.

My Goal: Build a self-correcting RAG pipeline that can:

  • Decide where to retrieve from
  • Judge how good the retrieval is
  • Improve itself by rewriting queries when needed

Key Learning: LangGraph is ideal for building conditional, feedback-driven RAG workflows instead of linear chains.


โœจ Key Features

  • Adaptive Routing Automatically selects between vectorstore retrieval and web search.

  • Retrieval Grading Evaluates whether retrieved documents are relevant to the query.

  • Hallucination Detection Grades generated answers against retrieved context.

  • Query Rewriting Reformulates weak questions to improve retrieval quality.

  • Graph-based Control Flow Uses LangGraph for explicit decision-making and retry loops.

  • Interactive UI Streamlit app for real-time querying and visualization.


๐Ÿ—๏ธ Architecture

High-Level Workflow

User Query
   โ”‚
   โ–ผ
Router Node
(Vectorstore or Web?)
   โ”‚
   โ”œโ”€โ”€โ–บ Vectorstore Retrieval (FAISS)
   โ”‚
   โ””โ”€โ”€โ–บ Web Search (Tavily)
           โ”‚
           โ–ผ
Document Grader
   โ”‚
   โ”œโ”€โ”€ Relevant โ†’ Answer Generator
   โ”‚
   โ””โ”€โ”€ Not Relevant โ†’ Query Rewriter
                            โ”‚
                            โ””โ”€โ”€ Loop Back to Router

LangGraph Advantage

LangGraph enables:

  • Conditional edges
  • Feedback loops
  • Explicit state transitions
  • Clean separation of logic

๐Ÿ”ฌ How It Works

1. Routing

A router node decides whether the query should go to:

  • Vectorstore (domain-specific questions)
  • Web Search (open-ended or current topics)

2. Retrieval

  • Vectorstore uses FAISS embeddings
  • Web search uses Tavily API

3. Grading

LLM-based graders check:

  • Document relevance
  • Answer grounding
  • Hallucination risk

4. Query Rewriting

If retrieval is weak, the query is rewritten and re-routed automatically.


๐Ÿ› ๏ธ Tech Stack

Component Technology
Orchestration LangGraph
RAG Framework LangChain
Vector Store FAISS
Web Search Tavily
LLM Groq (Llama 3.3)
UI Streamlit
Language Python

๐Ÿš€ Quick Start

Prerequisites

  • Python 3.10+
  • Groq API key
  • Tavily API key

Environment Setup

Create a .env file:

GROQ_API_KEY=your_groq_key
TAVILY_API_KEY=your_tavily_key

Install Dependencies

pip install -r requirements.txt

Run the App

streamlit run app.py

On first run:

  • A FAISS index is built
  • Cached under data/index/faiss_index

๐Ÿ“‚ Project Structure (this repo)

.
โ”œโ”€โ”€ app.py                     # Streamlit UI that invokes the LangGraph workflow
โ”œโ”€โ”€ requirements.txt           # Python dependencies
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ graphs/graph_builder.py  # FAISS index + retriever setup
โ”‚   โ”œโ”€โ”€ llms/llm.py              # RAG prompt and LLM chain
โ”‚   โ”œโ”€โ”€ nodes/node_implementation.py # Router, retrieve, web_search, graders, transform
โ”‚   โ””โ”€โ”€ states/state.py          # Graph state + compile (app)
โ””โ”€โ”€ data/faiss_index/          # Vectorstore cache (created at runtime)

๐Ÿš€ Deploy to Hugging Face Spaces

  1. Create a new Space: choose SDK "Streamlit".
  2. Push this repository to the Space (or connect via GitHub).
  3. In the Space Settings โ†’ Secrets, add:
  • GROQ_API_KEY: your Groq key
  • TAVILY_API_KEY: your Tavily key
  1. The app auto-builds using requirements.txt and runs app.py.
  2. Optional: users can also paste keys in the sidebar if Secrets are not set.

Notes:

  • First run may build a FAISS index and cache under src/data/faiss_index/.
  • If web search is disabled (missing Tavily key), queries will route to the vectorstore.

๐Ÿ“Š What Makes This Project Strong

  • Not a basic RAG demo
  • Uses decision-making and self-correction
  • Demonstrates real LangGraph value
  • Clean separation of concerns
  • Directly aligned with industry GenAI systems

This is the kind of RAG system used in:

  • Enterprise knowledge assistants
  • AI support bots
  • Research copilots
  • Internal search tools

๐Ÿ”ฎ Future Improvements

  • Multi-vector routing (code, docs, FAQs)
  • Tool-augmented RAG
  • Caching and latency optimization
  • Confidence-based answer refusal
  • Evaluation dashboards

๐Ÿ‘จโ€๐Ÿ’ป Author

Vivek Kumar Gupta AI Engineering Student | GenAI & Agentic Systems Builder


๐Ÿ“„ License

MIT License ยฉ 2025 Vivek Kumar Gupta

About

An adaptive Retrieval-Augmented Generation (RAG) system that dynamically routes queries between vector search and web search for accurate, grounded answers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published