# dhruvsinghal09/adaptive-rag architecture

> Architecture diagram of the GitHub repository dhruvsinghal09/adaptive-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhruvsinghal09/adaptive-rag
- Repository: https://github.com/dhruvsinghal09/adaptive-rag

## Overview

#### 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: [`src/api/routes.py`](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/api/routes.py) (RAG routes), [`src/rag/graph_builder.py`](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/graph_builder.py) (LangGraph assembly), [`src/llms/openai.py`](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/llms/openai.py) (OpenAI LLM), [`src/config/settings.py`](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/config/settings.py) (Runtime settings)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Clients"]
  node_streamlit["Streamlit client<br/>UI client<br/>[home.py]"]
  node_api_client["API client<br/>HTTP client<br/>[api_client.py]"]
end

subgraph group_api["API Service"]
  node_asgi["FastAPI ASGI app<br/>service entry<br/>[main.py]"]
  node_routes["RAG routes<br/>REST boundary<br/>[routes.py]"]
  node_query_model["Query request model<br/>request schema<br/>[query_request.py]"]
  node_upload["Document ingestion<br/>ingestion pipeline<br/>[document_upload.py]"]
end

subgraph group_workflow["Adaptive RAG Workflow"]
  node_graph_builder{{"LangGraph assembly<br/>workflow definition<br/>[graph_builder.py]"}}
  node_state["Shared graph state<br/>workflow state<br/>[state.py]"]
  node_nodes["Workflow nodes<br/>RAG operations<br/>[nodes.py]"]
  node_routing["Conditional routing<br/>graph decisions<br/>[graph_tools.py]"]
  node_agent["ReAct agent<br/>agent runtime<br/>[reAct_agent.py]"]
end

subgraph group_data["Data &amp; Infrastructure"]
  node_openai["OpenAI LLM<br/>LLM integration<br/>[openai.py]"]
  node_prompts["Prompt policy<br/>prompt configuration<br/>[prompts.yaml]"]
  node_retriever["Retriever setup<br/>vector-store adapter<br/>[retriever_setup.py]"]
  node_qdrant[("Qdrant vectors<br/>vector database")]
  node_tavily["Tavily web search<br/>search API"]
  node_chat_memory["Chat history<br/>conversation memory"]
  node_mongodb[("MongoDB<br/>session database<br/>[mongo_client.py]")]
  node_settings["Runtime settings<br/>environment configuration<br/>[settings.py]"]
end

node_streamlit -->|"uses"| node_api_client
node_api_client -->|"HTTP"| node_asgi
node_asgi -->|"serves"| node_routes
node_routes -->|"validates query"| node_query_model
node_routes -->|"invokes query workflow"| node_graph_builder
node_routes -->|"dispatches upload"| node_upload
node_upload -->|"indexes chunks"| node_retriever
node_retriever -->|"persists vectors"| node_qdrant
node_graph_builder -->|"uses"| node_state
node_graph_builder -->|"selects branches"| node_routing
node_routing -->|"index branch"| node_nodes
node_routing -->|"general or search branch"| node_agent
node_nodes -->|"retrieves context"| node_retriever
node_nodes -->|"grades and generates"| node_openai
node_agent -->|"reasons with"| node_openai
node_agent -->|"searches web"| node_tavily
node_openai -->|"uses policy"| node_prompts
node_graph_builder -->|"loads and saves context"| node_chat_memory
node_chat_memory -->|"persists sessions"| node_mongodb
node_settings -.->|"configures"| node_openai
node_settings -.->|"configures"| node_qdrant
node_settings -.->|"configures"| node_mongodb

click node_streamlit "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/streamlit_app/home.py"
click node_api_client "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/streamlit_app/utils/api_client.py"
click node_asgi "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/main.py"
click node_routes "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/api/routes.py"
click node_query_model "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/models/query_request.py"
click node_upload "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/document_upload.py"
click node_graph_builder "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/graph_builder.py"
click node_state "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/models/state.py"
click node_nodes "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/nodes.py"
click node_routing "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/tools/graph_tools.py"
click node_agent "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/reAct_agent.py"
click node_openai "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/llms/openai.py"
click node_prompts "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/config/prompts.yaml"
click node_retriever "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/retriever_setup.py"
click node_chat_memory "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/memory/chat_history_mongo.py"
click node_mongodb "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/db/mongo_client.py"
click node_settings "https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/config/settings.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_streamlit,node_api_client toneBlue
class node_asgi,node_routes,node_query_model,node_upload toneAmber
class node_graph_builder,node_state,node_nodes,node_routing,node_agent toneMint
class node_openai,node_prompts,node_retriever,node_qdrant,node_tavily,node_chat_memory,node_mongodb,node_settings toneRose
```

## Components

### Clients

- [Streamlit client](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/streamlit_app/home.py): UI client · Optional chat and upload interface. · `streamlit_app/home.py`
- [API client](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/streamlit_app/utils/api_client.py): HTTP client · `streamlit_app/utils/api_client.py`

### API Service

- [FastAPI ASGI app](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/main.py): service entry · `src/main.py`
- [RAG routes](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/api/routes.py): REST boundary · Accepts query and document-upload requests. · `src/api/routes.py`
- [Query request model](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/models/query_request.py): request schema · `src/models/query_request.py`
- [Document ingestion](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/document_upload.py): ingestion pipeline · Parses, chunks, embeds, and stores uploads. · `src/rag/document_upload.py`

### Adaptive RAG Workflow

- [LangGraph assembly](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/graph_builder.py): workflow definition · `src/rag/graph_builder.py`
- [Shared graph state](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/models/state.py): workflow state · Carries query, context, and session data through the graph. · `src/models/state.py`
- [Workflow nodes](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/nodes.py): RAG operations · Retrieval, grading, rewrite, and answer-generation steps. · `src/rag/nodes.py`
- [Conditional routing](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/tools/graph_tools.py): graph decisions · Routes index, general, and web-search branches. · `src/tools/graph_tools.py`
- [ReAct agent](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/reAct_agent.py): agent runtime · Encapsulates reasoning and tool use. · `src/rag/reAct_agent.py`

### Data & Infrastructure

- [OpenAI LLM](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/llms/openai.py): LLM integration · Classifies, grades, rewrites, and generates answers. · `src/llms/openai.py`
- [Prompt policy](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/config/prompts.yaml): prompt configuration · `src/config/prompts.yaml`
- [Retriever setup](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/rag/retriever_setup.py): vector-store adapter · Isolates vector backend setup from workflow nodes. · `src/rag/retriever_setup.py`
- **Qdrant vectors**: vector database · Stores embedded document chunks.
- **Tavily web search**: search API
- [Chat history](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/memory/chat_history_mongo.py): conversation memory · MongoDB-backed history with an in-memory alternative. · `src/memory/chat_history_mongo.py`
- [MongoDB](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/db/mongo_client.py): session database · Persists conversation history by session. · `src/db/mongo_client.py`
- [Runtime settings](https://github.com/dhruvsinghal09/adaptive-rag/blob/main/src/config/settings.py): environment configuration · Centralizes external endpoints, credentials, and collection names. · `src/config/settings.py`

## Connections

- 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
