How mohanapriyan2006/medimei-rag-chatbot works
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: router.py API router · rag_service.py RAG service · pipeline.py Document pipeline · hybrid_search.py Hybrid retrieval
Components 20
React SPA
- React application · Chat, document, comparison, and memory user workflows.
frontend/src/App.tsx - Client state · Owns chat, authentication, documents, and task progress state.
frontend/src/contexts/ChatContext.tsx - API client
frontend/src/api/client.ts - Frontend entry
frontend/src/main.tsx
FastAPI Backend
- FastAPI application
backend/app/main.py - API router · Auth dependencies isolate user-scoped routes.
backend/app/api/router.py - Chat endpoint
backend/app/api/routes/chat.py - Document pipeline · Extracts, OCRs, checks, and detects sections before chunking.
backend/app/services/pdf/pipeline.py - Chunk builder · Preserves document, page, and section metadata.
backend/app/services/chunking/chunker.py - Embedding indexer · Generates embeddings and indexes retrieval-ready chunks.
backend/app/services/retrieval/indexer_service.py - RAG service · Builds evidence context and maps claims to citations.
backend/app/services/chat/rag_service.py - Hybrid retrieval · Combines semantic and keyword retrieval with reranking.
backend/app/services/retrieval/hybrid_search.py - Grounding validation · Checks answer grounding and evidence support.
backend/app/services/validation/grounding_validator.py - LLM client
backend/app/services/llm/client.py - Comparison service · Assembles evidence-backed side-by-side attributes.
backend/app/services/comparison/comparison_service.py - Memory service
backend/app/services/chat/memory_service.py - Task manager · Runs long ingestion work and exposes status.
backend/app/core/task_manager.py
Data & AI Infrastructure
- MySQL · Stores users, documents, chunks, conversations, citations, and memories.
backend/app/db/database.py - Qdrant · Stores embedded document chunks for semantic retrieval.
backend/app/repositories/qdrant_repository.py - Document artifacts · Uploaded and processed document artifacts.
data/uploads
Connections 23
- 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