# fenil210/medical-rag architecture

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

- Interactive diagram: https://gitdiagram.com/fenil210/medical-rag
- Repository: https://github.com/fenil210/medical-rag

## Overview

#### Purpose and Runtime Model
This repository is a local, CPU-oriented medical RAG chatbot. It ingests PDF medical references, semantically chunks and embeds their content into Qdrant, retrieves relevant passages for a question, and uses a local BioMistral-7B GGUF model to generate an answer with source metadata.

Primary entry points:
- Corpus ingestion: `ingest.py`
- Retrieval validation / CLI-style query test: `retriever.py`
- Served chatbot application: `rag.py`
- Browser interface: `templates/index.html`

#### Document Corpus and Ingestion
The knowledge base is a fixed local PDF corpus under `Data/`, including:
- `Data/cancer_and_cure__a_critical_analysis.27.pdf`
- `Data/medical_oncology_handbook_june_2020_edition.pdf`

`ingest.py` is the offline indexing stage. It extracts document content, applies semantic chunking, creates PubMedBERT-based dense embeddings, and writes chunks plus page/document metadata to Qdrant. Re-running ingestion is the boundary where source documents become searchable vector records.

#### Vector Store Boundary
Qdrant is the persistent retrieval backend, expected to run externally in Docker and be exposed locally on port 6333. It stores embedded chunks and associated metadata needed to show document/page provenance.

Relevant repository artifacts:
- `ingest.py`
- `retriever.py`
- `docker.txt`

The application depends on Qdrant being available before either retrieval testing or serving queries. Its dashboard is available at the Qdrant local endpoint described in the README.

#### Retrieval Stage
`retriever.py` provides the retrieval-focused execution path: a user question is embedded using the same medical embedding model, then similarity search selects the top relevant chunks from Qdrant. The expected result includes retrieved text and metadata, rather than only a generated answer.

This stage is architecturally important because it establishes the grounded context passed to generation:
- Query input → embedding
- Embedding → Qdrant similarity search
- Top chunks + source/page metadata → RAG context

#### Generation and RAG Orchestration
`rag.py` is the online orchestration and API entry point, launched with Uvicorn. It combines retrieval results with the user prompt and invokes a locally downloaded BioMistral-7B GGUF model.

The model is not versioned in the repository; it must be downloaded separately and placed at the project root as instructed by `README.md`. Local CPU inference is a major operational constraint, with responses expected to take roughly 25–30 seconds.

#### Web/API Presentation Boundary
The application serves a browser-facing chat experience through:
- `rag.py`
- `templates/index.html`

The UI submits questions to the backend and displays generated output together with retrieved context and provenance metadata. Although the README refers to FastAPI and Flask, `rag.py` is the authoritative service entry point for deployment via `uvicorn rag:app`.

#### Dependencies and External Services
Python dependencies are declared in `requirements.txt`. Material external/runtime dependencies are:
- Docker-hosted Qdrant vector database.
- Hugging Face-distributed BioMistral-7B GGUF model for local generation.
- PubMedBERT embeddings for medical-domain vector retrieval.
- Local PDF files under `Data/` as the indexed knowledge source.

The system has no indicated hosted application dependency: documents, embedding/generation models, Qdrant, API server, and UI are intended to run locally.

Main entry points: [`rag.py`](https://github.com/fenil210/medical-rag/blob/main/rag.py) (RAG API service), [`ingest.py`](https://github.com/fenil210/medical-rag/blob/main/ingest.py) (Ingestion pipeline), [`retriever.py`](https://github.com/fenil210/medical-rag/blob/main/retriever.py) (Retrieval CLI), [`templates/index.html`](https://github.com/fenil210/medical-rag/blob/main/templates/index.html) (Browser chat UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_offline["Offline indexing"]
  node_pdf_corpus["Medical PDF corpus<br/>source documents"]
  node_ingest["Ingestion pipeline<br/>Python entry point<br/>[ingest.py]"]
  node_semantic_chunks["Semantic chunks<br/>index payload"]
end

subgraph group_online["Online RAG service"]
  node_retriever["Retrieval CLI<br/>Python entry point<br/>[retriever.py]"]
  node_question(("User question<br/>input"))
  node_query_embedding["Query embedding<br/>vector"]
  node_retrieved_context["Grounded context<br/>retrieval result"]
  node_rag_service["RAG API service<br/>Uvicorn Python entry point<br/>[rag.py]"]
  node_biomistral{{"BioMistral-7B GGUF<br/>local generation model"}}
  node_browser_ui["Browser chat UI<br/>HTML client<br/>[index.html]"]
  node_answer["Answer with sources<br/>response"]
end

node_pubmedbert{{"PubMedBERT<br/>embedding model"}}
node_qdrant[("Qdrant<br/>external vector database")]
node_docker_setup["Qdrant Docker setup<br/>runtime instructions<br/>[docker.txt]"]

node_pdf_corpus -->|"extracts"| node_ingest
node_ingest -->|"embeds chunks"| node_pubmedbert
node_ingest -->|"creates"| node_semantic_chunks
node_semantic_chunks -->|"indexes with metadata"| node_qdrant
node_docker_setup -.->|"runs"| node_qdrant
node_question -->|"CLI query"| node_retriever
node_retriever -->|"embeds query"| node_pubmedbert
node_retriever -->|"similarity search"| node_qdrant
node_qdrant -->|"top chunks"| node_retrieved_context
node_browser_ui -->|"submits question"| node_rag_service
node_rag_service -->|"embeds query"| node_pubmedbert
node_rag_service -->|"retrieves context"| node_qdrant
node_qdrant -->|"chunks and provenance"| node_rag_service
node_rag_service -->|"prompt plus context"| node_biomistral
node_biomistral -->|"generates"| node_answer
node_answer -->|"answer and sources"| node_browser_ui

click node_ingest "https://github.com/fenil210/medical-rag/blob/main/ingest.py"
click node_docker_setup "https://github.com/fenil210/medical-rag/blob/main/docker.txt"
click node_retriever "https://github.com/fenil210/medical-rag/blob/main/retriever.py"
click node_rag_service "https://github.com/fenil210/medical-rag/blob/main/rag.py"
click node_browser_ui "https://github.com/fenil210/medical-rag/blob/main/templates/index.html"

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_pdf_corpus,node_ingest,node_semantic_chunks toneBlue
class node_retriever,node_question,node_query_embedding,node_retrieved_context,node_rag_service,node_biomistral,node_browser_ui,node_answer toneAmber
class node_pubmedbert,node_qdrant,node_docker_setup toneNeutral
```

## Components

### Offline indexing

Transforms the local PDF corpus into searchable records.

- **Medical PDF corpus**: source documents · Fixed local medical reference corpus.
- [Ingestion pipeline](https://github.com/fenil210/medical-rag/blob/main/ingest.py): Python entry point · Extracts, chunks, embeds, and indexes PDFs. · `ingest.py`
- **Semantic chunks**: index payload · Chunk text carries document and page metadata.

### Online RAG service

Retrieves grounded context and generates local answers.

- [Retrieval CLI](https://github.com/fenil210/medical-rag/blob/main/retriever.py): Python entry point · Validates retrieval and returns chunks with provenance. · `retriever.py`
- **User question**: input
- **Query embedding**: vector
- **Grounded context**: retrieval result · Top chunks plus document and page metadata.
- [RAG API service](https://github.com/fenil210/medical-rag/blob/main/rag.py): Uvicorn Python entry point · Authoritative online orchestration entry point. · `rag.py`
- **BioMistral-7B GGUF**: local generation model · Separately downloaded CPU inference model.
- [Browser chat UI](https://github.com/fenil210/medical-rag/blob/main/templates/index.html): HTML client · Displays answers, context, and provenance. · `templates/index.html`
- **Answer with sources**: response

### Other components

- **PubMedBERT**: embedding model · Shared medical-domain embedding model.
- **Qdrant**: external vector database · Docker-hosted persistent similarity-search backend on local port 6333.
- [Qdrant Docker setup](https://github.com/fenil210/medical-rag/blob/main/docker.txt): runtime instructions · `docker.txt`

## Connections

- Medical PDF corpus → Ingestion pipeline: extracts
- Ingestion pipeline → PubMedBERT: embeds chunks
- Ingestion pipeline → Semantic chunks: creates
- Semantic chunks → Qdrant: indexes with metadata
- Qdrant Docker setup → Qdrant: runs
- User question → Retrieval CLI: CLI query
- Retrieval CLI → PubMedBERT: embeds query
- Retrieval CLI → Qdrant: similarity search
- Qdrant → Grounded context: top chunks
- Browser chat UI → RAG API service: submits question
- RAG API service → PubMedBERT: embeds query
- RAG API service → Qdrant: retrieves context
- Qdrant → RAG API service: chunks and provenance
- RAG API service → BioMistral-7B GGUF: prompt plus context
- BioMistral-7B GGUF → Answer with sources: generates
- Answer with sources → Browser chat UI: answer and sources
