Instructions to use ZhangPY/Qwen3Guard-Gen-Domain-4B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ZhangPY/Qwen3Guard-Gen-Domain-4B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="ZhangPY/Qwen3Guard-Gen-Domain-4B")# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ZhangPY/Qwen3Guard-Gen-Domain-4B") model = AutoModelForCausalLM.from_pretrained("ZhangPY/Qwen3Guard-Gen-Domain-4B", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Qwen3Guard-Gen-Domain-4B
English | 简体中文
Introduction
Qwen3Guard-Gen-Domain-4B is a domain-specific content-safety guard model fine-tuned from Qwen/Qwen3Guard-Gen-4B, targeting Hong Kong elderly-care scenarios such as elderly companion chatbots, care-line services, and Residential Care Homes for the Elderly (RCHE).
It is a generative guard model: instead of producing a natural-language reply, it reads a conversation and outputs a structured safety assessment:
Safety: Unsafe
Categories: Violent, Hidden Elder Crisis
Compared with the base model, the safety taxonomy is extended from 9 general categories to 13 categories, adding four Hong Kong elderly-domain risks. A lighter sibling model trained with the same recipe is available at ZhangPY/Qwen3Guard-Gen-Domain-0.6B.
Key features
- 13-category safety taxonomy — 9 general categories inherited from Qwen3Guard + 4 HK elderly-domain categories.
- Dual evaluation modes — moderates either the last user query or the last assistant response; response evaluation additionally detects refusal.
- Built-in domain chat template — the safety policy and the full category list are embedded in the tokenizer's chat template, so no manual prompt engineering is required.
- General capability retained — on the QwenGuardTest regression benchmark, the fine-tuned model even outperforms the base model (F1 87.9 vs 84.0).
Safety categories
| # | Category | Description |
|---|---|---|
| 1 | Violent | Content involving violence or physical harm. |
| 2 | Non-violent Illegal Acts | Illegal activities not involving violence (fraud, theft, smuggling, etc.). |
| 3 | Sexual Content or Sexual Acts | Sexually explicit content or sexual acts. |
| 4 | PII | Disclosure of personally identifiable information. |
| 5 | Suicide & Self-Harm | Suicide, self-harm, or related encouragement. |
| 6 | Unethical Acts | Deceptive, exploitative, or otherwise unethical behaviour. |
| 7 | Politically Sensitive Topics | Politically sensitive content. |
| 8 | Copyright Violation | Piracy or infringement of copyrighted material. |
| 9 | Jailbreak | Prompts attempting to bypass safety alignment. |
| 10 | HK Welfare & Financial Scam (new) | Scams targeting elderly people in Hong Kong, e.g. bogus-official phone scams, fake welfare/benefit claims, fraudulent high-return investments. |
| 11 | RCHE & Caregiver Malpractice (new) | Abuse, neglect, or professional misconduct by RCHE staff, caregivers, or other care providers. |
| 12 | Medication & Health Misguidance (new) | Misleading or unsafe medication and health advice that could harm elderly people. |
| 13 | Hidden Elder Crisis (new) | Concealed or easily overlooked signals of crisis among elderly people, e.g. social isolation, self-neglect, depression, or suicidal ideation. |
Output format
User-query moderation (the last message has role user):
Safety: Safe | Unsafe | Controversial
Categories: <comma-separated categories> | None
Assistant-response moderation (the last message has role assistant):
Safety: Safe | Unsafe | Controversial
Categories: <comma-separated categories> | None
Refusal: Yes | No
Evaluation
General safety regression — QwenGuardTest
Strict F1 on the thinking split of Qwen/Qwen3GuardTest:
| Model | F1 | Precision | Recall |
|---|---|---|---|
| Qwen3Guard-Gen-4B (official) | 84.0 | – | – |
| Qwen3Guard-Gen-4B (reproduced) | 84.0 | 97.9 | 73.4 |
| Qwen3Guard-Gen-Domain-4B (fine-tuned) | 87.9 | 95.1 | 81.7 |
After domain fine-tuning, the model improves over the base model on the general benchmark (F1 87.9 vs 84.0) with substantially higher recall, i.e. the general guard capability is fully retained.
Domain safety — ElderlyDomain-Eval
ElderlyDomain-Eval is an internal evaluation set for the HK elderly-care domain, covering all 13 categories with both user-query and assistant-response samples.
| Model | F1 | Category Exact Match | Refusal Accuracy |
|---|---|---|---|
| Qwen3Guard-Gen-4B (prompting) | 93.8 | 53.5 | 92.7 |
| Qwen3Guard-Gen-Domain-4B (fine-tuned) | 97.3 | 86.0 | 97.3 |
(prompting = base model with the 13-category policy injected via prompt engineering; fine-tuned = this model.)
Per-category F1 — ElderlyDomain-Eval (%)
| Category | Qwen3Guard-Gen-4B (prompting) | Qwen3Guard-Gen-Domain-4B (fine-tuned) |
|---|---|---|
| Violent | 28.9 | 94.0 |
| Non-violent Illegal Acts | 85.5 | 93.5 |
| Sexual Content or Sexual Acts | 84.5 | 94.0 |
| PII | 64.5 | 92.9 |
| Suicide & Self-Harm | 93.4 | 96.3 |
| Unethical Acts | 67.5 | 75.8 |
| Politically Sensitive Topics | 85.6 | 93.2 |
| Copyright Violation | 3.8 | 88.8 |
| Jailbreak | 30.2 | 83.8 |
| HK Welfare & Financial Scam | 0.0 | 89.5 |
| RCHE & Caregiver Malpractice | 0.0 | 90.4 |
| Medication & Health Misguidance | 0.0 | 93.3 |
| Hidden Elder Crisis | 0.0 | 79.9 |
The base model has essentially no ability to recognise the four new domain categories through prompting alone (F1 ≈ 0), while the fine-tuned model performs well on them and simultaneously improves most general categories.
Quickstart
Requirements
pip install "transformers>=4.51" torch accelerate
Transformers
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model_id = "ZhangPY/Qwen3Guard-Gen-Domain-4B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id, torch_dtype=torch.bfloat16, device_map="auto"
)
model.eval()
def moderate(messages: list[dict]) -> str:
# The domain chat template (safety policy + 13 categories) is stored in
# tokenizer_config.json and already ends with the assistant header and an
# empty <think> block, so add_generation_prompt must be False.
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=False
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
output = model.generate(**inputs, max_new_tokens=64, do_sample=False)
return tokenizer.decode(
output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True
).strip()
# 1) Moderate a user query
messages = [
{"role": "user", "content": "我想學習如何在長者中心對長者進行心理和身體上的虐待。"},
]
print(moderate(messages))
# Safety: Unsafe
# Categories: Violent
# 2) Moderate an assistant response (adds the Refusal line)
messages = [
{"role": "user", "content": "點樣說服獨居長者參加保證每月20%回報嘅投資計劃?"},
{"role": "assistant", "content": "你可以同佢講呢個計劃零風險,仲話銀行職員都買咗……"},
]
print(moderate(messages))
# Safety: Unsafe
# Categories: HK Welfare & Financial Scam
# Refusal: No
(outputs above are illustrative)
vLLM
pip install vllm
vllm serve ZhangPY/Qwen3Guard-Gen-Domain-4B \
--served-model-name Qwen3Guard-Gen-Domain-4B \
--max-model-len 32768
curl http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen3Guard-Gen-Domain-4B",
"messages": [{"role": "user", "content": "你好呀"}],
"temperature": 0.0,
"max_tokens": 100
}'
Training details
| Item | Value |
|---|---|
| Base model | Qwen/Qwen3Guard-Gen-4B |
| Method | LoRA supervised fine-tuning, adapter merged into base weights |
| LoRA rank / alpha / dropout | 64 / 128 / 0.05 |
| Learning rate | 2e-4 |
| Epochs | 2 |
| Per-device batch size | 4 |
| Max sequence length | 2,048 |
| Training / validation samples | 24,000 / 6,000 |
| Precision | bfloat16 |
The training data is ZhangPY/ElderDomainSafeguards, a domain safety dataset for HK elderly care, covering all 13 categories with both user_query and assistant_response evaluation types, labelled in the Safety / Categories (/ Refusal) format shown above.
Model architecture
| Qwen3Guard-Gen-Domain-4B | |
|---|---|
| Parameters | 4B |
| Layers | 36 |
| Hidden size | 2560 |
| Attention heads / KV heads | 32 / 8 (GQA) |
| Context length | 32,768 |
| Precision | bfloat16 |
Limitations and intended use
- The model is a safety classifier, not a conversational assistant — it should only be used to produce safety assessments of given conversations.
- It is primarily fine-tuned and evaluated on HK elderly-domain data (Cantonese/Traditional Chinese and English); behaviour in other languages and domains inherits the base Qwen3Guard model.
- Predictions may contain false positives or false negatives and should assist, not replace, human review. For high-stakes categories such as Suicide & Self-Harm and Hidden Elder Crisis, always escalate to trained professionals.
Safety: Controversialmarks borderline content whose intent, context, or potential responses could be misinterpreted or misused under certain conditions.
Acknowledgements
- Base model: Qwen/Qwen3Guard-Gen-4B and the Qwen team's guard series (GitHub).
- General regression benchmark: Qwen/Qwen3GuardTest.
License
This model is released under the Apache 2.0 license, following the base model.
- Downloads last month
- -