This repository contains the full MedLogix capstone pipeline and output artifacts:
medlogix/: complete codebase and data/model artifacts for a 5-phase medical LLM safety workflowOutputs/: generated output screenshots from project runs
.
├── README.md
├── Outputs/
│ ├── Screenshot 2026-04-11 144630.png
│ ├── Screenshot 2026-04-11 150539.png
│ ├── Screenshot 2026-04-11 150730.png
│ ├── Screenshot 2026-04-11 151024.png
│ ├── Screenshot 2026-04-11 151059.png
│ ├── Screenshot 2026-04-11 151310.png
│ └── Screenshot 2026-04-11 151528.png
└── medlogix/
├── download_datasets.py
├── requirements.txt
├── phase1_rag/
├── phase2_finetune/
├── phase3_rlhf/
├── phase4_agent/
├── phase5_ui/
├── tests/
├── data/
├── chroma_db/
└── models/
MedLogix is built as a progressive, 5-phase pipeline for safer pharmacology assistance:
- Phase 1 (
phase1_rag): Build and query a Chroma vector database of pharmacology guidance. - Phase 2 (
phase2_finetune): Generate synthetic extraction data and LoRA fine-tune Llama-3 for medication extraction. - Phase 3 (
phase3_rlhf): Add a safety-oriented reward function and run PPO-style alignment. - Phase 4 (
phase4_agent): Wrap the model in a LangChain ReAct agent with tool calling for interaction checks + guideline lookup. - Phase 5 (
phase5_ui): Expose the assistant through a Streamlit clinical safety dashboard.
medlogix/download_datasets.py- Pulls datasets from Hugging Face and KaggleHub.
- Stores all raw data under
medlogix/data/raw/.
- Key raw sources already present:
- FDA approvals dataset
- Drug-drug interactions dataset
- WebMD reviews
- Medical Meadow WikiDoc + MedQA
medlogix/phase1_rag/embed_webmd_data.py- Loads
medical_meadow_wikidoc.csv - Chunks text with recursive splitting
- Embeds with
all-MiniLM-L6-v2 - Stores vectors in
medlogix/chroma_db/
- Loads
medlogix/phase1_rag/drug_retriever.py- Queries Chroma collection
pharma_knowledge_base - Returns top-k relevant context chunks
- Queries Chroma collection
medlogix/phase2_finetune/generate_synthetic.py- Generates synthetic clinical-note extraction samples (
jsonl)
- Generates synthetic clinical-note extraction samples (
medlogix/phase2_finetune/train_med_lora.py- Loads
Meta-Llama-3-8B-Instructin 4-bit - Applies PEFT LoRA adapters
- Trains with TRL
SFTTrainer - Saves adapters to
medlogix/models/lora_med_extraction/
- Loads
medlogix/phase2_finetune/evaluate_extraction.py- Loads base model + LoRA adapters
- Runs a test extraction prompt
medlogix/phase3_rlhf/fda_hallucination_check.py- Builds approved-drug token set from FDA data
- Penalizes generated fake/unknown drug-like names
medlogix/phase3_rlhf/reward_model.py- Scores responses with safety heuristics:
- reward hedging and structured extraction
- penalize dangerous certainty and hallucinated drugs
- Scores responses with safety heuristics:
medlogix/phase3_rlhf/ppo_safety_train.py- Uses TRL PPO over curated prompts
- Saves aligned model output to
medlogix/models/final_aligned_model/
medlogix/phase4_agent/build_interaction_db.py- Builds SQLite interaction DB at
medlogix/data/db/interactions.db
- Builds SQLite interaction DB at
medlogix/phase4_agent/tools.pycheck_drug_interaction: checks all pairwise combinations in SQLitesearch_pharmacology_guidelines: retrieves RAG snippets from Chroma
medlogix/phase4_agent/safety_agent.pyandsafety_agentv2.py- Configure a ReAct-style LangChain agent executor
- Add custom output parsing to reduce tool-observation hallucinations
medlogix/phase5_ui/app.py- Streamlit chat app for clinical note prompts
- Displays safety warnings / pass states
- Includes operational disclaimer sidebar
medlogix/tests/exists for safety test coverage scaffolding.- Current listed files are placeholders (empty), ready for expansion:
test_hallucinated_drug.pytest_severe_interaction.py
This repository includes large artifacts (models and datasets), including files over 100MB. To keep GitHub pushes stable, Git LFS is used for:
*.safetensors*.bin*.pt*.sqlite3*.csv*.json
From repository root:
cd medlogix
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtFrom inside medlogix/:
# 0) Download datasets (if needed)
python download_datasets.py
# 1) Build vector DB
python phase1_rag/embed_webmd_data.py
# 2) Generate synthetic fine-tune set
python phase2_finetune/generate_synthetic.py
# 3) Train LoRA adapters
python phase2_finetune/train_med_lora.py
# 4) Align model with PPO reward
python phase3_rlhf/ppo_safety_train.py
# 5) Build interaction SQLite DB
python phase4_agent/build_interaction_db.py
# 6) Launch Streamlit app
streamlit run phase5_ui/app.py- Some scripts assume specific relative paths and naming conventions (for example model path aliases in Phase 4).
- If you move folders, update constants in:
phase1_rag/drug_retriever.pyphase4_agent/build_interaction_db.pyphase4_agent/safety_agent.pyphase4_agent/safety_agentv2.py
Outputs/ stores screenshot artifacts from project runs and UI execution captures. These are included in the repository as visual output evidence for the capstone pipeline.
MedLogix is an AI-assisted safety/documentation workflow and not a substitute for professional diagnosis or licensed clinical decision-making.