How joyceannie/medical_chatbot works
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 FastAPI server · ingest.py Ingestion pipeline · index.html Chat interface · requirements.txt Python dependencies
Components 13
Offline indexing
Builds the searchable medical corpus.
- Medical PDFs · PDFs added here are indexed on the next ingestion run.
- Ingestion pipeline · Extracts, chunks, embeds, and indexes source PDFs.
ingest.py - PubMedBERT · Produces 768-dimensional dense embeddings.
Online chat service
Serves conversation-aware RAG requests.
- Browser user
- Chat interface
templates/index.html - FastAPI server · Launched with uvicorn main:app.
main.py - Chat history · Prior exchanges contextualize follow-up questions.
main.py - Query embedding · Uses the corpus embedding space for retrieval.
main.py - Passage retrieval · Fetches the highest-similarity indexed chunks.
main.py - Conversational RAG chain · Combines contextualized queries, passages, and chat state.
main.py
Other components
- Qdrant collection · Durable self-hosted vector and document metadata store.
- BioMistral GGUF · CPU-local generation model not tracked in the repository.
- Python dependencies · Provides FastAPI, LangChain, LlamaCpp, and related libraries.
requirements.txt
Connections 16
- 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