🧠 Mental Health Triage — Phi-3-mini QLoRA Fine-Tune
A domain-specific fine-tuned version of microsoft/Phi-3-mini-4k-instruct trained to perform structured mental health triage classification from plain-text messages.
Given a person's self-described emotional or psychological state, the model produces a validated JSON triage response covering severity level, concern type, recommended clinical action, risk flags, an empathetic opening, and a follow-up question.
⚠️ Disclaimer: This model is for research and educational purposes only. It is not a substitute for professional mental health assessment or clinical diagnosis. Always refer people in crisis to qualified professionals and emergency services.
1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2import torch, json
34MODEL_ID ="kasi-ranaweera/mental-health-triage-phi3-qlora"56# Load with 4-bit quantization (recommended — fits on free GPU)7bnb_config = BitsAndBytesConfig(8 load_in_4bit=True,9 bnb_4bit_quant_type="nf4",10 bnb_4bit_compute_dtype=torch.bfloat16,11 bnb_4bit_use_double_quant=True12)1314tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)15model = AutoModelForCausalLM.from_pretrained(16 MODEL_ID,17 quantization_config=bnb_config,18 device_map="auto",19 trust_remote_code=True,20 torch_dtype=torch.bfloat16
21)2223SYSTEM_PROMPT ="""You are a mental health triage assistant. Analyze the person's message carefully and respond ONLY with a valid JSON object using this exact schema:
24{
25 "severity": "crisis | high | moderate | low",
26 "concern_type": "suicidal_ideation | self_harm | depression | anxiety | panic | ptsd | grief | burnout | loneliness | eating_disorder | substance_abuse | general_distress",
27 "recommended_action": "emergency_services | immediate_therapist | scheduled_therapist | self_help_resources | peer_support | monitoring",
28 "risk_flags": ["only flags explicitly present in the message"],
29 "empathetic_opening": "One warm validating sentence specific to this person",
30 "follow_up_question": "One clarifying question"
31}
32Rules: crisis → emergency_services or immediate_therapist only. No markdown. JSON only."""3334user_message ="I've been feeling really low for the past few weeks. I can't sleep, I've lost interest in things I used to enjoy, and I've been calling in sick to work. I just don't see the point anymore."3536prompt =(37f"<|system|>\n{SYSTEM_PROMPT}\n<|end|>\n"38f"<|user|>\n{user_message}\n<|end|>\n"39f"<|assistant|>\n"40)4142inputs = tokenizer(prompt, return_tensors="pt").to(model.device)43outputs = model.generate(44**inputs,45 max_new_tokens=300,46 temperature=0.1,47 do_sample=True,48 pad_token_id=tokenizer.eos_token_id
49)50generated = outputs[0][inputs["input_ids"].shape[1]:]51response = tokenizer.decode(generated, skip_special_tokens=True).strip()5253print(json.dumps(json.loads(response), indent=2))
Expected output:
json
1{2"severity":"high",3"concern_type":"depression",4"recommended_action":"immediate_therapist",5"risk_flags":["anhedonia","sleep_disruption","social_withdrawal","hopelessness"],6"empathetic_opening":"It sounds like you've been carrying something heavy for a while now, and losing interest in things you used to love is a real sign that you need support.",7"follow_up_question":"When you say you don't see the point anymore — can you tell me more about what that feels like for you?"8}
📋 Uses
Direct Use
This model is intended for:
Research into structured mental health NLP and triage automation
Educational demonstrations of domain-specific LLM fine-tuning
Prototype development for mental health support tools (with human oversight)
Benchmarking QLoRA fine-tuning on small-scale clinical datasets
Downstream Use
With additional development, this model could serve as a component in:
Mental health chatbot triage layers (with mandatory human review)
Support ticket severity routing systems
Clinical decision support prototypes (requiring clinical validation)
Out-of-Scope Use
Clinical diagnosis or treatment decisions — this model must not replace licensed clinicians
Crisis intervention without human oversight — always escalate crisis cases to emergency services
Deployment without safety guardrails — outputs must be reviewed by qualified professionals
Languages other than English — not trained on multilingual data
Paediatric populations — training data focused on adult presentations
⚠️ Bias, Risks, and Limitations
Clinical limitations:
Trained on 142 synthetic examples generated by an LLM teacher model — not reviewed by clinical psychologists
Severity boundary decisions (especially high vs moderate) may be inconsistent in edge cases
Risk flags are limited to a closed vocabulary of 12 concern types — novel presentations may be missed
Does not account for cultural differences in how mental health distress is expressed
Technical limitations:
Hallucination rate of 10.0% on held-out test set (10 manually reviewed responses)
Severity boundary confusion between high and moderate for anxiety-spectrum presentations
JSON parse failures possible on very long or unusual inputs (handle with try/except)
Performance on non-English text is untested and likely poor
Phi-3-mini tokeniser treats JSON as multi-token sequences — ROUGE-L is noisier than BERTScore for this task
Bias risks:
Training data was synthetically generated and may underrepresent minority demographics
Cultural, linguistic, and socioeconomic diversity in distress expression is limited
Model may reflect biases present in the teacher model (Groq Llama-3-70B / GPT-OSS-120B)
Recommendations
Always wrap model outputs in clinical human review before any action is taken
Implement a fallback to human triage for all crisis severity predictions
Do not use confidence scores alone to bypass human oversight
Regularly audit outputs across diverse demographic groups for systematic errors
📊 Training Details
Training Data
Dataset: Synthetically generated using Groq llama-3.3-70b-versatile (primary) and openai/gpt-oss-120b (fallback) as teacher models.
Total examples: 142
Format: JSONL with Phi-3 chat template (system / user / assistant turns)
All 7 attention + SwiGLU MLP layers for full semantic remapping
Learning rate
2e-4
Empirically validated for QLoRA (Dettmers et al., 2023)
LR scheduler
cosine
Prevents late-epoch format drift via gentle LR tail
Warmup ratio
0.03
3% steps warmup prevents gradient spikes at random adapter init
Epochs
3
Val loss decreases across all 3 epochs — confirmed in W&B run
Batch size
2
T4 VRAM limit (15GB); batch=4 causes OOM
Gradient accumulation
8
Effective batch = 16; simulates larger batch without VRAM increase
Max seq length
1024
Mental health examples ≤600 tokens; 1024 provides safe headroom
Gradient checkpointing
True
Recomputes activations in backward pass to reduce VRAM peak
Trainable Parameters
Trainable: 8,912,896 (0.233% of total)
Total: ~3.8 billion (Phi-3-mini base)
Validation Loss (per epoch)
Epoch
Validation Loss
1
1.0598
2
0.5949
3
0.4982 ✅
Loss decreases monotonically across all 3 epochs — no overfitting observed.
Speeds, Sizes, Times
Training time: ~35–45 minutes on Google Colab T4 GPU
VRAM usage: ~11GB peak during training (15GB available)
Merged model size: ~7.5GB (bfloat16 safetensors)
📈 Evaluation
Testing Data
Held-out test set: 15 examples (10% of the full 142-example dataset), same distribution as training set.
Baseline: identical microsoft/Phi-3-mini-4k-instruct base model with the same system prompt but no fine-tuning.
Metrics
Metric
Description
ROUGE-L
Longest common subsequence overlap between predicted and reference JSON
BERTScore F1
Semantic similarity via DistilBERT contextual embeddings
LLM-as-judge
Groq Llama-3-70B scores outputs on clinical rubric (structured JSON), n=10
Hallucination rate
% of manually reviewed responses with unsupported flags or severity-action mismatches
Results
Automatic Metrics
Metric
Base Model
Fine-Tuned
Delta
ROUGE-L
0.3494
0.3899
+0.0405
BERTScore F1
0.8742
0.8931
+0.0189
LLM-as-Judge Results (Groq Llama-3-70B, n=10)
Criterion
Base Model
Fine-Tuned
Overall score
0.000
0.000
% Correct verdicts
0%
0%
ℹ️ Note: LLM-as-judge scores of 0.000 for both models indicate that the judge model's structured JSON scoring rubric did not align with either model's output format on this evaluation run. Automatic ROUGE-L and BERTScore metrics, as well as manual hallucination review, are the primary evaluation signals for this task.
Hallucination Rate (Manual Review, n=10)
Label
Count
%
Correct
7
70%
Partially correct
2
20%
Hallucinated
1
10%
Hallucination rate
1
10.0%
Summary
Fine-tuning produced measurable improvements across all automatic metrics (+0.0405 ROUGE-L, +0.0189 BERTScore F1). The most significant gains were in severity–action consistency and risk flag grounding. The base model frequently recommended low-escalation actions for high-risk presentations; the fine-tuned model learned to apply the clinical severity-action rules enforced during training. Remaining failure modes include severity boundary confusion at the high/moderate boundary for anxiety-spectrum presentations without explicit functional impairment language.
🔍 Model Examination
Qualitative Analysis
Where fine-tuning improved performance:
The clearest improvement is in clinically consistent severity–action pairing. The base model assigned peer_support as the recommended action for passive suicidal ideation ("I sometimes wonder what's the point of going on"), while the fine-tuned model correctly escalated to immediate_therapist with severity=high. Risk flag grounding also improved significantly — the fine-tuned model learned to only include flags with textual evidence in the input, reducing hallucinated flags like substance_use or appetite_changes with no basis in the message.
Remaining failure modes:
The primary failure mode is severity boundary confusion between high and moderate for anxiety-spectrum presentations, particularly panic disorder with agoraphobia. Without explicit functional impairment language in the input, the model tends to classify these as moderate. This reflects training data imbalance — high-severity anxiety examples without suicidal ideation were underrepresented. A second data generation round with 30+ targeted examples and a DPO stage penalising generic empathetic openings would address the two main gaps.
🔄 RAG Fallback Layer
When the fine-tuned model's perplexity-normalised confidence falls below 0.65, the system retrieves relevant context from a ChromaDB vector store (10 clinical reference documents, embedded with all-MiniLM-L6-v2) and re-queries the model with the augmented prompt.