A LoRA adapter for
meta-llama/Llama-3.1-8B-Instruct that answers adverse drug event (ADE) questions on single-sentence clinical text and extracts the implicated drug and event as structured JSON. Distilled from a Vertex-hosted Llama 3.3 70B teacher; trained with QLoRA on ~3k teacher-labeled sentences from
ade_corpus_v2.
1{
2 "answer": "yes | no | abstain",
3 "drug": "<drug name or empty>",
4 "event": "<adverse event or empty>",
5 "evidence": "<quoted or closely paraphrased text>",
6 "short_justification": "<one short sentence>",
7 "confidence": 0.0
8}
1from peft import PeftModel
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3import torch
4
5base_id = "meta-llama/Llama-3.1-8B-Instruct"
6adapter_id = "Ventali/llama31-8b-ade-sft-v2"
7
8bnb = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_quant_type="nf4",
11 bnb_4bit_use_double_quant=True,
12 bnb_4bit_compute_dtype=torch.bfloat16,
13)
14tokenizer = AutoTokenizer.from_pretrained(base_id)
15model = AutoModelForCausalLM.from_pretrained(base_id, quantization_config=bnb, device_map="auto")
16model = PeftModel.from_pretrained(model, adapter_id)
17model.eval()
18
19messages = [
20 {"role": "system", "content": "You are a careful biomedical assistant. For each case, return a compact JSON answer grounded in the provided evidence. If the evidence is insufficient, abstain."},
21 {"role": "user", "content": "Case: The patient developed diffuse urticaria three days after starting amoxicillin.\n\nIs this consistent with a possible adverse drug event? Identify the drug and event if so, or abstain if the evidence is insufficient."},
22]
23prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
24inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
25with torch.no_grad():
26 out = model.generate(**inputs, max_new_tokens=256, do_sample=False, pad_token_id=tokenizer.eos_token_id)
27print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
1pip install mlx-lm
2mlx_lm.fuse --model meta-llama/Llama-3.1-8B-Instruct \
3 --adapter-path <local-adapter-dir> \
4 --save-path ~/models/llama31-ade-mlx
5mlx_lm.generate --model ~/models/llama31-ade-mlx --prompt "..."
Full pipeline (seed building, teacher generation config, filter, SFT prep, training, evaluation) lives at
https://github.com/ventali/medical-distill. Commit
547629f records this adapter's metrics.