# arjun-bhattarai/rag-with-langchain architecture

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

- Interactive diagram: https://gitdiagram.com/arjun-bhattarai/rag-with-langchain
- Repository: https://github.com/arjun-bhattarai/rag-with-langchain

## Overview

#### Purpose and Entry Points
Arjunx is a modular Python Retrieval-Augmented Generation assistant: it transforms a question, selects local/web sources, evaluates evidence, and generates a grounded answer through a local Ollama model. The HTTP entry point is `api/main.py`; `POST /chat/` is defined in `api/routes/chat.py` and delegates orchestration to `api/services/rag_service.py`. A lightweight browser client is rooted at `frontend/index.html` with behavior in `frontend/script.js`.

#### Application Boundary
FastAPI validates request/response payloads through `api/schemas/chat.py`, while the service layer isolates API concerns from the RAG implementation in `api/services/rag_service.py`. This boundary should remain the integration seam for alternate clients, streaming, authentication, or deployment concerns; pipeline modules should not require HTTP types.

#### Pipeline Orchestration and Generation
The integrated flow is centered on `src/pipeline/rag_pipeline.py`, with fundamental indexing, retrieval, and generation stages split across `src/pipeline/indexing_rag.py`, `src/pipeline/retrieval_rag.py`, and `src/pipeline/generation_rag.py`. Shared model/configuration abstractions reside in `src/core/models.py` and `src/core/config.py`. Generation ultimately targets Ollama/local Llama inference, mediated by LangChain-oriented RAG components.

#### Query Understanding and Source Selection
Before retrieval, query translation can create alternative retrieval formulations—multi-query, fusion, decomposition, HyDE, and step-back—in `src/query_translation/query_processor.py`, `src/query_translation/multi_query.py`, and `src/query_translation/rag_fusion.py`. Query structuring lives in `src/query_structuring/query_structuring.py`. Routing decides local, web, or hybrid retrieval via `src/routing/source_routing.py`, supported by logical and semantic policies in `src/routing/logical_routing.py` and `src/routing/semantic_routing.py`.

#### Retrieval and Indexing Boundary
Local retrieval implementations are separated from remote-source adapters: `src/retrieval/chroma.py` uses ChromaDB vector storage, while `src/retrieval/web.py` accesses web knowledge. External-source provider modules are `data_source/web_search.py`, `data_source/wikipedia.py`, and `data_source/personal_website.py`; Tavily and Wikipedia are material remote dependencies. Hybrid retrieval is composed in `src/retrieval/hybrid.py`. Advanced retrieval/indexing options include RAPTOR and ColBERT under `src/retrieval/raptor.py`, `src/advanced_indexing/raptor.py`, and `src/advanced_indexing/colbert.py`.

#### Evidence Quality and Adaptive Correction
Retrieved documents pass through evidence-quality stages: duplicate/overlap removal in `src/utils/deduplication.py`, relevance reranking in `src/Reranking/reranking.py`, and temporal prioritization in `src/Reranking/freshness.py`. Corrective and self-evaluating decisions are implemented in `src/advanced_RAG/CRAG/crag.py` and `src/advanced_RAG/Self_RAG/self_rag.py`; insufficient evidence can trigger more retrieval or yield `NOT_ANSWERABLE`. Context scaling is handled by `src/advanced_RAG/Long_Context/long_context.py`, and evidence representation/support utilities are in `src/utils/evidence.py`.

#### State, Evaluation, and Experimentation
Conversation/summarization/vector-memory capabilities are consolidated in `src/memory/memory.py`, providing state that can enrich later requests. Pipeline quality assessment is represented by `src/evaluation/rag_evaluation.py`. The production-oriented source tree is complemented by exploratory notebooks, especially `integration/integration.py` and `integration/integrated_rag.ipynb`, which document and exercise assembly of the techniques without being the application runtime.

Main entry points: [`src/pipeline/retrieval_rag.py`](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/retrieval_rag.py) (Retrieval stage), [`src/pipeline/rag_pipeline.py`](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/rag_pipeline.py) (RAG orchestrator), [`api/routes/chat.py`](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/routes/chat.py) (POST /chat), [`src/retrieval/hybrid.py`](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/hybrid.py) (Hybrid retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_browser["Browser client<br/>static client<br/>[index.html]"]
  node_browser_logic["Chat UI behavior<br/>browser script<br/>[script.js]"]
end

subgraph group_api["HTTP boundary"]
  node_api_app["FastAPI app<br/>HTTP entry point<br/>[main.py]"]
  node_chat_route["POST /chat<br/>route handler<br/>[chat.py]"]
  node_chat_schema["Chat schemas<br/>request/response DTOs<br/>[chat.py]"]
  node_rag_service["RAG service<br/>application service<br/>[rag_service.py]"]
end

subgraph group_pipeline["RAG pipeline"]
  node_rag_pipeline["RAG orchestrator<br/>pipeline coordinator<br/>[rag_pipeline.py]"]
  node_query_processing["Query translation<br/>query expansion<br/>[query_processor.py]"]
  node_query_structuring["Query structuring<br/>query parser"]
  node_source_routing{{"Source routing<br/>retrieval policy<br/>[source_routing.py]"}}
  node_indexing["Indexing stage<br/>pipeline stage<br/>[indexing_rag.py]"]
  node_generation["Grounded generation<br/>pipeline stage<br/>[generation_rag.py]"]
  node_model_core["Model abstractions<br/>shared model layer<br/>[models.py]"]
end

subgraph group_retrieval["Retrieval and evidence"]
  node_retrieval_stage["Retrieval stage<br/>pipeline stage<br/>[retrieval_rag.py]"]
  node_local_retrieval[("Local vector retrieval<br/>Chroma adapter<br/>[chroma.py]")]
  node_web_retrieval["Web retrieval<br/>remote-source adapter<br/>[web.py]"]
  node_hybrid_retrieval["Hybrid retrieval<br/>retrieval composer<br/>[hybrid.py]"]
  node_evidence_quality["Evidence quality<br/>dedupe and reranking<br/>[deduplication.py]"]
  node_adaptive_rag{{"Corrective RAG<br/>evidence decision<br/>[crag.py]"}}
end

subgraph group_state["State and quality"]
  node_memory[("Conversation memory<br/>state store<br/>[memory.py]")]
  node_evaluation["RAG evaluation<br/>quality assessment<br/>[rag_evaluation.py]"]
end

node_browser -->|"loads"| node_browser_logic
node_browser_logic -->|"POST /chat"| node_chat_route
node_api_app -->|"mounts"| node_chat_route
node_chat_route -->|"validates"| node_chat_schema
node_chat_route -->|"delegates"| node_rag_service
node_rag_service -->|"invokes"| node_rag_pipeline
node_rag_pipeline -->|"expands query"| node_query_processing
node_query_processing -->|"structures"| node_query_structuring
node_query_structuring -->|"routes"| node_source_routing
node_rag_pipeline -->|"indexes corpus"| node_indexing
node_indexing -->|"builds local index"| node_local_retrieval
node_source_routing -->|"selects retrieval"| node_retrieval_stage
node_retrieval_stage -->|"local"| node_local_retrieval
node_retrieval_stage -->|"web"| node_web_retrieval
node_retrieval_stage -->|"hybrid"| node_hybrid_retrieval
node_hybrid_retrieval -->|"combines"| node_local_retrieval
node_hybrid_retrieval -->|"combines"| node_web_retrieval
node_retrieval_stage -->|"documents"| node_evidence_quality
node_evidence_quality -->|"assesses evidence"| node_adaptive_rag
node_adaptive_rag -.->|"retry if needed"| node_retrieval_stage
node_adaptive_rag -->|"supported context"| node_generation
node_generation -->|"uses models"| node_model_core
node_rag_pipeline -->|"reads and updates"| node_memory
node_rag_pipeline -.->|"assesses quality"| node_evaluation

click node_browser "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/frontend/index.html"
click node_browser_logic "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/frontend/script.js"
click node_api_app "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/main.py"
click node_chat_route "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/routes/chat.py"
click node_chat_schema "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/schemas/chat.py"
click node_rag_service "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/services/rag_service.py"
click node_rag_pipeline "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/rag_pipeline.py"
click node_query_processing "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/query_translation/query_processor.py"
click node_query_structuring "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/query_structuring/query_structuring.py"
click node_source_routing "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/routing/source_routing.py"
click node_indexing "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/indexing_rag.py"
click node_retrieval_stage "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/retrieval_rag.py"
click node_local_retrieval "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/chroma.py"
click node_web_retrieval "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/web.py"
click node_hybrid_retrieval "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/hybrid.py"
click node_evidence_quality "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/utils/deduplication.py"
click node_adaptive_rag "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/advanced_RAG/CRAG/crag.py"
click node_generation "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/generation_rag.py"
click node_model_core "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/core/models.py"
click node_memory "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/memory/memory.py"
click node_evaluation "https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/evaluation/rag_evaluation.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_browser,node_browser_logic toneBlue
class node_api_app,node_chat_route,node_chat_schema,node_rag_service toneAmber
class node_rag_pipeline,node_query_processing,node_query_structuring,node_source_routing,node_indexing,node_generation,node_model_core toneMint
class node_retrieval_stage,node_local_retrieval,node_web_retrieval,node_hybrid_retrieval,node_evidence_quality,node_adaptive_rag toneRose
class node_memory,node_evaluation toneIndigo
```

## Components

### Client

- [Browser client](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/frontend/index.html): static client · `frontend/index.html`
- [Chat UI behavior](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/frontend/script.js): browser script · `frontend/script.js`

### HTTP boundary

- [FastAPI app](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/main.py): HTTP entry point · `api/main.py`
- [POST /chat](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/routes/chat.py): route handler · `api/routes/chat.py`
- [Chat schemas](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/schemas/chat.py): request/response DTOs · `api/schemas/chat.py`
- [RAG service](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/api/services/rag_service.py): application service · Keeps HTTP types outside the RAG implementation. · `api/services/rag_service.py`

### RAG pipeline

- [RAG orchestrator](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/rag_pipeline.py): pipeline coordinator · `src/pipeline/rag_pipeline.py`
- [Query translation](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/query_translation/query_processor.py): query expansion · Supports multi-query, fusion, decomposition, HyDE, and step-back formulations. · `src/query_translation/query_processor.py`
- [Query structuring](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/query_structuring/query_structuring.py): query parser · `src/query_structuring/query_structuring.py`
- [Source routing](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/routing/source_routing.py): retrieval policy · Chooses local, web, or hybrid retrieval. · `src/routing/source_routing.py`
- [Indexing stage](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/indexing_rag.py): pipeline stage · `src/pipeline/indexing_rag.py`
- [Grounded generation](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/generation_rag.py): pipeline stage · Generates through LangChain-oriented components and local Ollama/Llama inference. · `src/pipeline/generation_rag.py`
- [Model abstractions](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/core/models.py): shared model layer · `src/core/models.py`

### Retrieval and evidence

- [Retrieval stage](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/pipeline/retrieval_rag.py): pipeline stage · `src/pipeline/retrieval_rag.py`
- [Local vector retrieval](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/chroma.py): Chroma adapter · `src/retrieval/chroma.py`
- [Web retrieval](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/web.py): remote-source adapter · Uses web knowledge providers including Tavily and Wikipedia. · `src/retrieval/web.py`
- [Hybrid retrieval](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/retrieval/hybrid.py): retrieval composer · `src/retrieval/hybrid.py`
- [Evidence quality](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/utils/deduplication.py): dedupe and reranking · Removes overlap, reranks relevance, and prioritizes fresh evidence. · `src/utils/deduplication.py`
- [Corrective RAG](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/advanced_RAG/CRAG/crag.py): evidence decision · Can retry retrieval or return NOT_ANSWERABLE when evidence is insufficient. · `src/advanced_RAG/CRAG/crag.py`

### State and quality

- [Conversation memory](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/memory/memory.py): state store · Conversation, summary, and vector memory enrich later requests. · `src/memory/memory.py`
- [RAG evaluation](https://github.com/arjun-bhattarai/rag-with-langchain/blob/main/src/evaluation/rag_evaluation.py): quality assessment · `src/evaluation/rag_evaluation.py`

## Connections

- Browser client → Chat UI behavior: loads
- Chat UI behavior → POST /chat: POST /chat
- FastAPI app → POST /chat: mounts
- POST /chat → Chat schemas: validates
- POST /chat → RAG service: delegates
- RAG service → RAG orchestrator: invokes
- RAG orchestrator → Query translation: expands query
- Query translation → Query structuring: structures
- Query structuring → Source routing: routes
- RAG orchestrator → Indexing stage: indexes corpus
- Indexing stage → Local vector retrieval: builds local index
- Source routing → Retrieval stage: selects retrieval
- Retrieval stage → Local vector retrieval: local
- Retrieval stage → Web retrieval: web
- Retrieval stage → Hybrid retrieval: hybrid
- Hybrid retrieval → Local vector retrieval: combines
- Hybrid retrieval → Web retrieval: combines
- Retrieval stage → Evidence quality: documents
- Evidence quality → Corrective RAG: assesses evidence
- Corrective RAG → Retrieval stage: retry if needed
- Corrective RAG → Grounded generation: supported context
- Grounded generation → Model abstractions: uses models
- RAG orchestrator → Conversation memory: reads and updates
- RAG orchestrator → RAG evaluation: assesses quality
