A retrieval-augmented generation system that answers medical questions only from an indexed reference corpus — it retrieves the relevant passages with FAISS, grounds the language model on them, cites what it used, and says so plainly when the answer is not in its sources.
Not medical advice. This is an educational demonstration of RAG architecture. Every answer carries a disclaimer, and the bundled corpus is three sample documents written for testing — not a clinical reference.
Asking a general language model a medical question invites confident invention. RAG removes the model's freedom to answer from memory: the prompt supplies the retrieved passages and instructs the model to reply "The answer to this question was not found in the available sources" when they do not cover the question. The answer is then traceable to specific documents, which is the only defensible design in a domain where a fabricated dosage is a real harm.
This project implements that end to end, and keeps every layer swappable — corpus, embedding model, generator.
data/raw/sample_medical_data.json documents with metadata
|
v
MedicalDataLoader clean -> chunk (500 words, 50 overlap,
| split on sentence boundaries)
v
data/processed/processed_medical_chunks.json
|
v
MedicalVectorStore all-MiniLM-L6-v2 embeddings -> FAISS index
| persisted to vector_db/
v
Retriever (top k = 3)
|
v
MedicalRAGSystem grounding prompt + GPT-2 -> RetrievalQA
|
v
answer + cited sources + disclaimer
| Layer | Default | Configurable in |
|---|---|---|
| Embeddings | sentence-transformers/all-MiniLM-L6-v2 |
config.EMBEDDING_MODEL_NAME |
| Vector store | FAISS (local, on disk) | config.VECTOR_DB_DIR |
| Generator | gpt2 |
config.LLM_MODEL_NAME |
| Chunking | 500 words, 50 overlap | config.CHUNK_SIZE / CHUNK_OVERLAP |
| Retrieval | top 3 | config.RETRIEVAL_CONFIG |
git clone https://github.com/MrDanial-Rafiee/medical-rag-assistant.git
cd medical-rag-assistant
pip install -r requirements.txt
python main.pyThe first run downloads the embedding and language models from Hugging Face, processes the corpus, builds the FAISS index into vector_db/, runs a two-question smoke test, then drops into an interactive prompt:
Your medical question: What is a normal blood pressure?
Answer:
----------------------------------------
...
Sources used:
1. Blood Pressure: Definitions and Management (Cardiovascular conditions)
Interactive commands: test runs the sample questions, stats prints index statistics, exit quits.
streamlit run app.pyRequires the index to exist, so run python main.py at least once first.
├── main.py CLI entry point: build index -> QA chain -> interactive loop
├── app.py Streamlit front end
├── config.py all paths, model names and hyperparameters
├── src/
│ ├── data_loader.py load corpus, clean, sentence-aware chunking
│ ├── vector_store.py FAISS build / load / retrieve / stats
│ ├── rag_system.py prompt template, LLM loading, RetrievalQA chain
│ └── utils.py logging and directory setup
└── data/raw/
└── sample_medical_data.json the reference corpus
Replace data/raw/sample_medical_data.json with your own documents in the same shape, delete data/processed/ and vector_db/, then re-run python main.py:
[
{
"id": "DOC001",
"title": "Document title",
"content": "Full text of the document...",
"category": "Topic area",
"source": "Where it came from",
"language": "en",
"tags": ["tag1", "tag2"]
}
]Nothing else needs to change — chunking, embedding and indexing all follow from this file.
- The corpus is three documents. It is enough to exercise the pipeline, not to answer real questions. The value here is the architecture; the knowledge base is a placeholder.
- GPT-2 is a weak generator. It is the default because it runs on any CPU, but its answers are frequently incoherent. The retrieval half of the system works well; swapping
LLM_MODEL_NAMEfor an instruction-tuned model is the single biggest quality win available. - No retrieval evaluation. There are no recall@k or MRR numbers, because there is no labelled question-to-document set. Building one is the right next step before any tuning.
- Retrieval is pure dense similarity. Hybrid search (BM25 + dense) reliably outperforms it on domain corpora with heavy terminology.
RETRIEVAL_CONFIG["score_threshold"]is defined but not enforced — the retriever always returns exactlykdocuments, however weak the match.- No re-ranking step, no conversational memory, no streaming output.
MIT — see LICENSE.