How dhruvsinghal09/adaptive-rag works
Purpose and entry points
Adaptive RAG is a Python agentic retrieval-augmented chat system. It accepts queries and document uploads through FastAPI, optionally via a Streamlit client, then routes questions to indexed-document retrieval, an OpenAI general-knowledge response, or Tavily web search.
• Backend ASGI entry point: src/main.py
• REST boundary: src/api/routes.py
• Streamlit entry/client: streamlit_app/home.py, streamlit_app/pages/chat.py, streamlit_app/utils/api_client.py
API and request boundary
FastAPI exposes POST /rag/query and POST /rag/documents/upload. Query payloads carry a query and session identifier; upload accepts PDF/TXT plus description. API handlers should remain the boundary between untrusted HTTP input and RAG orchestration.
• src/api/routes.py
• src/models/query_request.py
• src/models/state.py
Document ingestion and indexing
Upload processing parses supported documents, chunks content, embeds it, and persists vectors for later semantic retrieval. The vector-store setup is isolated from workflow nodes, allowing Qdrant/FAISS-style retrieval implementation changes without changing the API or graph contract.
• src/rag/document_upload.py
• src/rag/retriever_setup.py
• src/api/routes.py
LangGraph adaptive query workflow
The core control plane is a LangGraph state machine. It classifies each query as index, general, or search, then conditionally executes the relevant branch. Indexed queries retrieve documents, grade relevance, and may rewrite/retry the query before generation; general and search branches feed their respective context into answer generation.
• Graph assembly: src/rag/graph_builder.py
• Node implementations: src/rag/nodes.py
• Conditional routing decisions: src/tools/graph_tools.py
• Shared graph state: src/models/state.py
LLM, agent, and tool boundary
OpenAI provides classification, relevance grading, query rewrite, and response generation via centrally defined prompts. A ReAct agent encapsulates reasoning/action behavior and tool use; common/graph tools support retrieval and web-search decisions. Prompt edits alter behavioral policy without changing graph topology.
• src/llms/openai.py
• src/rag/reAct_agent.py
• src/config/prompts.yaml
• src/tools/common_tools.py
Persistence and conversation context
Conversation history is keyed by session and persisted in MongoDB, with an in-memory alternative. This memory layer is separate from document vectors: MongoDB retains conversational context, while Qdrant holds embedded document chunks.
• src/memory/chat_history_mongo.py
• src/memory/chathistory_in_memory.py
• src/db/mongo_client.py
Runtime and external infrastructure
The service runs on Python 3.9+ with FastAPI/Uvicorn and LangChain/LangGraph; Streamlit is an optional separate UI process. Required external dependencies are OpenAI, Tavily for current-web search, Qdrant for vector search, and MongoDB for persistent sessions. Environment-backed settings centralize their endpoints, credentials, and collection names.
• src/config/settings.py
• src/core/config.py
• requirements.txt
Main entry points: routes.py RAG routes · graph_builder.py LangGraph assembly · openai.py OpenAI LLM · settings.py Runtime settings
Components 19
Clients
- Streamlit client · Optional chat and upload interface.
streamlit_app/home.py - API client
streamlit_app/utils/api_client.py
API Service
- FastAPI ASGI app
src/main.py - RAG routes · Accepts query and document-upload requests.
src/api/routes.py - Query request model
src/models/query_request.py - Document ingestion · Parses, chunks, embeds, and stores uploads.
src/rag/document_upload.py
Adaptive RAG Workflow
- LangGraph assembly
src/rag/graph_builder.py - Shared graph state · Carries query, context, and session data through the graph.
src/models/state.py - Workflow nodes · Retrieval, grading, rewrite, and answer-generation steps.
src/rag/nodes.py - Conditional routing · Routes index, general, and web-search branches.
src/tools/graph_tools.py - ReAct agent · Encapsulates reasoning and tool use.
src/rag/reAct_agent.py
Data & Infrastructure
- OpenAI LLM · Classifies, grades, rewrites, and generates answers.
src/llms/openai.py - Prompt policy
src/config/prompts.yaml - Retriever setup · Isolates vector backend setup from workflow nodes.
src/rag/retriever_setup.py - Qdrant vectors · Stores embedded document chunks.
- Tavily web search
- Chat history · MongoDB-backed history with an in-memory alternative.
src/memory/chat_history_mongo.py - MongoDB · Persists conversation history by session.
src/db/mongo_client.py - Runtime settings · Centralizes external endpoints, credentials, and collection names.
src/config/settings.py
Connections 22
- Streamlit client → API client: uses
- API client → FastAPI ASGI app: HTTP
- FastAPI ASGI app → RAG routes: serves
- RAG routes → Query request model: validates query
- RAG routes → LangGraph assembly: invokes query workflow
- RAG routes → Document ingestion: dispatches upload
- Document ingestion → Retriever setup: indexes chunks
- Retriever setup → Qdrant vectors: persists vectors
- LangGraph assembly → Shared graph state: uses
- LangGraph assembly → Conditional routing: selects branches
- Conditional routing → Workflow nodes: index branch
- Conditional routing → ReAct agent: general or search branch
- Workflow nodes → Retriever setup: retrieves context
- Workflow nodes → OpenAI LLM: grades and generates
- ReAct agent → OpenAI LLM: reasons with
- ReAct agent → Tavily web search: searches web
- OpenAI LLM → Prompt policy: uses policy
- LangGraph assembly → Chat history: loads and saves context
- Chat history → MongoDB: persists sessions
- Runtime settings → OpenAI LLM: configures
- Runtime settings → Qdrant vectors: configures
- Runtime settings → MongoDB: configures