Home

Awesome

medalpaca

medAlpaca: Finetuned Large Language Models for Medical Question Answering

Project Overview

MedAlpaca expands upon both Stanford Alpaca and AlpacaLoRA to offer an advanced suite of large language models specifically fine-tuned for medical question-answering and dialogue applications. Our primary objective is to deliver an array of open-source language models, paving the way for seamless development of medical chatbot solutions.

These models have been trained using a variety of medical texts, encompassing resources such as medical flashcards, wikis, and dialogue datasets. For more details on the data utilized, please consult the data section.

Getting Started

Create a new virtual environment, e.g. with conda

conda create -n medalpaca python>=3.9

Install the required packages:

pip install -r requirements.txt

Training of medAlpaca

<img width="256" alt="training your alpaca" src="https://user-images.githubusercontent.com/37253540/229250535-98f28e1c-0a8e-46e7-9e61-aeb98ef115cc.png">

Memory Requirements

We have benchmarked the needed GPU memory as well as the approximate duration per epoch for finetuning LLaMA 7b on the Medical Meadow small dataset (~6000 Q/A pairs) on a single GPU:

Model8bit trainigLoRAfp16bf16VRAM UsedGradient cktpDuration/epoch
LLaMA 7bTrueTrueTrueFalse8.9 GBFalse77:30
LLaMA 7bFalseTrueTrueFalse18.8 GBFalse14:30
LLaMA 7bFalseFalseTrueFalseOOMFalse-
LLaMA 7bFalseFalseFalseTrue79.5 GBTrue35:30
LLaMA 7bFalseFalseFalseFalseOOMTrue-

Train medAlpaca based on LLaMA

If you have access to the LLaMA or Alpaca weights you can finetune the model with the following command. Just replace <PATH_TO_LLAMA_WEIGHTS> with the folder containing you LLaMA or Alpaca weights.

python medalpaca/train.py \
    --model PATH_TO_LLAMA_WEIGHTS \
    --data_path medical_meadow_small.json \
    --output_dir 'output' \
    --train_in_8bit True \  
    --use_lora True \
    --bf16 True \
    --tf32 False \
    --fp16 False \
    --global_batch_size 128 \
    --per_device_batch_size 8 \

Per default the script performs mixed precision training.
You can toggle 8bit training with the train_in_8bit flag. While 8 bit training currently only works with use_lora True, however you can use LoRA without 8 bit training. It is also able to train other models such as facebook/opt-6.7 with the above script.

Data

<img width="256" alt="Screenshot 2023-03-31 at 09 37 41" src="https://user-images.githubusercontent.com/37253540/229244284-72b00e82-0da1-4218-b08e-63864306631e.png">

To ensure your cherished llamas and alpacas are well-fed and thriving, we have diligently gathered high-quality biomedical open-source datasets and transformed them into instruction tuning formats. We have dubbed this endeavor Medical Meadow. Medical Meadow currently encompasses roughly 1.5 million data points across a diverse range of tasks, including openly curated medical data transformed into Q/A pairs with OpenAI's gpt-3.5-turbo and a collection of established NLP tasks in the medical domain. Please note, that not all data is of the same quantitiy and quality and you may need tp subsample the data for training your own model. We will persistently update and refine the dataset, and we welcome everyone to contribute more 'grass' to Medical Meadow!

Data Overview

NameSourcenn included in training
Medical Flashcardsmedalpaca/medical_meadow_medical_flashcards3395533955
Wikidocmedalpaca/medical_meadow_wikidoc6770410000
Wikidoc Patient Informationmedalpaca/medical_meadow_wikidoc_patient_information59425942
Stackexchange academiamedalpaca/medical_meadow_stack_exchange4086540865
Stackexchange biologymedalpaca/medical_meadow_stack_exchange2788727887
Stackexchange fitnessmedalpaca/medical_meadow_stack_exchange98339833
Stackexchange healthmedalpaca/medical_meadow_stack_exchange77217721
Stackexchange bioinformaticsmedalpaca/medical_meadow_stack_exchange54075407
USMLE Self Assessment Step 1medalpaca/medical_meadow_usmle_self11992 (test only)
USMLE Self Assessment Step 2medalpaca/medical_meadow_usmle_self120110 (test only)
USMLE Self Assessment Step 3medalpaca/medical_meadow_usmle_self135122 (test only)
MEDIQAoriginal, preprocessed22082208
CORD-19original, preprocessed105666050000
MMMLUoriginal, preprocessed37873787
Pubmed Health Adviceoriginal, preprocessed1017810178
Pubmed Causaloriginal, preprocessed24462446
ChatDoctororiginal21500010000
OpenAssistantoriginal92099209

Data description

please refer to DATA_DESCRIPTION.md

Benchmarks

<img width="256" alt="benchmarks" src="https://user-images.githubusercontent.com/37253540/229249302-20ff8a88-95b4-42a3-bdd8-96a9dce9a92b.png">

We are benchmarking all models on the USMLE self assessment, which is available at this link. Note, that we removed all questions with images, as our models are not multimodal.

ModelStep1Step2Step3
LLaMA 7b0.1980.2020.203
Alpaca 7b naive (weights)0.2750.2660.293
Alpaca 7b LoRA0.2200.1380.252
MedAlpaca 7b0.2970.3120.398
MedAlpaca 7b LoRA0.2310.2020.179
MedAlpaca 7b LoRA 8bit0.2310.2410.211
ChatDoctor (7b)0.1870.1850.148
LLaMA 13b0.2220.2480.276
Alpaca 13b naive0.3190.3120.301
MedAlpaca 13b0.4730.4770.602
MedAlpaca 13b LoRA0.2500.2550.255
MedAlpaca 13b LoRA 8bit0.1890.3030.289
MedAlpaca 30b (still training)TBATBATBA
MedAlpaca 30b LoRA 8bit0.3150.3270.361

We are continuously working on improving the training as well as our evaluation prompts. Expect this table to change quite a bit.

Access the models

Visit the zoo and have a look at our alpacas here: https://huggingface.co/medalpaca

It should be obvious, but the models provided on this platform are shared for research purposes only and should not be used in any healthcare applications or settings. While we are excited to showcase our experimental models, please be aware that they have not undergone extensive testing or validation, and their reliability cannot be guaranteed. We kindly ask you to exercise caution when using these models, and we appreciate your understanding as we continue to explore and develop this innovative technology.

Paper

<img width="256" alt="chat-lama" src="https://user-images.githubusercontent.com/37253540/229261366-5cce9a60-176a-471b-80fd-ba390539da72.png">
@article{han2023medalpaca,
  title={MedAlpaca--An Open-Source Collection of Medical Conversational AI Models and Training Data},
  author={Han, Tianyu and Adams, Lisa C and Papaioannou, Jens-Michalis and Grundmann, Paul and Oberhauser, Tom and L{\"o}ser, Alexander and Truhn, Daniel and Bressem, Keno K},
  journal={arXiv preprint arXiv:2304.08247},
  year={2023}
}