# mohanapriyan2006/medimei-rag-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/mohanapriyan2006/medimei-rag-chatbot
- Repository: https://github.com/mohanapriyan2006/medimei-rag-chatbot

## Overview

#### 1. Purpose and deployment shape
MediMei is an evidence-first clinical drug-information assistant. Users upload drug-label documents, ask questions, receive retrieval-grounded answers with page/section citations, and can compare drugs or retain memory. It is a React SPA plus FastAPI RAG backend; the frontend can also run as a preview-only deployment.
Paths: `frontend/src/main.tsx`, `frontend/src/App.tsx`, `backend/app/main.py`.

#### 2. Application entry and API boundary
The FastAPI application is the backend composition root. Its router exposes auth, document ingestion, chat, search, citations, memories, task status, and comparison endpoints. Authentication dependencies enforce user isolation at the HTTP boundary; route handlers delegate substantive work to services/repositories.
Paths: `backend/app/main.py`, `backend/app/api/router.py`, `backend/app/api/routes/chat.py`.

#### 3. Document ingestion and indexing pipeline
Document upload flows into a staged pipeline: extraction/cleaning, optional OCR and table extraction, quality checks/section detection, chunk construction with metadata, embedding generation, then vector indexing. This separates raw source processing from retrieval-ready chunks and preserves document/page metadata required for citations.
Paths: `backend/app/services/pdf/pipeline.py`, `backend/app/services/chunking/chunker.py`, `backend/app/services/retrieval/indexer_service.py`.

#### 4. Evidence retrieval and answer generation
Chat processing routes the query, combines semantic, keyword, and hybrid retrieval, and reranks results. The RAG service builds evidence-aware context, invokes the LLM layer, and maps generated claims back to source citations. Validation services provide a final safety/grounding/evidence boundary intended to prevent unsupported answers.
Paths: `backend/app/services/chat/rag_service.py`, `backend/app/services/retrieval/hybrid_search.py`, `backend/app/services/validation/grounding_validator.py`.

#### 5. Persistence and external AI infrastructure
MySQL holds relational application state: users, documents/pages/chunks, conversations, citations, and memories. Qdrant holds vectorized document chunks for semantic retrieval. Embeddings use BGE-M3; LLM integration is encapsulated behind client/service/generation modules and is configured for Qwen models. PDF parsing uses PyMuPDF and OCR uses PaddleOCR.
Paths: `backend/app/db/database.py`, `backend/app/repositories/qdrant_repository.py`, `backend/app/services/llm/client.py`.

#### 6. Specialized user workflows
Comparison is a separate service flow that assembles evidence-backed side-by-side drug attributes. Memory persists user facts/preferences for cross-session conversational context. Background task management supports long-running ingestion/status reporting rather than blocking the UI request lifecycle.
Paths: `backend/app/services/comparison/comparison_service.py`, `backend/app/services/chat/memory_service.py`, `backend/app/core/task_manager.py`.

#### 7. Frontend composition and client state
The Vite React application routes users through chat, documents, comparisons, and memory views. Contexts/hooks own authentication, conversations, documents, task progress, and UI state; API modules form the client-to-FastAPI boundary. Chat/evidence/document components render streaming answers, citations, source viewing, and upload lifecycle.
Paths: `frontend/src/pages/ChatPage.tsx`, `frontend/src/contexts/ChatContext.tsx`, `frontend/src/api/client.ts`.

#### 8. Runtime topology
Frontend is designed for Vercel SPA deployment; backend/GPU deployment targets vast.ai. Local/full-stack runtime requires MySQL and Qdrant, with Qdrant also startable via the repository script. Uploaded/processed document artifacts reside under the repository data directories.
Paths: `frontend/vercel.json`, `backend/start_qdrant.sh`, `data/uploads`.

Main entry points: [`backend/app/api/router.py`](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/api/router.py) (API router), [`backend/app/services/chat/rag_service.py`](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chat/rag_service.py) (RAG service), [`backend/app/services/pdf/pipeline.py`](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/pdf/pipeline.py) (Document pipeline), [`backend/app/services/retrieval/hybrid_search.py`](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/retrieval/hybrid_search.py) (Hybrid retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React SPA"]
  node_spa["React application<br/>Vite SPA<br/>[App.tsx]"]
  node_client_state["Client state<br/>contexts<br/>[ChatContext.tsx]"]
  node_api_client["API client<br/>HTTP client<br/>[client.ts]"]
  node_frontend_entry["Frontend entry<br/>browser entry<br/>[main.tsx]"]
end

subgraph group_backend["FastAPI Backend"]
  node_fastapi_app["FastAPI application<br/>composition root<br/>[main.py]"]
  node_api_router["API router<br/>HTTP boundary<br/>[router.py]"]
  node_chat_api["Chat endpoint<br/>route handler<br/>[chat.py]"]
  node_ingestion_pipeline["Document pipeline<br/>ingestion pipeline<br/>[pipeline.py]"]
  node_chunker["Chunk builder<br/>chunking service<br/>[chunker.py]"]
  node_indexer["Embedding indexer<br/>indexing service<br/>[indexer_service.py]"]
  node_rag_service{{"RAG service<br/>answer orchestration<br/>[rag_service.py]"}}
  node_hybrid_search["Hybrid retrieval<br/>retrieval service<br/>[hybrid_search.py]"]
  node_grounding_validator["Grounding validation<br/>safety boundary"]
  node_llm_client["LLM client<br/>Qwen integration<br/>[client.py]"]
  node_comparison_service["Comparison service<br/>drug comparison"]
  node_memory_service["Memory service<br/>conversation memory<br/>[memory_service.py]"]
  node_task_manager["Task manager<br/>background jobs<br/>[task_manager.py]"]
end

subgraph group_data["Data &amp; AI Infrastructure"]
  node_mysql[("MySQL<br/>relational database<br/>[database.py]")]
  node_qdrant[("Qdrant<br/>vector database")]
  node_uploads["Document artifacts<br/>file storage"]
end

node_frontend_entry -->|"mounts"| node_spa
node_spa -->|"uses"| node_client_state
node_client_state -->|"requests"| node_api_client
node_api_client -->|"HTTP"| node_fastapi_app
node_fastapi_app -->|"registers"| node_api_router
node_api_router -->|"routes chat"| node_chat_api
node_chat_api -->|"answers"| node_rag_service
node_rag_service -->|"retrieves evidence"| node_hybrid_search
node_hybrid_search -->|"semantic search"| node_qdrant
node_rag_service -->|"generates answer"| node_llm_client
node_rag_service -->|"validates output"| node_grounding_validator
node_rag_service -->|"stores citations"| node_mysql
node_ingestion_pipeline -->|"clean source"| node_chunker
node_chunker -->|"metadata chunks"| node_indexer
node_indexer -->|"upserts vectors"| node_qdrant
node_ingestion_pipeline -->|"reads and writes"| node_uploads
node_task_manager -->|"runs asynchronously"| node_ingestion_pipeline
node_api_router -->|"starts and polls"| node_task_manager
node_api_router -->|"comparison requests"| node_comparison_service
node_comparison_service -->|"retrieves evidence"| node_hybrid_search
node_api_router -->|"memory requests"| node_memory_service
node_memory_service -->|"persists memory"| node_mysql
node_ingestion_pipeline -->|"stores document state"| node_mysql

click node_spa "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/App.tsx"
click node_client_state "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/contexts/ChatContext.tsx"
click node_api_client "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/api/client.ts"
click node_frontend_entry "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/main.tsx"
click node_fastapi_app "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/main.py"
click node_api_router "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/api/router.py"
click node_chat_api "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/api/routes/chat.py"
click node_ingestion_pipeline "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/pdf/pipeline.py"
click node_chunker "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chunking/chunker.py"
click node_indexer "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/retrieval/indexer_service.py"
click node_rag_service "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chat/rag_service.py"
click node_hybrid_search "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/retrieval/hybrid_search.py"
click node_grounding_validator "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/validation/grounding_validator.py"
click node_llm_client "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/llm/client.py"
click node_comparison_service "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/comparison/comparison_service.py"
click node_memory_service "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chat/memory_service.py"
click node_task_manager "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/core/task_manager.py"
click node_mysql "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/db/database.py"
click node_qdrant "https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/repositories/qdrant_repository.py"
click node_uploads "https://github.com/mohanapriyan2006/medimei-rag-chatbot/tree/main/data/uploads"

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_spa,node_client_state,node_api_client,node_frontend_entry toneBlue
class node_fastapi_app,node_api_router,node_chat_api,node_ingestion_pipeline,node_chunker,node_indexer,node_rag_service,node_hybrid_search,node_grounding_validator,node_llm_client,node_comparison_service,node_memory_service,node_task_manager toneAmber
class node_mysql,node_qdrant,node_uploads toneMint
```

## Components

### React SPA

- [React application](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/App.tsx): Vite SPA · Chat, document, comparison, and memory user workflows. · `frontend/src/App.tsx`
- [Client state](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/contexts/ChatContext.tsx): contexts · Owns chat, authentication, documents, and task progress state. · `frontend/src/contexts/ChatContext.tsx`
- [API client](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/api/client.ts): HTTP client · `frontend/src/api/client.ts`
- [Frontend entry](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/frontend/src/main.tsx): browser entry · `frontend/src/main.tsx`

### FastAPI Backend

- [FastAPI application](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/main.py): composition root · `backend/app/main.py`
- [API router](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/api/router.py): HTTP boundary · Auth dependencies isolate user-scoped routes. · `backend/app/api/router.py`
- [Chat endpoint](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/api/routes/chat.py): route handler · `backend/app/api/routes/chat.py`
- [Document pipeline](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/pdf/pipeline.py): ingestion pipeline · Extracts, OCRs, checks, and detects sections before chunking. · `backend/app/services/pdf/pipeline.py`
- [Chunk builder](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chunking/chunker.py): chunking service · Preserves document, page, and section metadata. · `backend/app/services/chunking/chunker.py`
- [Embedding indexer](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/retrieval/indexer_service.py): indexing service · Generates embeddings and indexes retrieval-ready chunks. · `backend/app/services/retrieval/indexer_service.py`
- [RAG service](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chat/rag_service.py): answer orchestration · Builds evidence context and maps claims to citations. · `backend/app/services/chat/rag_service.py`
- [Hybrid retrieval](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/retrieval/hybrid_search.py): retrieval service · Combines semantic and keyword retrieval with reranking. · `backend/app/services/retrieval/hybrid_search.py`
- [Grounding validation](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/validation/grounding_validator.py): safety boundary · Checks answer grounding and evidence support. · `backend/app/services/validation/grounding_validator.py`
- [LLM client](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/llm/client.py): Qwen integration · `backend/app/services/llm/client.py`
- [Comparison service](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/comparison/comparison_service.py): drug comparison · Assembles evidence-backed side-by-side attributes. · `backend/app/services/comparison/comparison_service.py`
- [Memory service](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/services/chat/memory_service.py): conversation memory · `backend/app/services/chat/memory_service.py`
- [Task manager](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/core/task_manager.py): background jobs · Runs long ingestion work and exposes status. · `backend/app/core/task_manager.py`

### Data & AI Infrastructure

- [MySQL](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/db/database.py): relational database · Stores users, documents, chunks, conversations, citations, and memories. · `backend/app/db/database.py`
- [Qdrant](https://github.com/mohanapriyan2006/medimei-rag-chatbot/blob/main/backend/app/repositories/qdrant_repository.py): vector database · Stores embedded document chunks for semantic retrieval. · `backend/app/repositories/qdrant_repository.py`
- [Document artifacts](https://github.com/mohanapriyan2006/medimei-rag-chatbot/tree/main/data/uploads): file storage · Uploaded and processed document artifacts. · `data/uploads`

## Connections

- Frontend entry → React application: mounts
- React application → Client state: uses
- Client state → API client: requests
- API client → FastAPI application: HTTP
- FastAPI application → API router: registers
- API router → Chat endpoint: routes chat
- Chat endpoint → RAG service: answers
- RAG service → Hybrid retrieval: retrieves evidence
- Hybrid retrieval → Qdrant: semantic search
- RAG service → LLM client: generates answer
- RAG service → Grounding validation: validates output
- RAG service → MySQL: stores citations
- Document pipeline → Chunk builder: clean source
- Chunk builder → Embedding indexer: metadata chunks
- Embedding indexer → Qdrant: upserts vectors
- Document pipeline → Document artifacts: reads and writes
- Task manager → Document pipeline: runs asynchronously
- API router → Task manager: starts and polls
- API router → Comparison service: comparison requests
- Comparison service → Hybrid retrieval: retrieves evidence
- API router → Memory service: memory requests
- Memory service → MySQL: persists memory
- Document pipeline → MySQL: stores document state
