feat: Add YuFeng XGuard template support for training#8179
feat: Add YuFeng XGuard template support for training#8179ciaoyizhen wants to merge 3 commits intomodelscope:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new template, Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for the YuFeng_XGuard template for content safety classification tasks. The changes include adding the new template type in swift/template/constant.py and defining the template string and its registration in swift/template/templates/qwen.py. The implementation is straightforward. I've left one comment regarding a naming convention to improve code consistency.
swift/template/templates/qwen.py
Outdated
| YuFeng_XGuard_Template = ('<|im_start|>user\n' | ||
| 'You are an expert in content safety evaluation, possessing strong ' | ||
| 'language comprehension and risk identification skills.\n' | ||
| 'Your task is to classify the provided input text into the most appropriate ' | ||
| 'category from the list below as part of a safety audit project.\n\n' | ||
| '# Category List\n' | ||
| '- sec: Safe\n' | ||
| '- pc: Pornographic Contraband\n' | ||
| '- dc: Drug Crimes\n' | ||
| '- dw: Dangerous Weapons\n' | ||
| '- pi: Property Infringement\n' | ||
| '- ec: Economic Crimes\n' | ||
| '- ac: Abusive Curses\n' | ||
| '- def: Defamation\n' | ||
| '- ti: Threats and Intimidation\n' | ||
| '- cy: Cyberbullying\n' | ||
| '- ph: Physical Health\n' | ||
| '- mh: Mental Health\n' | ||
| '- se: Social Ethics\n' | ||
| '- sci: Science Ethics\n' | ||
| '- pp: Personal Privacy\n' | ||
| '- cs: Commercial Secret\n' | ||
| '- acc: Access Control\n' | ||
| '- mc: Malicious Code\n' | ||
| '- ha: Hacker Attack\n' | ||
| '- ps: Physical Security\n' | ||
| '- ter: Violent Terrorist Activities\n' | ||
| '- sd: Social Disruption\n' | ||
| '- ext: Extremist Ideological Trends\n' | ||
| '- fin: Finance\n' | ||
| '- med: Medicine\n' | ||
| '- law: Law\n' | ||
| '- cm: Corruption of Minors\n' | ||
| '- ma: Minor Abuse and Exploitation\n' | ||
| '- md: Minor Delinquency\n\n' | ||
| '# Instructions\n' | ||
| '- Identify the single most relevant category ID for the input text.\n' | ||
| '- On the next line, provide a concise justification for your choice, ' | ||
| 'placing it between <explanation> and </explanation> tags.\n\n' | ||
| '---\n\n' | ||
| 'Input Text: {{QUERY}}<|im_end|>\n' | ||
| '<|im_start|>assistant\n') | ||
| register_template(Qwen3MixedTemplateMeta( | ||
| LLMTemplateType.yufeng_xguard, | ||
| prompt=[YuFeng_XGuard_Template], | ||
| )) |
There was a problem hiding this comment.
For consistency with other template constants in this file (e.g., QWEN3_GUARD_TEMPLATE), it's recommended to rename YuFeng_XGuard_Template to YUFENG_XGUARD_TEMPLATE. This follows the PEP 8 convention for constants (uppercase with underscores).
YUFENG_XGUARD_TEMPLATE = ('<|im_start|>user\n'
'You are an expert in content safety evaluation, possessing strong '
'language comprehension and risk identification skills.\n'
'Your task is to classify the provided input text into the most appropriate '
'category from the list below as part of a safety audit project.\n\n'
'# Category List\n'
'- sec: Safe\n'
'- pc: Pornographic Contraband\n'
'- dc: Drug Crimes\n'
'- dw: Dangerous Weapons\n'
'- pi: Property Infringement\n'
'- ec: Economic Crimes\n'
'- ac: Abusive Curses\n'
'- def: Defamation\n'
'- ti: Threats and Intimidation\n'
'- cy: Cyberbullying\n'
'- ph: Physical Health\n'
'- mh: Mental Health\n'
'- se: Social Ethics\n'
'- sci: Science Ethics\n'
'- pp: Personal Privacy\n'
'- cs: Commercial Secret\n'
'- acc: Access Control\n'
'- mc: Malicious Code\n'
'- ha: Hacker Attack\n'
'- ps: Physical Security\n'
'- ter: Violent Terrorist Activities\n'
'- sd: Social Disruption\n'
'- ext: Extremist Ideological Trends\n'
'- fin: Finance\n'
'- med: Medicine\n'
'- law: Law\n'
'- cm: Corruption of Minors\n'
'- ma: Minor Abuse and Exploitation\n'
'- md: Minor Delinquency\n\n'
'# Instructions\n'
'- Identify the single most relevant category ID for the input text.\n'
'- On the next line, provide a concise justification for your choice, '
'placing it between <explanation> and </explanation> tags.\n\n'
'---\n\n'
'Input Text: {{QUERY}}<|im_end|>\n'
'<|im_start|>assistant\n')
register_template(Qwen3MixedTemplateMeta(
LLMTemplateType.yufeng_xguard,
prompt=[YUFENG_XGUARD_TEMPLATE],
))References
- According to PEP 8, constants are usually defined on a module level and written in all capital letters with underscores separating words. The variable
YuFeng_XGuard_Templateis a module-level constant and should be namedYUFENG_XGUARD_TEMPLATEfor consistency with this convention and other constants in the file likeQWEN3_GUARD_TEMPLATE. (link)
生成模版的方式如上 然后手动删除了think对应的标签 具体是参照Qwen3-Guard PS: Qwen3-Guard那个提示词不对。。。 他只做了prompt检测部分 没做content检测部分。。。 |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for the YuFeng XGuard template, enabling content safety classification training. The changes correctly integrate the new template type into the LLMTemplateType enum and define the corresponding prompt in qwen.py. The template adheres to the Qwen chat format and specifies 29 safety classification categories, aligning with the PR description.
| YUFENG_XGUARD_TEMPLATE = ('<|im_start|>user\n' | ||
| 'You are an expert in content safety evaluation, possessing strong ' | ||
| 'language comprehension and risk identification skills.\n' | ||
| 'Your task is to classify the provided input text into the most appropriate ' | ||
| 'category from the list below as part of a safety audit project.\n\n' | ||
| '# Category List\n' | ||
| '- sec: Safe\n' | ||
| '- pc: Pornographic Contraband\n' | ||
| '- dc: Drug Crimes\n' | ||
| '- dw: Dangerous Weapons\n' | ||
| '- pi: Property Infringement\n' | ||
| '- ec: Economic Crimes\n' | ||
| '- ac: Abusive Curses\n' | ||
| '- def: Defamation\n' | ||
| '- ti: Threats and Intimidation\n' | ||
| '- cy: Cyberbullying\n' | ||
| '- ph: Physical Health\n' | ||
| '- mh: Mental Health\n' | ||
| '- se: Social Ethics\n' | ||
| '- sci: Science Ethics\n' | ||
| '- pp: Personal Privacy\n' | ||
| '- cs: Commercial Secret\n' | ||
| '- acc: Access Control\n' | ||
| '- mc: Malicious Code\n' | ||
| '- ha: Hacker Attack\n' | ||
| '- ps: Physical Security\n' | ||
| '- ter: Violent Terrorist Activities\n' | ||
| '- sd: Social Disruption\n' | ||
| '- ext: Extremist Ideological Trends\n' | ||
| '- fin: Finance\n' | ||
| '- med: Medicine\n' | ||
| '- law: Law\n' | ||
| '- cm: Corruption of Minors\n' | ||
| '- ma: Minor Abuse and Exploitation\n' | ||
| '- md: Minor Delinquency\n\n' | ||
| '# Instructions\n' | ||
| '- Identify the single most relevant category ID for the input text.\n' | ||
| '- On the next line, provide a concise justification for your choice, ' | ||
| 'placing it between <explanation> and </explanation> tags.\n\n' | ||
| '---\n\n' | ||
| 'Input Text: {{QUERY}}<|im_end|>\n' | ||
| '<|im_start|>assistant\n') |
There was a problem hiding this comment.
For improved readability and easier editing of the prompt content, consider using a triple-quoted string for YUFENG_XGUARD_TEMPLATE. This allows the prompt to be written naturally across multiple lines without explicit \n characters and string concatenation, making it cleaner to manage, especially for long, structured text like this template.
YUFENG_XGUARD_TEMPLATE = """<|im_start|>user
You are an expert in content safety evaluation, possessing strong language comprehension and risk identification skills.
Your task is to classify the provided input text into the most appropriate category from the list below as part of a safety audit project.
# Category List
- sec: Safe
- pc: Pornographic Contraband
- dc: Drug Crimes
- dw: Dangerous Weapons
- pi: Property Infringement
- ec: Economic Crimes
- ac: Abusive Curses
- def: Defamation
- ti: Threats and Intimidation
- cy: Cyberbullying
- ph: Physical Health
- mh: Mental Health
- se: Social Ethics
- sci: Science Ethics
- pp: Personal Privacy
- cs: Commercial Secret
- acc: Access Control
- mc: Malicious Code
- ha: Hacker Attack
- ps: Physical Security
- ter: Violent Terrorist Activities
- sd: Social Disruption
- ext: Extremist Ideological Trends
- fin: Finance
- med: Medicine
- law: Law
- cm: Corruption of Minors
- ma: Minor Abuse and Exploitation
- md: Minor Delinquency
# Instructions
- Identify the single most relevant category ID for the input text.
- On the next line, provide a concise justification for your choice, placing it between <explanation> and </explanation> tags.
---
Input Text: {{QUERY}}<|im_end|>
<|im_start|>assistant
"""There was a problem hiding this comment.
不行 这个是X-Guard的模版 不能这样删除\n
PR type
PR information
This PR adds the
YuFeng_XGuard_Templateto support training with the YuFengGuard content safety model.The template is designed for content safety classification tasks, enabling the model to categorize input text into predefined risk categories (e.g., pornographic contraband, drug crimes, cyberbullying, etc.).
Changes
YuFeng_XGuard_Templateinswift/template/templates/qwen.py<|im_start|>/<|im_end|>) and supports 29 safety classification categoriesScripts
CUDA_VISIBLE_DEVICES=1 \ swift sft \ --model /data2/open-source-model/XGuard-Reason-0.6B \ --model_type qwen3 \ --template yufeng_xguard \ --tuner_type lora \ --dataset data/X-Guard-测试数据/train_data.jsonl \ --torch_dtype bfloat16 \ --num_train_epochs 1 \ --per_device_train_batch_size 1 \ --per_device_eval_batch_size 1 \ --learning_rate 1e-4 \ --target_modules all-linear \ --gradient_accumulation_steps 16 \ --eval_steps 50 \ --save_steps 50 \ --save_total_limit 2 \ --logging_steps 5 \ --max_length 2048 \ --output_dir output/qwen3_guard \ --warmup_ratio 0.05 \ --dataloader_num_workers 4 \ --enable_thinking falseResult