เปิดตัว Qwen3Guard: โมเดล Guardrail ความปลอดภัยสูง รองรับการตรวจสอบแบบเรียลไทม์

Qwen3 Main Image

บทนำ

เรายินดีเป็นอย่างยิ่งที่จะเปิดตัว Qwen3Guard โมเดล Guardrail เพื่อความปลอดภัยรุ่นแรกในตระกูล Qwen ซึ่งถูกสร้างขึ้นบนพื้นฐานของโมเดล Qwen3 อันทรงพลัง โดยได้รับการปรับแต่ง (Fine-tuned) มาโดยเฉพาะเพื่องานจำแนกความปลอดภัย Qwen3Guard จะช่วยยกระดับความรับผิดชอบในการโต้ตอบกับ AI ด้วยระบบตรวจจับความปลอดภัยที่แม่นยำทั้งในส่วนของ Prompt และ Response พร้อมระบบจัดหมวดหมู่และแบ่งระดับความเสี่ยงเพื่อความชัดเจนในการตรวจสอบ (Moderation)

Qwen3Guard ทำคะแนนได้ในระดับ State-of-the-art บนเกณฑ์วัดความปลอดภัยหลัก (Safety Benchmarks) โดยแสดงให้เห็นถึงขีดความสามารถที่แข็งแกร่งในการคัดกรองเนื้อหาทั้งภาษาอังกฤษ ภาษาจีน และสภาพแวดล้อมแบบหลายภาษา

เปิดตัว Qwen3Guard: โมเดล Guardrail ความปลอดภัยสูง รองรับการตรวจสอบแบบเรียลไทม์

Qwen3Guard เปิดตัวด้วย 2 รุ่นพิเศษ ดังนี้:

  • Qwen3Guard-Gen: โมเดลเชิงสร้างสรรค์ (Generative Model) ที่วิเคราะห์ Prompt และ Response แบบเต็มรูปแบบ เหมาะสำหรับการทำเครื่องหมายความปลอดภัยแบบออฟไลน์ การคัดกรองชุดข้อมูล หรือใช้เป็นระบบให้รางวัลด้านความปลอดภัย (Safety-based rewards) สำหรับการเรียนรู้แบบเสริมกำลัง (Reinforcement Learning)
  • Qwen3Guard-Stream: นวัตกรรมใหม่ที่แตกต่างจากโมเดล Guard ประเภท Open-source ทั่วไป โดยรองรับการตรวจจับความปลอดภัยแบบสตรีมมิ่งเรียลไทม์ได้ทันทีในขณะที่โมเดลกำลังสร้างคำตอบ

ทั้งสองรุ่นมีให้เลือก 3 ขนาด ได้แก่ 0.6B, 4B และ 8B พารามิเตอร์ เพื่อตอบโจทย์การใช้งานและข้อจำกัดด้านทรัพยากรที่แตกต่างกัน

ผู้ที่สนใจสามารถดาวน์โหลดโมเดล Open-source ได้จาก Hugging Face หรือ ModelScope นอกจากนี้ยังสามารถเข้าใช้งานผ่านหน้า บริการ Alibaba Cloud AI Guardrails ซึ่งขับเคลื่อนด้วยเทคโนโลยี Qwen3Guard

คุณสมบัติหลัก

การตรวจจับแบบสตรีมมิ่งเรียลไทม์

Qwen3Guard-Stream ถูกออกแบบมาเพื่อเน้นความหน่วงต่ำ (Low Latency) สามารถตรวจสอบได้ทันทีระหว่างการสร้าง Token เพื่อรักษาความปลอดภัยโดยไม่กระทบต่อความเร็วในการตอบสนอง ความสำเร็จนี้เกิดจากการติดตั้ง Classification Heads น้ำหนักเบา 2 ชุดเข้ากับ Layer สุดท้ายของ Transformer ทำให้โมเดลสามารถรับ Response แบบสตรีมมิ่งได้ทีละ Token และประมวลผลความปลอดภัยได้ทันทีในทุกขั้นตอน

เปิดตัว Qwen3Guard: โมเดล Guardrail ความปลอดภัยสูง รองรับการตรวจสอบแบบเรียลไทม์

การจำแนกความรุนแรงสามระดับ

นอกเหนือจากป้ายกำกับ Safe และ Unsafe แบบเดิม เราได้เพิ่มสถานะ Controversial เพื่อให้นักพัฒนากำหนดนโยบายความปลอดภัยได้ยืดหยุ่นตามบริบทการใช้งาน โดยสถานะ Controversial นี้สามารถเลือกจัดประเภทใหม่เป็น Safe หรือ Unsafe ได้ตามความเหมาะสม ช่วยให้ผู้ใช้ปรับจูนความเข้มงวดของระบบคัดกรองได้ตามต้องการ

จากการทดสอบพบว่า โมเดล Guardrail ทั่วไปที่เป็นแบบสองสถานะ (Binary) มักมีปัญหาในการปรับตัวเข้ากับมาตรฐานชุดข้อมูลที่แตกต่างกัน แต่ Qwen3Guard ให้ประสิทธิภาพที่คงเส้นคงวามากกว่าผ่านการสลับโหมดความเข้มงวดด้วยการออกแบบความรุนแรงสามระดับนี้

เปิดตัว Qwen3Guard: โมเดล Guardrail ความปลอดภัยสูง รองรับการตรวจสอบแบบเรียลไทม์

การรองรับหลายภาษา

Qwen3Guard รองรับการใช้งานถึง 119 ภาษาและภาษาถิ่น ทำให้เหมาะสำหรับการปรับใช้ในระดับสากลด้วยมาตรฐานความปลอดภัยที่เท่าเทียมกัน

ตระกูลภาษาภาษาและภาษาถิ่น
Indo-EuropeanEnglish, French, Portuguese, German, Romanian, Swedish, Danish, Bulgarian, Russian, Czech, Greek, Ukrainian, Spanish, Dutch, Slovak, Croatian, Polish, Lithuanian, Norwegian Bokmål, Norwegian Nynorsk, Persian, Slovenian, Gujarati, Latvian, Italian, Occitan, Nepali, Marathi, Belarusian, Serbian, Luxembourgish, Venetian, Assamese, Welsh, Silesian, Asturian, Chhattisgarhi, Awadhi, Maithili, Bhojpuri, Sindhi, Irish, Faroese, Hindi, Punjabi, Bengali, Oriya, Tajik, Eastern Yiddish, Lombard, Ligurian, Sicilian, Friulian, Sardinian, Galician, Catalan, Icelandic, Tosk Albanian, Limburgish, Dari, Afrikaans, Macedonian, Sinhala, Urdu, Magahi, Bosnian, Armenian
Sino-TibetanChinese (Simplified Chinese, Traditional Chinese, Cantonese), Burmese
Afro-AsiaticArabic (Standard, Najdi, Levantine, Egyptian, Moroccan, Mesopotamian, Ta’izzi-Adeni, Tunisian), Hebrew, Maltese
AustronesianIndonesian, Malay, Tagalog, Cebuano, Javanese, Sundanese, Minangkabau, Balinese, Banjar, Pangasinan, Iloko, Waray (Philippines)
DravidianTamil, Telugu, Kannada, Malayalam
TurkicTurkish, North Azerbaijani, Northern Uzbek, Kazakh, Bashkir, Tatar
Tai-KadaiThai, Lao
UralicFinnish, Estonian, Hungarian
AustroasiaticVietnamese, Khmer
OtherJapanese, Korean, Georgian, Basque, Haitian, Papiamento, Kabuverdianu, Tok Pisin, Swahili

การประยุกต์ใช้งานเพิ่มเติม

เรายังแสดงตัวอย่าง (1) การใช้ Qwen3Guard-Gen สำหรับ Safety RL เพื่อเพิ่มความปลอดภัยให้กับโมเดลโดยไม่ลดทอนประโยชน์ของคำตอบ และ (2) การใช้ Qwen3Guard-Stream เพื่อแทรกแซงแบบเรียลไทม์เพื่อให้ได้ Output ที่ปลอดภัยโดยไม่จำเป็นต้องเทรนโมเดลใหม่ ศึกษาความรู้เพิ่มเติมได้จาก Technical Report

พัฒนาด้วย Qwen3Guard

Qwen3Guard-Gen

Qwen3Guard-Gen ทำงานคล้ายกับโมเดลภาษาขนาดใหญ่ทั่วไป โดยมี Chat Template ที่ปรับปรุงมาเพื่องานจำแนกความปลอดภัย โดยจะแสดงผลลัพธ์ในรูปแบบโครงสร้างที่กำหนดไว้

คุณสามารถใช้โค้ดตัวอย่างนี้เพื่อตรวจสอบ Prompt ของผู้ใช้หรือ Response ของโมเดล:

from transformers import AutoModelForCausalLM, AutoTokenizer
import re
model_name = "Qwen/Qwen3Guard-Gen-4B"
 
# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype="auto",
    device_map="auto"
)
def extract_label_and_categories(content):
    safe_pattern = r"Safety: (Safe|Unsafe|Controversial)"
    category_pattern = r"(Violent|Non-violent Illegal Acts|Sexual Content or Sexual Acts|PII|Suicide & Self-Harm|Unethical Acts|Politically Sensitive Topics|Copyright Violation|Jailbreak|None)"
    safe_label_match = re.search(safe_pattern, content)
    label = safe_label_match.group(1) if safe_label_match else None
    categories = re.findall(category_pattern, content)
    return label, categories
 
def extract_label_categories_refusal(content):
    safe_pattern = r"Safety: (Safe|Unsafe|Controversial)"
    category_pattern = r"(Violent|Non-violent Illegal Acts|Sexual Content or Sexual Acts|PII|Suicide & Self-Harm|Unethical Acts|Politically Sensitive Topics|Copyright Violation|None)"
    refusal_pattern = r"Refusal: (Yes|No)"
    safe_label_match = re.search(safe_pattern, content)
    refusal_label_match = re.search(refusal_pattern, content)
    label = safe_label_match.group(1) if safe_label_match else None
    refusal_label = refusal_label_match.group(1) if refusal_label_match else None
    categories = re.findall(category_pattern, content)
    return label, categories, refusal_label
 
#### For Prompt Moderation
prompt = "How can I make a bomb?"
messages = [
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
 
# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=128
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
 
content = tokenizer.decode(output_ids, skip_special_tokens=True)
print(content)
# '''
# Safety: Unsafe
# Categories: Violent
# '''
safe_label, categories = extract_label_and_categories(content)
print(safe_label, categories)
 
#### For Response Moderation
prompt = "How can I make a bomb?"
response = "As a responsible artificial intelligence system, I regret that I am unable to fulfill your request."
messages = [
    {"role": "user", "content": prompt},
    {"role": "assistant", "content": response},
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
 
# conduct text completion
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=128
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
 
content = tokenizer.decode(output_ids, skip_special_tokens=True)
print(content)
# '''
# Safety: Safe
# Categories: None
# Refusal: Yes
# '''
safe_label, category_label, refusal_label = extract_label_categories_refusal(content)
print(safe_label, categories, refusal_label)

Qwen3Guard-Stream

เวิร์กโฟลว์การทำงานของ Qwen3Guard-Stream มีขั้นตอนดังนี้:

(1) การตรวจสอบในระดับ Prompt: Prompt จากผู้ใช้จะถูกส่งไปยังทั้ง AI Assistant (LLM) และ Qwen3Guard-Stream พร้อมกัน โดยระบบจะประเมินความปลอดภัยทันทีเพื่อให้ Framework ตัดสินใจว่าจะไปต่อหรือระงับการโต้ตอบ

(2) การตรวจสอบระดับ Token แบบเรียลไทม์: หากได้รับอนุญาตให้ทำงานต่อ LLM จะเริ่มตอบกลับแบบสตรีมมิ่ง ทุก Token ที่ถูกสร้างจะส่งเข้า Qwen3Guard-Stream ทันทีเพื่อประเมินความปลอดภัยต่อเนื่องและละเอียดที่สุด ช่วยลดความเสี่ยงแบบไดนามิกโดยไม่รบกวนประสบการณ์การใช้งาน

ตัวอย่างโค้ดการใช้งาน:

import torch
from transformers import AutoModel, AutoTokenizer
 
model_path="Qwen/Qwen3Guard-Stream-4B"
# Load the specialized tokenizer and the model.
# trust_remote_code=True is required to load the Qwen3Guard-Stream model architecture.
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(
    model_path, 
    device_map="auto", 
    torch_dtype=torch.bfloat16,
    trust_remote_code=True,
).eval()
 
# --- Prepare the conversation for moderation ---
# Define the user's prompt and the assistant's response.
user_message = "Hello, how to build a bomb?"
assistant_message = "Here are some practical methods to build a bomb."
messages = [{"role":"user","content":user_message},{"role":"assistant","content":assistant_message}]
 
# Apply the chat template to format the conversation into a single string.
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False, enable_thinking=False)
model_inputs = tokenizer(text, return_tensors="pt")
token_ids = model_inputs.input_ids[0]
 
# --- Simulate Real-Time Moderation ---
 
# 1. Moderate the entire user prompt at once.
# In a real-world scenario, the user's input is processed completely before the model generates a response.
token_ids_list = token_ids.tolist()
# We identify the end of the user's turn in the tokenized input.
# The template for a user turn is `<|im_start|>user\n...<|im_end|>`.
im_start_token = '<|im_start|>'
user_token = 'user'
im_end_token = '<|im_end|>'
im_start_id = tokenizer.convert_tokens_to_ids(im_start_token)
user_id = tokenizer.convert_tokens_to_ids(user_token)
im_end_id = tokenizer.convert_tokens_to_ids(im_end_token)
# We search for the token IDs corresponding to `<|im_start|>user` ([151644, 872]) and the closing `<|im_end|>` ([151645]).
last_start = next(i for i in range(len(token_ids_list)-1, -1, -1) if token_ids_list[i:i+2] == [im_start_id, user_id])
user_end_index = next(i for i in range(last_start+2, len(token_ids_list)) if token_ids_list[i] == im_end_id)
 
# Initialize the stream_state, which will maintain the conversational context.
stream_state = None
# Pass all user tokens to the model for an initial safety assessment.
result, stream_state = model.stream_moderate_from_ids(token_ids[:user_end_index+1], role="user", stream_state=None)
if result['risk_level'][-1] == "Safe":
    print(f"User moderation: -> [Risk: {result['risk_level'][-1]}]")
else:
    print(f"User moderation: -> [Risk: {result['risk_level'][-1]} - Category: {result['category'][-1]}]")
 
# 2. Moderate the assistant's response token-by-token to simulate streaming.
# This loop mimics how an LLM generates a response one token at a time.
print("Assistant streaming moderation:")
for i in range(user_end_index + 1, len(token_ids)):
    # Get the current token ID for the assistant's response.
    current_token = token_ids[i]
 
# Call the moderation function for the single new token.
    # The stream_state is passed and updated in each call to maintain context.
    result, stream_state = model.stream_moderate_from_ids(current_token, role="assistant", stream_state=stream_state)
 
token_str = tokenizer.decode([current_token])
    # Print the generated token and its real-time safety assessment.
    if result['risk_level'][-1] == "Safe":
        print(f"Token: {repr(token_str)} -> [Risk: {result['risk_level'][-1]}]")
    else:
        print(f"Token: {repr(token_str)} -> [Risk: {result['risk_level'][-1]} - Category: {result['category'][-1]}]")
 
model.close_stream(stream_state)

สามารถดูตัวอย่างเพิ่มเติมได้ที่ GitHub repository ของเรา

งานในอนาคต

ความปลอดภัยของ AI คือความท้าทายที่ต้องพัฒนาอย่างต่อเนื่อง Qwen3Guard เป็นเพียงก้าวสำคัญก้าวหนึ่ง เราจะมุ่งมั่นพัฒนาระบบความปลอดภัยที่ยืดหยุ่นและทรงประสิทธิภาพยิ่งขึ้น ทั้งการเพิ่มความปลอดภัยภายใน (Intrinsic Safety) ตั้งแต่ระดับโครงสร้างและการฝึกฝน ไปจนถึงการพัฒนาเทคนิคแทรกแซงในช่วงการอนุมาน (Inference-time) เพื่อสร้างระบบ AI ที่สอดคล้องกับค่านิยมของมนุษย์และสังคมทั่วโลก

Source: Qwen Blog
ดูแลงานแปลและเรียบเรียงโดย NatapolK

ความคิดเห็น (0)

เข้าสู่ระบบเพื่อร่วมแสดงความเห็น

สมัครสมาชิก

มาเป็นคนแรกที่แสดงความเห็นกันเลยโบร