Skip to content

feat: Add YuFeng XGuard template support for training#8179

Open
ciaoyizhen wants to merge 3 commits intomodelscope:mainfrom
ciaoyizhen:main
Open

feat: Add YuFeng XGuard template support for training#8179
ciaoyizhen wants to merge 3 commits intomodelscope:mainfrom
ciaoyizhen:main

Conversation

@ciaoyizhen
Copy link
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

This PR adds the YuFeng_XGuard_Template to 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

  • Added YuFeng_XGuard_Template in swift/template/templates/qwen.py
  • Template follows the Qwen chat format (<|im_start|> / <|im_end|>) and supports 29 safety classification categories

Scripts

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 false

Result

1772534274634_b2c7879406b545e9916bc95b1ae66b26 7d137932b5e1917dd0ba10667fa983d7

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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, YuFeng_XGuard_Template, to facilitate the training of content safety models, specifically the YuFengGuard model. This enhancement allows for robust classification of text into numerous predefined safety categories, improving the system's ability to identify and manage various forms of inappropriate content.

Highlights

  • New Template for Content Safety: Introduced YuFeng_XGuard_Template to enable training with the YuFengGuard content safety model, specifically designed for content safety classification tasks.
  • Extensive Category Support: The new template supports 29 distinct safety classification categories, allowing for detailed categorization of input text into various risk types.
  • Qwen Chat Format Adherence: The YuFeng_XGuard_Template adheres to the standard Qwen chat format (<|im_start|> / <|im_end|>), ensuring compatibility and ease of integration within the existing system.

🧠 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
  • swift/template/constant.py
    • Added yufeng_xguard to the LLMTemplateType enum.
  • swift/template/templates/qwen.py
    • Defined the YuFeng_XGuard_Template string, which includes a detailed prompt for content safety evaluation with 29 categories and specific instructions for classification and justification.
    • Registered the YuFeng_XGuard_Template using Qwen3MixedTemplateMeta and LLMTemplateType.yufeng_xguard.
Activity
  • No human activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1151 to +1196
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],
))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
  1. 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_Template is a module-level constant and should be named YUFENG_XGUARD_TEMPLATE for consistency with this convention and other constants in the file like QWEN3_GUARD_TEMPLATE. (link)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK 迟点改一下

@ciaoyizhen
Copy link
Contributor Author

from transformers import AutoTokenizer, AutoModel
from rich import print
checkpoint = "/data2/open-source-model/XGuard-Reason-0.6B"
# checkpoint = "/data2/open-source-model/Qwen3Guard-Gen-4B"
tokenizer = AutoTokenizer.from_pretrained(checkpoint)


messages = [
    {
        "role": "user",
        "content": "{{QUERY}}"
    }
]
# print(tokenizer.chat_template)

response = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    enable_thinking=False
)
print(repr(response))

生成模版的方式如上 然后手动删除了think对应的标签 具体是参照Qwen3-Guard

PS: Qwen3-Guard那个提示词不对。。。 他只做了prompt检测部分 没做content检测部分。。。

@ciaoyizhen
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +1151 to +1192
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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
"""

Copy link
Contributor Author

@ciaoyizhen ciaoyizhen Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不行 这个是X-Guard的模版 不能这样删除\n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant