# sourangshupal/agentic-rag-project architecture

> Architecture diagram of the GitHub repository sourangshupal/agentic-rag-project, 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/sourangshupal/agentic-rag-project
- Repository: https://github.com/sourangshupal/agentic-rag-project

## Overview

#### Purpose and Entry Points
This Python 3.12+ system curates arXiv research papers into a hybrid-search corpus and exposes conventional and agentic RAG APIs. FastAPI starts from `src/main.py`; route composition is in `src/routers/ask.py`, `src/routers/hybrid_search.py`, and `src/routers/agentic_ask.py`. Additional user channels are the Gradio app (`src/gradio_app.py`, `gradio_launcher.py`), Telegram integration (`src/services/telegram/bot.py`), and an MCP server (`src/mcp_server/server.py`).

#### API Composition and Dependency Boundary
`src/config.py` centralizes environment-backed settings. `src/dependencies.py` assembles service dependencies/factories for routers, keeping HTTP handlers separate from infrastructure clients. Request/response contracts live under `src/schemas/api/`; persistence domain objects and repositories are separated in `src/models/paper.py`, `src/repositories/paper.py`, and `src/db/interfaces/`.

Primary synchronous API flow is: request router → dependency-provided services → retrieval/cache/LLM integrations → typed response or SSE stream. Middleware and error shaping reside in `src/middlewares.py` and `src/exceptions.py`.

#### Corpus Ingestion Pipeline
Scheduled ingestion is orchestrated by Airflow’s DAG entry point `airflow/dags/arxiv_paper_ingestion.py`, with stages split across `airflow/dags/arxiv_ingestion/fetching.py`, `indexing.py`, and `reporting.py`. The pipeline fetches arXiv metadata/PDFs, parses scientific documents through Docling, chunks content, embeds it, and indexes searchable chunks.

Application-side boundaries are explicit: arXiv access in `src/services/arxiv/client.py`; document extraction in `src/services/pdf_parser/docling.py`; and indexing orchestration in `src/services/indexing/hybrid_indexer.py`. Paper metadata is persisted through PostgreSQL abstractions while chunk retrieval is served from OpenSearch.

#### Search and RAG Path
Hybrid retrieval combines BM25 and dense-vector similarity through OpenSearch query construction and RRF-style fusion. Core components are `src/services/opensearch/client.py`, `src/services/opensearch/query_builder.py`, and `src/services/opensearch/index_config_hybrid.py`. Text preparation and embeddings are isolated in `src/services/indexing/text_chunker.py` and `src/services/embeddings/jina_client.py`.

For `/ask` and streaming requests, retrieved chunks become academic-Q&A context for the selected LLM client—primarily OpenAI (`src/services/openai_llm/client.py`), with Bedrock and Ollama adapters also present at `src/services/bedrock_llm/client.py` and `src/services/ollama/client.py`. Redis exact-match caching is behind `src/services/cache/client.py`; Langfuse tracing wraps request execution via `src/services/langfuse/tracer.py`.

#### Agentic RAG Control Flow
The LangGraph workflow is defined in `src/services/agents/agentic_rag.py` and operates on explicit state/context in `src/services/agents/state.py` and `context.py`. It enforces an academic-domain guardrail, retrieves documents, grades relevance, conditionally rewrites the query and retries retrieval, then generates an answer. The key executable nodes are `nodes/guardrail_node.py`, `nodes/retrieve_node.py`, `nodes/grade_documents_node.py`, and `nodes/generate_answer_node.py`. Bedrock guardrails are separately abstracted in `src/services/bedrock_guardrails/service.py`.

#### Runtime and Infrastructure
Local deployment is Docker Compose (`compose.yml`) with API, Airflow, OpenSearch, and Dashboards; API and Airflow images are defined by `Dockerfile` and `airflow/Dockerfile`. Managed external dependencies include Neon PostgreSQL, Upstash Redis, OpenAI, Jina AI, Langfuse, arXiv, and optionally AWS Bedrock.

Production targets AWS EKS: cluster/namespace definitions in `deployment/eks/cluster.yaml` and `namespace.yaml`; scalable API deployment in `deployment/k8s/api/deployment.yaml` and `hpa.yaml`; persistent OpenSearch in `deployment/k8s/opensearch/statefulset.yaml`; and Airflow deployment in `deployment/k8s/airflow/deployment.yaml`. GitHub Actions CI/CD builds, publishes to ECR, and deploys through `.github/workflows/cd.yml`.

Main entry points: [`src/dependencies.py`](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/dependencies.py) (Dependency assembly), [`src/main.py`](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/main.py) (FastAPI API), [`src/services/opensearch/query_builder.py`](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/opensearch/query_builder.py) (OpenSearch hybrid corpus), [`src/routers/ask.py`](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/routers/ask.py) (Ask, search & agent routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_channels["User Channels"]
  node_fastapi["FastAPI API<br/>API entrypoint<br/>[main.py]"]
  node_http_routes["Ask, search &amp; agent routes<br/>route composition<br/>[ask.py]"]
  node_gradio["Gradio UI<br/>interactive client<br/>[gradio_app.py]"]
  node_telegram["Telegram bot<br/>chat channel<br/>[bot.py]"]
  node_mcp["MCP server<br/>tool server<br/>[server.py]"]
end

subgraph group_app["Application"]
  node_dependencies["Dependency assembly<br/>service factories<br/>[dependencies.py]"]
  node_api_contracts["API contracts<br/>request/response schemas"]
  node_hybrid_indexer["Hybrid indexer<br/>indexing orchestrator<br/>[hybrid_indexer.py]"]
  node_agentic_rag["Agentic RAG graph<br/>LangGraph workflow<br/>[agentic_rag.py]"]
end

subgraph group_ingestion["Corpus Pipeline"]
  node_airflow_dag["ArXiv ingestion DAG<br/>Airflow orchestration"]
  node_arxiv_fetch["ArXiv fetch<br/>metadata and PDF source<br/>[client.py]"]
  node_document_processing["Parse, chunk &amp; embed<br/>corpus preparation<br/>[docling.py]"]
end

subgraph group_data["Data &amp; AI Services"]
  node_opensearch[("OpenSearch hybrid corpus<br/>BM25 and vector retrieval<br/>[query_builder.py]")]
  node_postgres[("PostgreSQL metadata<br/>paper persistence<br/>[paper.py]")]
  node_redis[("Redis cache<br/>exact-match cache<br/>[client.py]")]
  node_llm_adapters["LLM adapters<br/>answer generation<br/>[client.py]"]
  node_langfuse["Langfuse tracing<br/>observability<br/>[tracer.py]"]
end

subgraph group_deployment["Deployment"]
  node_compose["Local Compose stack<br/>local runtime<br/>[compose.yml]"]
  node_eks_api["EKS API deployment<br/>scalable API workload<br/>[deployment.yaml]"]
  node_eks_opensearch[("EKS OpenSearch<br/>stateful workload<br/>[statefulset.yaml]")]
  node_cicd["CI/CD to ECR and EKS<br/>GitHub Actions workflow<br/>[cd.yml]"]
end

node_fastapi -->|"mounts"| node_http_routes
node_gradio -->|"uses API"| node_fastapi
node_telegram -->|"uses services"| node_dependencies
node_mcp -->|"uses services"| node_dependencies
node_http_routes -->|"validates"| node_api_contracts
node_http_routes -->|"requests services"| node_dependencies
node_dependencies -->|"cache lookup"| node_redis
node_dependencies -->|"hybrid retrieval"| node_opensearch
node_dependencies -->|"agent requests"| node_agentic_rag
node_dependencies -->|"answers and SSE"| node_llm_adapters
node_dependencies -.->|"traces"| node_langfuse
node_agentic_rag -->|"retrieve and retry"| node_opensearch
node_agentic_rag -->|"grade and generate"| node_llm_adapters
node_airflow_dag -->|"fetch stage"| node_arxiv_fetch
node_arxiv_fetch -->|"PDFs and metadata"| node_document_processing
node_document_processing -->|"chunks and vectors"| node_hybrid_indexer
node_hybrid_indexer -->|"indexes chunks"| node_opensearch
node_hybrid_indexer -->|"persists papers"| node_postgres
node_compose -->|"runs locally"| node_fastapi
node_compose -->|"runs locally"| node_airflow_dag
node_eks_api -->|"deploys"| node_fastapi
node_eks_opensearch -->|"hosts"| node_opensearch
node_cicd -->|"builds and deploys"| node_eks_api

click node_fastapi "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/main.py"
click node_http_routes "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/routers/ask.py"
click node_gradio "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/gradio_app.py"
click node_telegram "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/telegram/bot.py"
click node_mcp "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/mcp_server/server.py"
click node_dependencies "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/dependencies.py"
click node_hybrid_indexer "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/indexing/hybrid_indexer.py"
click node_agentic_rag "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/agents/agentic_rag.py"
click node_airflow_dag "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/airflow/dags/arxiv_paper_ingestion.py"
click node_arxiv_fetch "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/arxiv/client.py"
click node_document_processing "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/pdf_parser/docling.py"
click node_opensearch "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/opensearch/query_builder.py"
click node_postgres "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/repositories/paper.py"
click node_redis "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/cache/client.py"
click node_llm_adapters "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/openai_llm/client.py"
click node_langfuse "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/langfuse/tracer.py"
click node_compose "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/compose.yml"
click node_eks_api "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/deployment/k8s/api/deployment.yaml"
click node_eks_opensearch "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/deployment/k8s/opensearch/statefulset.yaml"
click node_cicd "https://github.com/sourangshupal/agentic-rag-project/blob/agentops/.github/workflows/cd.yml"

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_fastapi,node_http_routes,node_gradio,node_telegram,node_mcp toneBlue
class node_dependencies,node_api_contracts,node_hybrid_indexer,node_agentic_rag toneAmber
class node_airflow_dag,node_arxiv_fetch,node_document_processing toneMint
class node_opensearch,node_postgres,node_redis,node_llm_adapters,node_langfuse toneRose
class node_compose,node_eks_api,node_eks_opensearch,node_cicd toneIndigo
```

## Components

### User Channels

- [FastAPI API](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/main.py): API entrypoint · `src/main.py`
- [Ask, search & agent routes](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/routers/ask.py): route composition · `src/routers/ask.py`
- [Gradio UI](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/gradio_app.py): interactive client · `src/gradio_app.py`
- [Telegram bot](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/telegram/bot.py): chat channel · `src/services/telegram/bot.py`
- [MCP server](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/mcp_server/server.py): tool server · `src/mcp_server/server.py`

### Application

- [Dependency assembly](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/dependencies.py): service factories · Supplies infrastructure-backed services to handlers. · `src/dependencies.py`
- **API contracts**: request/response schemas
- [Hybrid indexer](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/indexing/hybrid_indexer.py): indexing orchestrator · `src/services/indexing/hybrid_indexer.py`
- [Agentic RAG graph](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/agents/agentic_rag.py): LangGraph workflow · Guardrails, retrieves, grades, retries, then generates. · `src/services/agents/agentic_rag.py`

### Corpus Pipeline

- [ArXiv ingestion DAG](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/airflow/dags/arxiv_paper_ingestion.py): Airflow orchestration · `airflow/dags/arxiv_paper_ingestion.py`
- [ArXiv fetch](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/arxiv/client.py): metadata and PDF source · `src/services/arxiv/client.py`
- [Parse, chunk & embed](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/pdf_parser/docling.py): corpus preparation · Uses Docling extraction, chunking, and Jina embeddings. · `src/services/pdf_parser/docling.py`

### Data & AI Services

- [OpenSearch hybrid corpus](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/opensearch/query_builder.py): BM25 and vector retrieval · Query construction applies RRF-style hybrid fusion. · `src/services/opensearch/query_builder.py`
- [PostgreSQL metadata](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/repositories/paper.py): paper persistence · `src/repositories/paper.py`
- [Redis cache](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/cache/client.py): exact-match cache · `src/services/cache/client.py`
- [LLM adapters](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/openai_llm/client.py): answer generation · OpenAI primary; Bedrock and Ollama are supported. · `src/services/openai_llm/client.py`
- [Langfuse tracing](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/src/services/langfuse/tracer.py): observability · `src/services/langfuse/tracer.py`

### Deployment

- [Local Compose stack](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/compose.yml): local runtime · Runs API, Airflow, OpenSearch, and Dashboards. · `compose.yml`
- [EKS API deployment](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/deployment/k8s/api/deployment.yaml): scalable API workload · `deployment/k8s/api/deployment.yaml`
- [EKS OpenSearch](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/deployment/k8s/opensearch/statefulset.yaml): stateful workload · `deployment/k8s/opensearch/statefulset.yaml`
- [CI/CD to ECR and EKS](https://github.com/sourangshupal/agentic-rag-project/blob/agentops/.github/workflows/cd.yml): GitHub Actions workflow · `.github/workflows/cd.yml`

## Connections

- FastAPI API → Ask, search & agent routes: mounts
- Gradio UI → FastAPI API: uses API
- Telegram bot → Dependency assembly: uses services
- MCP server → Dependency assembly: uses services
- Ask, search & agent routes → API contracts: validates
- Ask, search & agent routes → Dependency assembly: requests services
- Dependency assembly → Redis cache: cache lookup
- Dependency assembly → OpenSearch hybrid corpus: hybrid retrieval
- Dependency assembly → Agentic RAG graph: agent requests
- Dependency assembly → LLM adapters: answers and SSE
- Dependency assembly → Langfuse tracing: traces
- Agentic RAG graph → OpenSearch hybrid corpus: retrieve and retry
- Agentic RAG graph → LLM adapters: grade and generate
- ArXiv ingestion DAG → ArXiv fetch: fetch stage
- ArXiv fetch → Parse, chunk & embed: PDFs and metadata
- Parse, chunk & embed → Hybrid indexer: chunks and vectors
- Hybrid indexer → OpenSearch hybrid corpus: indexes chunks
- Hybrid indexer → PostgreSQL metadata: persists papers
- Local Compose stack → FastAPI API: runs locally
- Local Compose stack → ArXiv ingestion DAG: runs locally
- EKS API deployment → FastAPI API: deploys
- EKS OpenSearch → OpenSearch hybrid corpus: hosts
- CI/CD to ECR and EKS → EKS API deployment: builds and deploys
