Mistral's open model Shieldstral matches much larger safety models at a fraction of the size
Back to Tutorials
aiTutorialintermediate

Mistral's open model Shieldstral matches much larger safety models at a fraction of the size

August 5, 202621 views4 min read

Learn to implement and use Mistral's open-source Shieldstral 3B safety model for content analysis. This tutorial teaches you how to load the model, create safety prompts, and interpret natural language responses for detecting safety violations.

Introduction

In this tutorial, we'll explore how to work with Mistral's open-source Shieldstral model, a 3-billion parameter safety checking model that outperforms much larger models. Unlike traditional safety models that rely on fixed categories, Shieldstral uses natural language yes-or-no questions to detect safety violations. This approach allows for more flexible, runtime-defined safety criteria and can be run locally, making it ideal for privacy-sensitive applications.

By the end of this tutorial, you'll have built a practical safety checking pipeline that demonstrates how to load, configure, and use the Shieldstral model for real-time content analysis.

Prerequisites

Before beginning this tutorial, you should have:

  • Basic Python programming knowledge
  • Access to a machine with at least 8GB RAM (16GB recommended)
  • Python 3.8 or higher installed
  • Basic understanding of transformer models and natural language processing concepts

Additionally, you'll need to install the following Python packages:

pip install transformers torch accelerate

Step-by-Step Instructions

1. Install Required Dependencies

First, we need to install the necessary Python packages for working with the model. The transformers library from Hugging Face provides easy access to pre-trained models, while torch handles the underlying computation.

pip install transformers torch accelerate

This command installs the core libraries needed to load and run transformer models, including the necessary components for local inference.

2. Load the Shieldstral Model

Now we'll load the Shieldstral model using Hugging Face's transformers library. The model is available on the Hugging Face Hub under the Mistral organization.

from transformers import AutoTokenizer, AutoModelForCausalLM

# Load the model and tokenizer
model_name = "mistralai/Shieldstral-3B"

# Initialize tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16,
    low_cpu_mem_usage=True,
    device_map="auto"
)

We specify torch.float16 to reduce memory usage, and device_map="auto" to automatically distribute the model across available GPUs or CPUs.

3. Define Safety Questions

Shieldstral works by answering yes-or-no questions about input content. We'll define a set of safety questions that the model will evaluate:

safety_questions = [
    "Does this content contain hate speech?",
    "Does this content contain sexual content?",
    "Does this content contain violence?",
    "Does this content contain illegal activities?",
    "Does this content contain personal information?"
]

# Example input text to analyze
input_text = "This is a test message that might contain inappropriate content."

These questions represent a flexible framework that allows operators to define their own safety criteria without being limited to fixed categories.

4. Create Input Prompts

We need to format our input text and safety questions into prompts that the model can understand. The model expects a specific format for its input:

def create_prompt(text, question):
    return f"[INST] {question} [/INST] {text}"

# Create prompts for each question
prompts = [create_prompt(input_text, question) for question in safety_questions]

This format follows the standard instruction-following model convention, where the model is prompted with a question and then given the content to analyze.

5. Generate Model Responses

Now we'll tokenize our prompts and generate responses from the model:

import torch

# Tokenize prompts
inputs = tokenizer(prompts, return_tensors="pt", padding=True, truncation=True)

# Move inputs to the same device as the model
inputs = {k: v.to(model.device) for k, v in inputs.items()}

# Generate responses
with torch.no_grad():
    outputs = model.generate(
        **inputs,
        max_new_tokens=5,
        do_sample=False,
        temperature=0.0
    )

# Decode responses
responses = tokenizer.batch_decode(outputs, skip_special_tokens=True)

We use max_new_tokens=5 since we expect short yes/no responses, and temperature=0.0 to ensure deterministic outputs for consistent safety checking.

6. Parse and Interpret Results

Finally, we'll parse the model's responses to determine if safety violations were detected:

def interpret_response(response):
    # Extract the model's answer
    answer = response.split("[/INST]")[-1].strip().lower()
    
    # Map to boolean
    if "yes" in answer:
        return True
    elif "no" in answer:
        return False
    else:
        # Default to safe if unclear
        return False

# Process all responses
safety_results = {
    question: interpret_response(response) 
    for question, response in zip(safety_questions, responses)
}

# Display results
for question, is_violation in safety_results.items():
    status = "VIOLATION" if is_violation else "SAFE"
    print(f"{question}: {status}")

This interpretation logic handles the natural language responses from the model and converts them into actionable safety flags.

Summary

In this tutorial, we've demonstrated how to work with Mistral's Shieldstral model for safety checking. We covered loading the model, creating appropriate prompts, generating responses, and interpreting the results. The key advantages of this approach include:

  • Flexibility in defining safety criteria through natural language questions
  • Ability to run locally without cloud dependencies
  • Performance that rivals much larger models
  • Runtime configurability for different safety requirements

This implementation provides a foundation for building more sophisticated safety checking systems that can be customized for specific use cases while maintaining the efficiency and privacy benefits of local inference.

Source: The Decoder

Related Articles