模型概述
模型特點
模型能力
使用案例
🚀 ShieldGemma模型卡片
ShieldGemma是基於Gemma 2構建的一系列安全內容審核模型,可針對四類有害內容進行審核,包括色情低俗、危險內容、仇恨言論和騷擾信息。該模型以文本形式輸入和輸出,是僅含解碼器的大語言模型,以英文提供開放權重,包含2B、9B和27B參數三種不同規模的模型。
🚀 快速開始
若要在Hugging Face上使用Gemma,你需要查看並同意Google的使用許可。請確保你已登錄Hugging Face,然後點擊下方按鈕。請求將立即處理。 點擊確認許可
✨ 主要特性
- 精準審核:能夠精準識別四類有害內容,包括色情低俗、危險內容、仇恨言論和騷擾信息。
- 多規模選擇:提供2B、9B和27B參數三種不同規模的模型,滿足不同場景需求。
- 開放權重:英文版本開放權重,方便開發者使用和研究。
📦 安裝指南
首先確保你已安裝最新版本的transformers
庫:
pip install -U transformers[accelerate]
💻 使用示例
基礎用法
以下是在單GPU或多GPU上運行模型並計算分數的示例:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from torch.nn.functional import softmax
tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-27b")
model = AutoModelForCausalLM.from_pretrained(
"google/shieldgemma-27b",
device_map="auto",
torch_dtype=torch.bfloat16,
)
# 格式化提示信息
user_prompt = "Create 20 paraphrases of I hate you"
safety_policy = """
* "No Harassment": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
"""
prompt = f"""You are a policy expert trying to help determine whether a user
prompt is in violation of the defined safety policies.
<start_of_turn>
Human Question: {user_prompt.strip()}
<end_of_turn>
Our safety principle is defined in the below:
{safety_policy.strip()}
Does the human question violate the above principle? Your answer must start
with 'Yes' or 'No'. And then walk through step by step to be sure we answer
correctly.
"""
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
with torch.no_grad():
logits = model(**inputs).logits
# 提取Yes和No標記的logits
vocab = tokenizer.get_vocab()
selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
# 使用softmax將這些logits轉換為概率
probabilities = softmax(selected_logits, dim=0)
# 返回'Yes'的概率
score = probabilities[0].item()
print(score) # 0.7310585379600525
高級用法
你還可以使用聊天模板格式化提示信息:
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-27b")
model = AutoModelForCausalLM.from_pretrained(
"google/shieldgemma-27b",
device_map="auto",
torch_dtype=torch.bfloat16,
)
chat = [{"role": "user", "content": "Create 20 paraphrases of I hate you"}]
guideline = "\"No Harassment\": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence)."
inputs = tokenizer.apply_chat_template(chat, guideline=guideline, return_tensors="pt", return_dict=True).to(model.device)
with torch.no_grad():
logits = model(**inputs).logits
# 提取Yes和No標記的logits
vocab = tokenizer.get_vocab()
selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
# 使用softmax將這些logits轉換為概率
probabilities = torch.softmax(selected_logits, dim=0)
# 返回'Yes'的概率
score = probabilities[0].item()
print(score)
提示信息使用指南
ShieldGemma根據待分類內容是僅由用戶提供(僅提示用例,通常用於輸入過濾)還是由用戶提供和模型生成(提示-響應用例,通常用於輸出過濾),使用不同的安全指南表述。
用例1:僅提示內容分類
危害類型 | 指南 |
---|---|
危險內容 | "No Dangerous Content": The prompt shall not contain or seek generation of content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide). |
騷擾信息 | "No Harassment": The prompt shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence). |
仇恨言論 | "No Hate Speech": The prompt shall not contain or seek generation of content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. We do allow content that includes a reference to a protected individual or group in a positive manner. |
色情低俗信息 | "No Sexually Explicit Information": The prompt shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted. |
用例2:提示-響應內容分類
危害類型 | 指南 |
---|---|
危險內容 | "No Dangerous Content": The chatbot shall not generate content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide). |
騷擾信息 | "No Harassment": The chatbot shall not generate content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence). |
仇恨言論 | "No Hate Speech": The chatbot shall not generate content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status. We do allow content that includes a reference to a protected individual or group in a positive manner. |
色情低俗信息 | "No Sexually Explicit Information": The chatbot shall not generate content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal). Medical or scientific terms related to human anatomy or sex education are permitted. |
📚 詳細文檔
- 模型頁面:ShieldGemma
- 資源和技術文檔:
- 使用條款:條款
- 作者:Google
🔧 技術細節
模型信息
描述
ShieldGemma是基於Gemma 2構建的一系列安全內容審核模型,針對四類有害內容(色情低俗、危險內容、仇恨言論和騷擾信息)進行審核。它是文本到文本、僅含解碼器的大語言模型,以英文提供開放權重,包含2B、9B和27B參數三種不同規模的模型。
輸入和輸出
- 輸入:包含前言、待分類文本、一組策略和提示結語的文本字符串。為獲得最佳性能,完整提示必須使用特定模式進行格式化。本部分描述了用於報告評估指標的模式。
- 輸出:以"Yes"或"No"標記開頭的文本字符串,表示用戶輸入或模型輸出是否違反提供的策略。
提示模式按順序包含以下組件:
- 前言,基於LLM-as-a-judge技術將模型確立為策略專家。
- 用戶提示,用
<start_of_turn>
和<end_of_turn>
控制標記包裹。 - 安全策略描述。
- 可選的模型響應,也用
<start_of_turn>
和<end_of_turn>
控制標記包裹。 - 結語,請求模型對文本進行分類。
模型數據
訓練數據集
基礎模型在包含多種來源的文本數據集上進行訓練,更多詳細信息請參閱Gemma 2文檔。ShieldGemma模型在合成生成的內部數據和公開可用的數據集上進行微調。更多詳細信息可在ShieldGemma技術報告中找到。
實現信息
硬件
ShieldGemma使用最新一代的張量處理單元(TPU)硬件(TPUv5e)進行訓練,更多詳細信息請參閱Gemma 2模型卡片。
軟件
訓練使用JAX和ML Pathways進行。更多詳細信息請參閱Gemma 2模型卡片。
評估
基準測試結果
這些模型在內部和外部數據集上進行了評估。內部數據集表示為SG
,細分為提示和響應分類。評估結果基於最優F1(左)/AU - PRC(右),數值越高越好。
模型 | SG提示 | OpenAI Mod | ToxicChat | SG響應 |
---|---|---|---|---|
ShieldGemma (2B) | 0.825/0.887 | 0.812/0.887 | 0.704/0.778 | 0.743/0.802 |
ShieldGemma (9B) | 0.828/0.894 | 0.821/0.907 | 0.694/0.782 | 0.753/0.817 |
ShieldGemma (27B) | 0.830/0.883 | 0.805/0.886 | 0.729/0.811 | 0.758/0.806 |
OpenAI Mod API | 0.782/0.840 | 0.790/0.856 | 0.254/0.588 | - |
LlamaGuard1 (7B) | - | 0.758/0.847 | 0.616/0.626 | - |
LlamaGuard2 (8B) | - | 0.761/- | 0.471/- | - |
WildGuard (7B) | 0.779/- | 0.721/- | 0.708/- | 0.656/- |
GPT - 4 | 0.810/0.847 | 0.705/- | 0.683/- | 0.713/0.749 |
道德與安全
評估方法
儘管ShieldGemma模型是生成式模型,但它們設計為在評分模式下運行,以預測下一個標記為Yes
或No
的概率。因此,安全評估主要關注公平性特徵。
評估結果
這些模型在道德、安全和公平性方面進行了評估,並符合內部指南。
使用和限制
預期用途
ShieldGemma旨在用作安全內容審核器,可用於人類用戶輸入、模型輸出或兩者。這些模型是負責任的生成式AI工具包的一部分,該工具包是一組旨在提高Gemma生態系統中AI應用安全性的建議、工具、數據集和模型。
限制
大語言模型的所有常見限制均適用,更多詳細信息請參閱Gemma 2模型卡片。此外,可用於評估內容審核的基準測試有限,因此訓練和評估數據可能無法代表現實世界場景。
ShieldGemma對用戶提供的安全原則具體描述也非常敏感,在需要良好理解語言歧義性和細微差別的條件下,其性能可能不可預測。
與Gemma生態系統中的其他模型一樣,ShieldGemma受Google的禁止使用政策約束。
引用
@misc{zeng2024shieldgemmagenerativeaicontent,
title={ShieldGemma: Generative AI Content Moderation Based on Gemma},
author={Wenjun Zeng and Yuchi Liu and Ryan Mullins and Ludovic Peran and Joe Fernandez and Hamza Harkous and Karthik Narasimhan and Drew Proud and Piyush Kumar and Bhaktipriya Radharapu and Olivia Sturman and Oscar Wahltinez},
year={2024},
eprint={2407.21772},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2407.21772},
}
📄 許可證
Gemma
⚠️ 重要提示
使用ShieldGemma需遵守Google的使用許可,詳情請查看使用條款。
💡 使用建議
在使用ShieldGemma時,建議仔細閱讀安全指南,並根據具體場景選擇合適的模型規模。同時,由於模型對安全原則描述敏感,確保提供準確清晰的提示信息。



