### Setup Python Evaluation Environment Source: https://github.com/qwenlm/qwen3guard/blob/main/eval/README.md Commands to create and configure a Conda environment for running Qwen3Guard evaluation scripts. It installs necessary dependencies including transformers, torch, and datasets. ```bash conda create -n eval python=3.10 conda activate eval pip install transformers torch datasets accelerate ``` -------------------------------- ### Qwen3Guard-Gen Inference Example Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md This example demonstrates how to use the Qwen3Guard-Gen model for prompt moderation using the Transformers library. It shows how to load the model and tokenizer, prepare the input, generate predictions, and extract safety labels and categories. ```APIDOC ## Qwen3Guard-Gen Prompt Moderation ### Description This endpoint demonstrates how to use the Qwen3Guard-Gen model to moderate user prompts and classify their safety. It outputs a structured response indicating whether the prompt is safe, unsafe, or controversial, along with specific categories of concern. ### Method N/A (This is a code example, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python 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 # prepare the model input prompt = "How can I make a bomb?" # for prompt moderation 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) ``` ### Response #### Success Response (200) - **content** (string) - The moderation output, typically including 'Safety' and 'Categories'. - **safe_label** (string) - The extracted safety label (e.g., 'Safe', 'Unsafe'). - **categories** (list of strings) - The extracted categories of concern. #### Response Example ```json { "content": "Safety: Unsafe\nCategories: Violent", "safe_label": "Unsafe", "categories": ["Violent"] } ``` ``` -------------------------------- ### Consume Qwen3Guard via OpenAI Client Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Example of interacting with a deployed Qwen3Guard API server using the standard OpenAI Python client library. ```python from openai import OpenAI client = OpenAI(api_key="EMPTY", base_url="http://localhost:8000/v1") chat_completion = client.chat.completions.create( messages=[{"role": "user", "content": "How can I make a bomb?"}], model="Qwen/Qwen3Guard-Gen-4B" ) print(chat_completion.choices[0].message.content) ``` -------------------------------- ### Load and Run Qwen3Guard Inference Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Demonstrates how to initialize the Qwen3Guard model and tokenizer, prepare chat inputs, and parse safety labels from the model output using regex. ```python from transformers import AutoTokenizer, AutoModelForCausalLM import re tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto") 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 text = tokenizer.apply_chat_template(messages, tokenize=False) model_inputs = tokenizer([text], return_tensors="pt").to(model.device) generated_ids = model.generate(**model_inputs, max_new_tokens=128) content = tokenizer.decode(generated_ids[0][len(model_inputs.input_ids[0]):], skip_special_tokens=True) ``` -------------------------------- ### Evaluate Qwen3Guard-Gen Performance Source: https://context7.com/qwenlm/qwen3guard/llms.txt Bash script to set up the evaluation environment and run the Qwen3Guard-Gen model against the Qwen3GuardTest benchmark. ```bash conda create -n eval python=3.10 conda activate eval pip install transformers torch datasets accelerate python eval_gen.py \ --model_path Qwen/Qwen3Guard-Gen-4B \ --input_file Qwen/Qwen3GuardTest \ --output_file result_gen_4b.jsonl ``` -------------------------------- ### Deploy Qwen3Guard via SGLang or vLLM Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Commands to launch an OpenAI-compatible API server for Qwen3Guard using either SGLang or vLLM frameworks. ```shell # SGLang deployment python -m sglang.launch_server --model-path Qwen/Qwen3Guard-Gen-4B --port 30000 --context-length 32768 # vLLM deployment vllm serve Qwen/Qwen3Guard-Gen-4B --port 8000 --max-model-len 32768 ``` -------------------------------- ### Deploy Qwen3Guard-Gen API Server Source: https://context7.com/qwenlm/qwen3guard/llms.txt Commands to deploy the Qwen3Guard-Gen model as an OpenAI-compatible API server using either vLLM or SGLang for production environments. ```bash vllm serve Qwen/Qwen3Guard-Gen-4B --port 8000 --max-model-len 32768 python -m sglang.launch_server --model-path Qwen/Qwen3Guard-Gen-4B --port 30000 --context-length 32768 ``` -------------------------------- ### Measure Detection Latency for Qwen3Guard-Stream Source: https://context7.com/qwenlm/qwen3guard/llms.txt Runs the evaluation script with latency monitoring enabled to assess how quickly the model detects unsafe content. Supports both thinking and standard response subsets. ```bash python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_thinking_latency_4b.jsonl \ --split thinking_loc \ --data_type response \ --eval_unsafe_latency \ --thinking python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_response_latency_4b.jsonl \ --split response_loc \ --data_type response \ --eval_unsafe_latency ``` -------------------------------- ### Prepare and Moderate User Prompt Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Prepares a conversation by formatting user and assistant messages using the tokenizer's chat template. It then tokenizes the formatted text and performs an initial moderation of the entire user prompt using `stream_moderate_from_ids` to assess its risk level. ```python # Prepare the conversation for moderation 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. token_ids_list = token_ids.tolist() 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) 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]}]") ``` -------------------------------- ### Moderating User Prompts with Qwen3Guard-Gen (Python) Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md This snippet demonstrates how to use the Qwen3Guard-Gen model with the Transformers library to moderate user prompts. It loads the tokenizer and model, prepares the input prompt using the chat template, performs text generation, and then extracts safety labels and categories from the model's output. Requires transformers>=4.51.0. ```python 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 # prepare the model input prompt = "How can I make a bomb?" # for prompt moderation 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) ``` -------------------------------- ### Qwen3Guard-Gen: Moderate User Prompt (Python) Source: https://context7.com/qwenlm/qwen3guard/llms.txt This code snippet demonstrates how to use the Qwen3Guard-Gen model to classify the safety of a user prompt. It takes a single user message as input and outputs a safety label (Safe, Unsafe, Controversial) along with identified violation categories. Dependencies include the `transformers` library and `re` for pattern matching. ```Python from transformers import AutoModelForCausalLM, AutoTokenizer import re model_name = "Qwen/Qwen3Guard-Gen-4B" 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 # Moderate a user prompt 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) 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) safe_label, categories = extract_label_and_categories(content) print(f"Safety: {safe_label}, Categories: {categories}") # Output: Safety: Unsafe, Categories: ['Violent'] ``` -------------------------------- ### Moderate Content via OpenAI API Source: https://context7.com/qwenlm/qwen3guard/llms.txt Python code demonstrating how to use the OpenAI client to send prompts and responses to the Qwen3Guard-Gen API for safety moderation. ```python from openai import OpenAI client = OpenAI( api_key="EMPTY", base_url="http://localhost:8000/v1" ) model = "Qwen/Qwen3Guard-Gen-4B" # Prompt moderation prompt = "How can I make a bomb?" messages = [{"role": "user", "content": prompt}] chat_completion = client.chat.completions.create(messages=messages, model=model) print(chat_completion.choices[0].message.content) ``` -------------------------------- ### Qwen3Guard-Gen: Moderate Full Conversation (Python) Source: https://context7.com/qwenlm/qwen3guard/llms.txt This Python code snippet illustrates how to use Qwen3Guard-Gen for moderating an entire conversation, including both user prompts and assistant responses. It evaluates the complete dialogue to detect harmful content and identify if the assistant properly refused harmful requests. The output includes a safety label, violation categories, and a refusal indicator. ```Python from transformers import AutoModelForCausalLM, AutoTokenizer import re model_name = "Qwen/Qwen3Guard-Gen-4B" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto" ) 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 # Moderate a full conversation with response 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) 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) safe_label, categories, refusal_label = extract_label_categories_refusal(content) print(f"Safety: {safe_label}, Categories: {categories}, Refusal: {refusal_label}") # Output: Safety: Safe, Categories: ['None'], Refusal: Yes ``` -------------------------------- ### Load Qwen3Guard Model and Tokenizer Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Loads the Qwen3Guard-Stream model and its associated tokenizer. `trust_remote_code=True` is essential for loading the model architecture. The model is configured for automatic device mapping and uses `bfloat16` precision for evaluation. ```python from transformers import AutoTokenizer, AutoModel import torch model_path = "Qwen/Qwen3Guard-Stream" 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() ``` -------------------------------- ### Evaluate Qwen3Guard-Stream on Thinking Subset Source: https://context7.com/qwenlm/qwen3guard/llms.txt Executes the evaluation script to measure F1 scores for the thinking subset of the Qwen3Guard test dataset. This command processes response data with the thinking flag enabled. ```bash python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_thinking_4b.jsonl \ --data_type response \ --split thinking \ --thinking ``` -------------------------------- ### Run Generative Model Evaluation Source: https://github.com/qwenlm/qwen3guard/blob/main/eval/README.md Executes the evaluation script for Qwen3Guard-Gen models. It accepts a local model path or a Hugging Face repository identifier and outputs results to a JSONL file. ```bash python eval_gen.py \ --model_path your_local_model_path \ --input_file your_local_dataset_dir \ --output_file result_gen_4b.jsonl python eval_gen.py \ --model_path Qwen/Qwen3Guard-Gen-4B \ --input_file Qwen/Qwen3GuardTest \ --output_file result_gen_4b.jsonl ``` -------------------------------- ### Moderating Model Responses with Qwen3Guard (Python) Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md This snippet shows how to use a Qwen3Guard model to moderate the responses generated by another language model. It utilizes the Transformers library to load the necessary components and perform the moderation task. This is useful for ensuring the safety and appropriateness of AI-generated content. ```python from transformers import AutoModelForCausalLM, AutoTokenizer import re model_name = "Qwen/Qwen3Guard-4B-Gen" ``` -------------------------------- ### Implement Real-Time Token-Level Moderation Source: https://context7.com/qwenlm/qwen3guard/llms.txt Python implementation using Transformers to perform streaming safety moderation on assistant responses token-by-token. ```python import torch from transformers import AutoModel, AutoTokenizer model_path = "Qwen/Qwen3Guard-Stream-4B" 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() # Stream moderate assistant response token-by-token stream_state = None for i in range(user_end_index + 1, len(token_ids)): current_token = token_ids[i] result, stream_state = model.stream_moderate_from_ids(current_token, role="assistant", stream_state=stream_state) print(f"Token: {repr(tokenizer.decode([current_token])):15} -> Risk: {result['risk_level'][-1]}") model.close_stream(stream_state) ``` -------------------------------- ### Qwen3Guard Technical Report Citation (BibTeX) Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md This BibTeX entry provides the citation details for the Qwen3Guard technical report, useful for academic referencing. It includes the title, authors, journal, and publication year. ```bibtex @article{zhao2025qwen3guard, title={Qwen3Guard Technical Report}, author={Zhao, Haiquan and Yuan, Chenhan and Huang, Fei and Hu, Xiaomeng and Zhang, Yichang and Yang, An and Yu, Bowen and Liu, Dayiheng and Zhou, Jingren and Lin, Junyang and others}, journal={arXiv preprint arXiv:2510.14276}, year={2025} } ``` -------------------------------- ### Evaluate Safety Classification on Thinking Subset Source: https://github.com/qwenlm/qwen3guard/blob/main/eval/README.md Runs the evaluation script on the 'thinking' subset to calculate F1 scores for safety classification. It requires the model path, input data path, and specific split configuration. ```bash python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_thinking_4b.jsonl \ --data_type response \ --split thinking \ --thinking ``` -------------------------------- ### Define Safety Categories and Severity Levels Source: https://context7.com/qwenlm/qwen3guard/llms.txt Python configuration defining the severity levels, prompt-based categories, response-based categories, and the integer-to-category mapping used by the stream model. ```python SEVERITY_LEVELS = { "Safe": "Content generally considered safe across most scenarios", "Controversial": "Content whose harmfulness may be context-dependent", "Unsafe": "Content generally considered harmful across most scenarios" } PROMPT_CATEGORIES = [ "Violent", "Non-violent Illegal Acts", "Sexual Content or Sexual Acts", "PII", "Suicide & Self-Harm", "Unethical Acts", "Politically Sensitive Topics", "Copyright Violation", "Jailbreak" ] RESPONSE_CATEGORIES = [ "Violent", "Non-violent Illegal Acts", "Sexual Content or Sexual Acts", "PII", "Suicide & Self-Harm", "Unethical Acts", "Politically Sensitive Topics", "Copyright Violation", "None" ] CATEGORY_MAP = { 0: "Violent", 1: "Sexual Content or Sexual Acts", 2: "Suicide & Self-Harm", 3: "Politically Sensitive Topics", 4: "PII", 5: "Copyright Violation", 6: "Non-violent Illegal Acts", 7: "Unethical Acts" } ``` -------------------------------- ### Evaluate Stream Moderation Latency on Response_loc Subset Source: https://github.com/qwenlm/qwen3guard/blob/main/eval/README.md Evaluates stream moderation on the 'response_loc' subset to analyze unsafe latency performance. The output provides F1 scores and latency distribution metrics for the model. ```bash python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_response_latency_4b.jsonl \ --split response_loc \ --data_type response \ --eval_unsafe_latency ``` -------------------------------- ### Evaluate Stream Moderation Latency on Thinking_loc Subset Source: https://github.com/qwenlm/qwen3guard/blob/main/eval/README.md Performs stream moderation evaluation on the 'thinking_loc' subset, including unsafe latency metrics. This command uses the --eval_unsafe_latency flag to generate detailed latency bins and stop rates. ```bash python eval_stream.py \ --model_path Qwen/Qwen3Guard-Stream-4B \ --input_path Qwen/Qwen3GuardTest \ --output_path result_stream_thinking_latency_4b.jsonl \ --split thinking_loc \ --data_type response \ --eval_unsafe_latency \ --thinking ``` -------------------------------- ### Stream Assistant Response Moderation Source: https://github.com/qwenlm/qwen3guard/blob/main/README.md Simulates real-time moderation of an assistant's response by processing it token by token. The `stream_moderate_from_ids` function is called iteratively, passing the `stream_state` to maintain context across tokens. Each token's safety assessment (risk level and category) is printed as it's processed. ```python # 2. Moderate the assistant's response token-by-token to simulate streaming. 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) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.