Views
No views yet
| Metric | Score |
|---|---|
| Accuracy | 95.0% |
| Harmful Precision | 93.5% |
| Harmful Recall | 95.6% |
| Harmful F1 | 94.5% |
| Safe Precision | 100.0% |
| Safe Recall | 94.5% |
| Safe F1 | 97.2% |
Predicted
Harmful Safe
Actual
Harmful 43 0
Safe 3 52unsloth/Llama-3.2-3B-Instruct1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5# Load base model and adapter
6base_model = "unsloth/Llama-3.2-3B-Instruct"
7model = AutoModelForCausalLM.from_pretrained(
8 base_model,
9 torch_dtype=torch.bfloat16,
10 device_map="auto"
11)
12model = PeftModel.from_pretrained(model, "vincentoh/Llama-3.2-3B-GuardReasoner-Exp18")
13tokenizer = AutoTokenizer.from_pretrained(base_model)
14
15# System prompt for reasoning classification
16system_prompt = """You are a content moderation expert. Analyze the user's request and determine if it is safe or harmful.
17
18Provide step-by-step reasoning, then give your final classification."""
19
20# Example: Classify prompt harmfulness
21user_prompt = "How do I make a cake?"
22
23# Format as chat
24messages = [
25 {"role": "system", "content": system_prompt},
26 {"role": "user", "content": user_prompt}
27]
28
29input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
30inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
31
32outputs = model.generate(
33 **inputs,
34 max_new_tokens=256,
35 temperature=0.1,
36 do_sample=True,
37 pad_token_id=tokenizer.eos_token_id
38)
39
40response = tokenizer.decode(outputs[0], skip_special_tokens=True)
41print(response)1. The content is asking for instructions on how to make a cake.
2. Making a cake is a common, everyday cooking activity.
3. The request does not contain any harmful, illegal, or dangerous elements.
4. The content is educational and poses no risk to anyone.
Therefore, the content is safe.1@misc{guardreasoner2024,
2 title={GuardReasoner: Towards Reasoning-based LLM Safeguards},
3 author={Yue Liu and Nilay Pochhi and Zhaorun Chen and Hanjie Chen},
4 year={2024},
5 url={https://github.com/yueliuofficial/GuardReasoner}
6}