Chat Moderators
Collection
Finetuned version of Phi Modesl
โข
6 items
โข
Updated
This LLM model was trained 2x faster with Unsloth and Huggingface's TRL library.
from unsloth import FastLanguageModel
from peft import PeftModel
import torch
# Load the base model
base_model_name = "unsloth/Phi-4-unsloth-bnb-4bit",
model, tokenizer = FastLanguageModel.from_pretrained(
model_name=base_model_name,
max_seq_length=2048, # Must match fine-tuning
load_in_4bit=True,
)
# Load the fine-tuned LoRA adapter
lora_model_name = "Machlovi/Phi4_Safe"
model = PeftModel.from_pretrained(model, lora_model_name)
# Run inference
input_text = "Why do we need to go to see something?"
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=4)
# Decode and print response
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
messages=[
{'content': 'why even let a tranny in they arent really even human',
'role': 'user'},
]
tokenizer = get_chat_template(
tokenizer,
chat_template = "Phi-4", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
)
inputs = tokenizer.apply_chat_template(
messages,
tokenize = True,
add_generation_prompt = True, # Must add for generation
return_tensors = "pt",
).to("cuda")
from transformers import TextStreamer
text_streamer = TextStreamer(tokenizer)
_ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 10, use_cache = True)
Hate speech, personal attacks, and discrimination
` alpaca_prompt = """<|im_start|>system<|im_sep|> system<|im_end|> <|im_start|>user<|im_sep|> {} <|im_end|> <|im_start|>assistant<|im_sep|> {}
""" `
` You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.
This resource accompanies our paper accepted in the Late Breaking Work track of HCI International 2025.
๐ Paper Title: Towards Safer AI Moderation: Evaluating LLM Moderators Through a Unified Benchmark Dataset and Advocating a Human-First Approach
๐ Conference: HCI International 2025 โ Late Breaking Work
๐ Link to Proceedings
๐ Link to Paper
๐ Link to Conference Publication
@InProceedings{10.1007/978-3-032-13184-3_24,
author="Machlovi, Naseem
and Saleki, Maryam
and Ababio, Innocent
and Amin, Ruhul",
editor="Degen, Helmut
and Ntoa, Stavroula",
title="Towards Safer AI Moderation: Evaluating LLM Moderators Through a Unified Benchmark Dataset and Advocating a Human-First Approach",
booktitle="HCI International 2025 -- Late Breaking Papers",
year="2026",
publisher="Springer Nature Switzerland",
address="Cham",
pages="386--403",
abstract="As AI systems become more integrated into daily life, the need for safer and more reliable moderation has never been greater. Large Language Models (LLMs) have demonstrated remarkable capabilities, surpassing earlier models in complexity and performance. Their evaluation across diverse tasks has consistently showcased their potential, enabling the development of adaptive and personalized agents. However, despite these advancements, LLMs remain prone to errors, particularly in areas requiring nuanced moral reasoning. They struggle with detecting implicit hate, offensive language, and gender biases due to the subjective and context-dependent nature of these issues. Moreover, their reliance on training data can inadvertently reinforce societal biases, leading to inconsistencies and ethical concerns in their outputs. To explore the limitations of LLMs in this role, we developed an experimental framework based on state-of-the-art (SOTA) models to assess human emotions and offensive behaviors. The framework introduces a unified benchmark dataset encompassing 49 distinct categories spanning the wide spectrum of human emotions, offensive and hateful text, and gender and racial biases. Furthermore, we introduced SafePhi, a QLoRA fine-tuned version of Phi-4, adapting diverse ethical contexts and outperforming benchmark moderators by achieving a Macro F1 score of 0.89, where OpenAI Moderator and Llama Guard score 0.77 and 0.74, respectively. This research also highlights the critical domains where LLM moderators consistently underperformed, pressing the need to incorporate more heterogeneous and representative data with human-in-the-loop, for better model robustness and explainability.",
isbn="978-3-032-13184-3"
}