How sourangshupal/agentic-rag-project works
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: dependencies.py Dependency assembly · main.py FastAPI API · query_builder.py OpenSearch hybrid corpus · ask.py Ask, search & agent routes
Components 21
User Channels
- FastAPI API
src/main.py - Ask, search & agent routes
src/routers/ask.py - Gradio UI
src/gradio_app.py - Telegram bot
src/services/telegram/bot.py - MCP server
src/mcp_server/server.py
Application
- Dependency assembly · Supplies infrastructure-backed services to handlers.
src/dependencies.py - API contracts
- Hybrid indexer
src/services/indexing/hybrid_indexer.py - Agentic RAG graph · Guardrails, retrieves, grades, retries, then generates.
src/services/agents/agentic_rag.py
Corpus Pipeline
- ArXiv ingestion DAG
airflow/dags/arxiv_paper_ingestion.py - ArXiv fetch
src/services/arxiv/client.py - Parse, chunk & embed · Uses Docling extraction, chunking, and Jina embeddings.
src/services/pdf_parser/docling.py
Data & AI Services
- OpenSearch hybrid corpus · Query construction applies RRF-style hybrid fusion.
src/services/opensearch/query_builder.py - PostgreSQL metadata
src/repositories/paper.py - Redis cache
src/services/cache/client.py - LLM adapters · OpenAI primary; Bedrock and Ollama are supported.
src/services/openai_llm/client.py - Langfuse tracing
src/services/langfuse/tracer.py
Deployment
- Local Compose stack · Runs API, Airflow, OpenSearch, and Dashboards.
compose.yml - EKS API deployment
deployment/k8s/api/deployment.yaml - EKS OpenSearch
deployment/k8s/opensearch/statefulset.yaml - CI/CD to ECR and EKS
.github/workflows/cd.yml
Connections 23
- 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