# joyceannie/medical_chatbot architecture

> Architecture diagram of the GitHub repository joyceannie/medical_chatbot, 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/joyceannie/medical_chatbot
- Repository: https://github.com/joyceannie/medical_chatbot

## Overview

#### Purpose and Entry Points
This repository is a local medical-domain Retrieval-Augmented Generation (RAG) chatbot. It indexes PDF medical documents into Qdrant, retrieves relevant passages for a user question, and uses a quantized BioMistral model to generate an answer.

Primary execution paths:
- Corpus ingestion: `ingest.py`
- FastAPI application/server entry point: `main.py`
- Browser UI: `templates/index.html`

#### Document Corpus and Ingestion
`ingest.py` is the offline indexing stage. It reads source PDFs from `data/`, splits/extracts their content, generates PubMedBERT dense embeddings, and writes vectors plus document metadata to a Qdrant collection.

Current knowledge sources:
- `data/Oncology.pdf`
- `data/jc306-un-staff-rev1_en.pdf`
- `ingest.py`

Ingestion must run before the chat service can retrieve grounded context. Adding PDFs to `data/` expands the corpus on the next ingestion run.

#### Vector Retrieval Boundary
Qdrant is the durable retrieval service and is intentionally external to the Python process. It is expected to run as a self-hosted Docker container with HTTP/gRPC ports exposed (6333/6334) and persistent storage mounted outside the repository.

The application converts queries into the same 768-dimensional PubMedBERT embedding space used during ingestion, then retrieves the highest-similarity chunks from Qdrant. This separates corpus-scale semantic search from LLM inference.

Relevant boundary paths:
- `ingest.py`
- `main.py`
- `data/`

#### LLM and RAG Orchestration
`main.py` owns online query handling and connects retrieval to generation through LangChain/LlamaCpp orchestration. Retrieved Qdrant passages are supplied as constrained context to BioMistral, reducing the need to place the entire medical corpus in the prompt.

The runtime model is a locally downloaded GGUF quantized BioMistral-7B artifact; it is not represented in the tracked file tree. The model is selected to make CPU-local inference practical.

Core paths:
- `main.py`
- `requirements.txt`

#### Conversational Flow
The chat layer uses a conversational retrieval chain. For follow-up turns, prior user/assistant exchanges are used to rewrite or contextualize the new question before retrieval. The resulting retrieval context and conversation-aware query feed the LLM, whose response is returned to the caller.

Control flow:
1. User submits a question through the HTTP/UI layer.
2. `main.py` applies conversation-aware retrieval.
3. Qdrant returns relevant indexed document chunks.
4. BioMistral generates an answer from the retrieved context and chat state.
5. The server returns the response to the UI/client.

#### Presentation and Service Interface
FastAPI in `main.py` is the service host launched with `uvicorn main:app`. It serves the application’s request endpoints and likely renders or exposes the single chat interface at `templates/index.html`.

Core paths:
- `main.py`
- `templates/index.html`

#### Runtime Dependencies
The system requires Python dependencies in `requirements.txt`, a running Qdrant instance, source PDFs, and a locally available quantized BioMistral GGUF model. External model sources are Hugging Face (BioMistral/quantized BioMistral and PubMedBERT); Qdrant is the only persistent infrastructure service.

Main entry points: [`main.py`](https://github.com/joyceannie/medical_chatbot/blob/main/main.py) (FastAPI server), [`ingest.py`](https://github.com/joyceannie/medical_chatbot/blob/main/ingest.py) (Ingestion pipeline), [`templates/index.html`](https://github.com/joyceannie/medical_chatbot/blob/main/templates/index.html) (Chat interface), [`requirements.txt`](https://github.com/joyceannie/medical_chatbot/blob/main/requirements.txt) (Python dependencies)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Offline indexing"]
  node_pdf_corpus["Medical PDFs<br/>source corpus"]
  node_ingest["Ingestion pipeline<br/>Python entry point<br/>[ingest.py]"]
  node_pubmedbert{{"PubMedBERT<br/>embedding model"}}
end

subgraph group_runtime["Online chat service"]
  node_browser(("Browser user<br/>client"))
  node_chat_ui["Chat interface<br/>HTML UI<br/>[index.html]"]
  node_fastapi["FastAPI server<br/>service host<br/>[main.py]"]
  node_conversation["Chat history<br/>conversation state<br/>[main.py]"]
  node_query_embedding{{"Query embedding<br/>PubMedBERT query encoder<br/>[main.py]"}}
  node_retrieval["Passage retrieval<br/>RAG retrieval<br/>[main.py]"]
  node_rag_chain["Conversational RAG chain<br/>LangChain orchestration<br/>[main.py]"]
end

node_qdrant[("Qdrant collection<br/>external vector database")]
node_biomistral{{"BioMistral GGUF<br/>local quantized LLM"}}
node_dependencies["Python dependencies<br/>runtime dependencies<br/>[requirements.txt]"]

node_pdf_corpus -->|"read and split"| node_ingest
node_ingest -->|"embed chunks"| node_pubmedbert
node_pubmedbert -->|"vectors + metadata"| node_qdrant
node_browser -->|"uses"| node_chat_ui
node_chat_ui -->|"submits question"| node_fastapi
node_fastapi -->|"loads prior turns"| node_conversation
node_conversation -->|"contextualizes query"| node_rag_chain
node_fastapi -->|"encodes question"| node_query_embedding
node_query_embedding -->|"semantic query"| node_retrieval
node_qdrant -->|"matching chunks"| node_retrieval
node_retrieval -->|"grounding context"| node_rag_chain
node_rag_chain -->|"contextual prompt"| node_biomistral
node_biomistral -->|"generated answer"| node_fastapi
node_fastapi -->|"response"| node_chat_ui
node_dependencies -.->|"supports"| node_ingest
node_dependencies -.->|"supports"| node_fastapi

click node_ingest "https://github.com/joyceannie/medical_chatbot/blob/main/ingest.py"
click node_chat_ui "https://github.com/joyceannie/medical_chatbot/blob/main/templates/index.html"
click node_fastapi "https://github.com/joyceannie/medical_chatbot/blob/main/main.py"
click node_conversation "https://github.com/joyceannie/medical_chatbot/blob/main/main.py"
click node_query_embedding "https://github.com/joyceannie/medical_chatbot/blob/main/main.py"
click node_retrieval "https://github.com/joyceannie/medical_chatbot/blob/main/main.py"
click node_rag_chain "https://github.com/joyceannie/medical_chatbot/blob/main/main.py"
click node_dependencies "https://github.com/joyceannie/medical_chatbot/blob/main/requirements.txt"

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_pubmedbert toneBlue
class node_browser,node_chat_ui,node_fastapi,node_conversation,node_query_embedding,node_retrieval,node_rag_chain toneAmber
class node_qdrant,node_biomistral,node_dependencies toneNeutral
```

## Components

### Offline indexing

Builds the searchable medical corpus.

- **Medical PDFs**: source corpus · PDFs added here are indexed on the next ingestion run.
- [Ingestion pipeline](https://github.com/joyceannie/medical_chatbot/blob/main/ingest.py): Python entry point · Extracts, chunks, embeds, and indexes source PDFs. · `ingest.py`
- **PubMedBERT**: embedding model · Produces 768-dimensional dense embeddings.

### Online chat service

Serves conversation-aware RAG requests.

- **Browser user**: client
- [Chat interface](https://github.com/joyceannie/medical_chatbot/blob/main/templates/index.html): HTML UI · `templates/index.html`
- [FastAPI server](https://github.com/joyceannie/medical_chatbot/blob/main/main.py): service host · Launched with uvicorn main:app. · `main.py`
- [Chat history](https://github.com/joyceannie/medical_chatbot/blob/main/main.py): conversation state · Prior exchanges contextualize follow-up questions. · `main.py`
- [Query embedding](https://github.com/joyceannie/medical_chatbot/blob/main/main.py): PubMedBERT query encoder · Uses the corpus embedding space for retrieval. · `main.py`
- [Passage retrieval](https://github.com/joyceannie/medical_chatbot/blob/main/main.py): RAG retrieval · Fetches the highest-similarity indexed chunks. · `main.py`
- [Conversational RAG chain](https://github.com/joyceannie/medical_chatbot/blob/main/main.py): LangChain orchestration · Combines contextualized queries, passages, and chat state. · `main.py`

### Other components

- **Qdrant collection**: external vector database · Durable self-hosted vector and document metadata store.
- **BioMistral GGUF**: local quantized LLM · CPU-local generation model not tracked in the repository.
- [Python dependencies](https://github.com/joyceannie/medical_chatbot/blob/main/requirements.txt): runtime dependencies · Provides FastAPI, LangChain, LlamaCpp, and related libraries. · `requirements.txt`

## Connections

- Medical PDFs → Ingestion pipeline: read and split
- Ingestion pipeline → PubMedBERT: embed chunks
- PubMedBERT → Qdrant collection: vectors + metadata
- Browser user → Chat interface: uses
- Chat interface → FastAPI server: submits question
- FastAPI server → Chat history: loads prior turns
- Chat history → Conversational RAG chain: contextualizes query
- FastAPI server → Query embedding: encodes question
- Query embedding → Passage retrieval: semantic query
- Qdrant collection → Passage retrieval: matching chunks
- Passage retrieval → Conversational RAG chain: grounding context
- Conversational RAG chain → BioMistral GGUF: contextual prompt
- BioMistral GGUF → FastAPI server: generated answer
- FastAPI server → Chat interface: response
- Python dependencies → Ingestion pipeline: supports
- Python dependencies → FastAPI server: supports
