An advanced Retrieval Augmented Generation (RAG) system that dynamically adapts to user queries through intelligent routing, document grading, and query transformation.
- Adaptive Routing: Intelligently routes queries between vectorstore and web search
- Document Relevance Grading: Filters retrieved documents based on relevance
- Query Transformation: Reformulates queries to improve retrieval quality
- Hallucination Detection: Ensures generated responses are grounded in retrieved context
- Answer Quality Assessment: Verifies that responses properly address the original query
- LangGraph Workflow: Orchestrated with a flexible state machine workflow
- Multiple UI Options: Choose between Streamlit, Gradio, or Flask interfaces
- Python 3.9+
- API keys for:
- OpenAI (required)
- Tavily (for web search)
- LangSmith (for tracing, optional)
- Clone this repository:
git clone https://github.com/affanrasheed/Adaptive_RAG.git
cd Adaptive_RAG- Install dependencies:
pip install -e .- Create a
.envfile based on the provided.env.example:
cp .env.example .env- Edit the
.envfile with your API keys:
OPENAI_API_KEY=your_openai_api_key
TAVILY_API_KEY=your_tavily_api_key
LANGSMITH_API_KEY=your_langsmith_api_key
python examples/simple_question.py# Launch the default Streamlit UI
python ui/launch_ui.py
# Or specify a UI type
python ui/launch_ui.py --ui gradio
python ui/launch_ui.py --ui flaskfrom adaptive_rag import AdaptiveRAG
# Initialize the RAG system
rag = AdaptiveRAG()
# Run a query
response = rag.query("What are the types of agent memory?")
print(response)The system is modular and can be customized:
from adaptive_rag import AdaptiveRAG
from adaptive_rag.components import CustomRetriever, CustomGrader
# Create a custom RAG system
custom_rag = AdaptiveRAG(
retriever=CustomRetriever(),
document_grader=CustomGrader(),
model="gpt-4"
)The system uses a LangGraph workflow to coordinate components:
- Routing: Decides between vectorstore retrieval or web search
- Retrieval: Gets relevant documents from the chosen source
- Grading: Assesses document relevance to the query
- Generation: Produces an answer based on relevant documents
- Evaluation: Checks for hallucinations and response quality
This project is licensed under the MIT License - see the LICENSE file for details.

