Notebooks
G
Google Gemini
Shieldgemma On Huggingface

Shieldgemma On Huggingface

recurrentgemmagemmapaligemmaresponsiblegemma-cookbookcodegemma
Copyright 2024 Google LLC
[ ]

Evaluating content safety with ShieldGemma and Hugging Face Transformers

When you deploy artificial intelligence (AI) models in your applications, it's important to implement safeguards to manage the behavior of the model and it's potential impact on your users.

This tutorial shows you how to employ one class of safeguards—content classifiers for filtering—using ShieldGemma and the Hugging Face Transformers framework. Setting up content classifier filters helps your AI application comply with the safety policies you define, and ensures your users have a positive experience.

For more information on building safeguards for use with generative AI models such as Gemma, see the Safeguards topic in the Responsible Generative AI Toolkit.

Supported safety checks

ShieldGemma models are trained to detect and predict violations of four harm types listed below, and taken from the Responsible Generative AI Toolkit. Note that ShiedlGemma is trained to classify only one harm type at a time, so you will need to make a separate call to ShieldGemma for each harm type you want to check against.

  • Harrassment - The application must not generate malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
  • Hate speech - The application must not generate negative or harmful content targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups).
  • Dangerous content - The application must not generate instructions or advice on harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide).
  • Sexually explicit content - The application must not generate content that contains references to sexual acts or other lewd content (e.g., sexually graphic descriptions, content aimed at causing arousal).

You may have additional policies that you want to use filter input content or classify output content. If this is the case, you can use model tuning techniques on the ShieldGemma models to recognize potential violations of your policies, and this technique should work for all ShieldGemma model sizes. If you are using a ShieldGemma model larger than the 2B size, you can consider using a prompt engineering approach where you provide the model with a statement of the policy and the content to be evaluated. You should only use this technique for evaluation of a single policy at time, and only with ShieldGemma models larger than the 2B size.

Supported use cases

ShieldGemma supports two modes of operation:

  1. Prompt-only mode for input filtering. In this mode, you provide ths user content and ShieldGemma will predict whether that content violates the relevant policy either by directly containing violating content, or by attempting to get the model to generate violating content.
  2. Prompt-response mode for output filtering. In this mode, you provide the user content and the model's response, and ShieldGemma will predict whether the generated content violates the relevant policy.

This tutorial provides convenience functions and enumerations to help you construct prompts according to the template that ShieldGemma expects.

Prediction modes

ShieldGemma works best in scoring mode where the model generates a prediction between zero (0) and one (1), where values closer to one indicate a higher probability of violation. It is recommended to use ShieldGemma in this mode so that you can have finer-grained control over the filtering behavior by adjusting a filtering threshold.

It is also possible to use this in a generating mode, similar to the LLM-as-a-Judge approach, though this mode provides less control and is more opaque than using the model in scoring mode.

Using ShieldGemma in Hugging Face Transformers

[ ]
[ ]
MODEL_VARIANT

Prompt Construction

This cell creates the UseCase and HarmType enumerations and the make_prompt(4) function to allow you to easily create prompts using the use case-specific templates that ShieldGemma requires.

Example usage is shown in the "Inference Examples" section.

[ ]

Inference Examples

Input Filtering

[ ]

Output Filtering

[ ]