Skip to content

A self-reflective and adaptive RAG system that dynamically routes queries between web search and vector retrieval, assesses document relevance, checks for hallucinations, and ensures answer quality using a graph-based flow architecture.

License

Notifications You must be signed in to change notification settings

extrawest/langgraph_demo_app_with_self_reflective_rag

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 Self-Reflective RAG (Retrieval Augmented Generation)

Maintenance Maintainer Ask Me Anything ! License GitHub release

A self-reflective and adaptive RAG system that dynamically routes queries between web search and vector retrieval, assesses document relevance, checks for hallucinations, and ensures answer quality using a graph-based flow architecture.

📋 Features

Dynamic Query Routing

  • 🔄 Intelligently routes queries between vectorstore and web search
  • 🧠 Uses LLM to determine the best source for each query
  • 🌐 Falls back to web search for queries outside the knowledge base

Contextual Document Assessment

  • 📑 Grades document relevance before using them in answers
  • 🔍 Filters out irrelevant documents to improve context quality
  • 🔄 Can transform queries when relevant documents aren't found

Self-Critical Evaluation

  • 🕵️ Checks generated answers for hallucinations
  • ⚖️ Verifies that answers are grounded in retrieved documents
  • 🎯 Confirms that answers address the original questions

Adaptive Response System

  • 🔁 Regenerates answers when quality issues are detected
  • 🔄 Transforms queries when retrieval fails to find relevant info
  • 📈 Creates a feedback loop to improve response quality

🏗️ Architecture

The system is built with LangGraph and follows a workflow pattern:

Screenshot from 2025-04-25 12-26-07

🛠️ Requirements

  • Python 3.9+
  • OpenAI API key
  • Tavily API key
  • LangChain and LangGraph libraries

📦 Installation

# Install dependencies
pip install -r requirements.txt

# Set up environment variables
export OPENAI_API_KEY="your-openai-api-key"
export TAVILY_API_KEY="your-tavily-api-key"

🚀 Usage

import asyncio
from langgraph_adaptive_rag_simple import AdaptiveRAG, main

# Run the example
asyncio.run(main())

# Or use the system for your own queries
async def custom_query():
    rag_system = AdaptiveRAG()
    await rag_system.initialize()
    
    result = await rag_system.process_query("What are the pros and cons of RAG systems?")
    print(result)

asyncio.run(custom_query())

📊 Example Output

===== PROCESSING: 'What player are the Bears expected to draft first in the 2024 NFL draft?' =====

---ROUTE QUESTION---
---ROUTE QUESTION TO WEB SEARCH---
---WEB SEARCH---
Node: 'web_search'
---
---GENERATE---
---CHECK HALLUCINATIONS---
---DECISION: GENERATION IS GROUNDED IN DOCUMENTS---
---GRADE GENERATION vs QUESTION---
---DECISION: GENERATION ADDRESSES QUESTION---
Node: 'generate'
---

===== ANSWER =====
The Chicago Bears are expected to draft QB Caleb Williams first in the 2024 NFL draft. Williams is seen as the savior at quarterback that the Bears have always needed, with exceptional arm talent, mobility, and leadership skills.

🏆 Key Benefits

  • Accuracy: Self-critical evaluation prevents hallucinations
  • Flexibility: Handles both known topics and up-to-date information
  • Adaptability: Transforms approaches based on intermediate results
  • Scalability: Modular design allows easy extension with new components

Developed by extrawest. Software development company

About

A self-reflective and adaptive RAG system that dynamically routes queries between web search and vector retrieval, assesses document relevance, checks for hallucinations, and ensures answer quality using a graph-based flow architecture.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages