LoRA adapter on Qwen/Qwen3-1.7B-Base that reads a clinical case and a question, writes a priority level (urgence_maximale P1, urgence_moderee P2, differee P3), then a short answer. It is the proof of concept of a triage assistant for a fictional hospital (CHSA), built for the last project of an AI engineer training. Code: Christophe-Ringot/OC_P14_finetuning.
Not for clinical use. Trained on exam cases with LLM-made priority labels that no physician has validated. The free-text answers often contain medical errors (see Limits). Any real use needs a human to validate every decision.
q_proj, k_proj, v_proj, o_proj.c2a08bd), 5,345 training pairs after the length filter (MediQAl, FrenchMedMCQA, MedQuAD), anonymised with Presidio and regular expressions. Answers on annotated cases start with [Niveau de priorité estimé : <level>].Do not parse the generated text: score the three levels and apply the decision rule in decision_rule.json.
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-lora").eval()
labels = ["urgence_maximale", "urgence_moderee", "differee"]
cost = torch.tensor([[0, 4, 10], [1, 0, 4], [2, 1, 0]], dtype=torch.float)
cost = torch.where(torch.triu(torch.ones(3, 3), 1) > 0, cost * 0.5, cost) # alpha = 0.5 on under-triage
prompt = "### Cas clinique :\n<case>\n\n### Question :\n<question>\n\n### Réponse :\n[Niveau de priorité estimé :"
def logp(label):
# Context and continuation are tokenized apart, as in the evaluation.
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.25).softmax(-1) # temperature fitted on validation
level = labels[int((proba @ cost).argmin())] # minimum expected cost
Test: 400 questions on 109 annotated cases (clinical eval set). Validation is a separate, harder set of about 70 cases: quote both.
| Decision | Split | Accuracy | Macro F1 | P1 recall | Under-triage | Over-triage |
|---|---|---|---|---|---|---|
| Most likely level | test | 78.8 | 78.7 | 86.5 | 7.0 | 14.2 |
| Most likely level | validation | 62.6 | 62.9 | 72.4 | 16.2 | 21.3 |
| Cost rule (alpha 0.5) | test | 70.8 | 69.8 | 90.2 | 4.2 | 25.0 |
| Cost rule (alpha 0.5) | validation | 66.9 | 66.6 | 89.6 | 4.9 | 28.2 |
No P1 case is ever predicted P3. For comparison, the base model reaches 27.8 % accuracy and the SFT without triage examples 32.2 %.
Other checks, same protocol for every model:
Qwen/Qwen3-1.7B-Base