p14-triage-qwen3-1.7B-dpo-lora
LoRA and DPO for medical triage
2 min readupdated
This project is an experimental LoRA adapter for the Qwen/Qwen3-1.7B-Base model, further aligned using DPO (Direct Preference Optimization) for the task of medical triage (patient sorting). The model analyzes a clinical case and a question, then determines the urgency level and provides a brief explanation. This model was created as part of an educational project to simulate emergency department operations and allows users to explore the features of applying DPO on top of SFT adapters.
Key features
- Base model: built on
Qwen/Qwen3-1.7B-Basethrough the sequential application of SFT and DPO. - Decision-making algorithm: uses a cost matrix and custom temperature to determine the final priority instead of simple text parsing.
- Experimental status: compared against a basic SFT adapter, showing interesting results in fair blind testing.
- Open source: a fully reproducible training pipeline based on the
peftlibrary and TRL.
What you can do
- Run locally: integrate the LoRA adapter into a Python script to evaluate clinical cases.
- Explore SFT and DPO comparison: investigate why direct preference optimization in this experiment did not improve text response quality compared to pure SFT.
- Test on datasets: run the model on a test set to evaluate accuracy metrics.
Example
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer
base = "Qwen/Qwen3-1.7B-Base"
tok = AutoTokenizer.from_pretrained(base)
model = PeftModel.from_pretrained(AutoModelForCausalLM.from_pretrained(base, dtype=torch.bfloat16, device_map="auto"),
"christophe-ringot/p14-triage-qwen3-1.7b-dpo-lora").eval()
labels = ["urgence_maximale", "urgence_moderee", "differee"]
cost = torch.tensor([[0, 4, 10], [1, 0, 4], [2, 1, 0]], dtype=torch.float)
prompt = "### Cas clinique :\n\n\n### Question :\n\n\n### Réponse :\n[Niveau de priorité estimé :"
def logp(label):
context = tok(prompt, add_special_tokens=False).input_ids
continuation = tok(f" {label}]", add_special_tokens=False).input_ids
ids = torch.tensor([context + continuation], device=model.device)
with torch.no_grad():
logits = model(input_ids=ids).logits[0, len(context) - 1:-1].float().log_softmax(-1)
return logits.gather(1, torch.tensor(continuation, device=model.device).unsqueeze(1)).sum().item()
scores = torch.tensor([logp(l) for l in labels])
proba = (scores / 2.0).softmax(-1)
level = labels[int((proba @ cost).argmin())]
print(level)
Read more
Details• Training: TRL, beta 0.1, lr 2e-5, 1 epoch, batch 8, bf16, RTX 2060 6 GB.
• Dataset: 785 training and 95 validation pairs.
• Metrics (test): Accuracy 76.5% (with cost rule — 77.8%), Macro F1 76.3%.
• Limitations: the model is not validated by doctors, texts may contain medical errors, and it is not intended for real clinical use.










Comments