Azure-AI_Foundry

Lesson 1: Azure AI Content Safety

Introduction

Azure AI Content Safety is a dedicated service that detects harmful content across text and images. Every Azure OpenAI deployment includes built-in content filters, but the standalone Content Safety service provides deeper customization, custom categories, and blocklists for your specific domain.

πŸ“– Reference: Azure AI Content Safety


Text Moderation Categories

Content Safety analyzes text across four harm categories:

Category Description Examples
Hate Content targeting identity groups Slurs, discrimination, stereotypes
Violence Content depicting physical harm Threats, graphic descriptions
Sexual Sexually explicit content Explicit descriptions, solicitation
Self-Harm Content related to self-injury Instructions, glorification

Severity Levels (0–7 Scale)

Each category returns a severity score:

Level Range Meaning Typical Action
Safe 0–1 No harmful content Allow
Low 2–3 Mild or contextual Allow with logging
Medium 4–5 Moderate harmful content Block or review
High 6–7 Severe harmful content Block immediately

Analyzing Text Content

from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.core.credentials import AzureKeyCredential

client = ContentSafetyClient(
    endpoint="https://<your-resource>.cognitiveservices.azure.com",
    credential=AzureKeyCredential("<your-key>")
)

# Analyze text for harmful content
request = AnalyzeTextOptions(
    text="This is a sample text to analyze for safety.",
    categories=[
        TextCategory.HATE,
        TextCategory.VIOLENCE,
        TextCategory.SEXUAL,
        TextCategory.SELF_HARM
    ]
)

response = client.analyze_text(request)

for result in response.categories_analysis:
    print(f"Category: {result.category}, Severity: {result.severity}")
    if result.severity >= 4:
        print(f"  ⚠️ BLOCKED β€” severity {result.severity} exceeds threshold")

Image Moderation

Content Safety also analyzes images using the same four categories and severity scale.

from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData

# Analyze an image from a URL
request = AnalyzeImageOptions(
    image=ImageData(blob_url="https://example.com/image.jpg")
)
response = client.analyze_image(request)

for result in response.categories_analysis:
    print(f"Category: {result.category}, Severity: {result.severity}")

πŸ“– Reference: Image moderation quickstart


Custom Categories and Blocklists

For domain-specific moderation, you can create custom blocklists:

from azure.ai.contentsafety.models import (
    TextBlocklist,
    AddOrUpdateTextBlocklistItemsOptions,
    TextBlocklistItem
)

# Create a blocklist
blocklist_name = "financial-terms-blocklist"
client.create_or_update_text_blocklist(
    blocklist_name=blocklist_name,
    options=TextBlocklist(description="Blocked financial advice terms")
)

# Add terms to the blocklist
items = AddOrUpdateTextBlocklistItemsOptions(
    blocklist_items=[
        TextBlocklistItem(text="guaranteed returns"),
        TextBlocklistItem(text="risk-free investment"),
        TextBlocklistItem(text="insider tip"),
    ]
)
client.add_or_update_blocklist_items(
    blocklist_name=blocklist_name,
    options=items
)

Using Blocklists in Analysis

request = AnalyzeTextOptions(
    text="I have an insider tip for a risk-free investment.",
    blocklist_names=["financial-terms-blocklist"],
    halt_on_blocklist_hit=True
)
response = client.analyze_text(request)

if response.blocklists_match:
    for match in response.blocklists_match:
        print(f"Blocked term: '{match.blocklist_item_text}' "
              f"from list: {match.blocklist_name}")

Configuring Content Filters on Azure OpenAI Deployments

Azure OpenAI deployments have built-in content filters that you can customize in AI Foundry:

  1. Navigate to your AI Foundry project β†’ Deployments
  2. Select your model deployment β†’ Content filters
  3. Create a custom content filter configuration
  4. Set severity thresholds per category for both input and output
from openai import AzureOpenAI

client = AzureOpenAI(
    azure_endpoint="https://<your-resource>.openai.azure.com",
    api_key="<your-key>",
    api_version="2024-06-01"
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Tell me about renewable energy."}
        ]
    )
    print(response.choices[0].message.content)
except Exception as e:
    # Content filter violations raise exceptions
    if "content_filter" in str(e):
        print("Content was filtered by Azure AI safety systems.")
    else:
        raise

πŸ“– Reference: Configure content filters


πŸ’‘ Best Practices

  1. Always start with Microsoft’s default filters β€” they are well-tuned and cover most scenarios
  2. Customize only when your domain requires it β€” e.g., medical apps may need relaxed self-harm filters
  3. Log all content filter triggers β€” monitor patterns to improve your system prompts
  4. Test with adversarial inputs β€” red-team your application before production deployment
  5. Use blocklists for brand-specific terms β€” prevent your AI from mentioning competitors or restricted topics
  6. Set different thresholds for input vs output β€” input filtering catches user abuse, output filtering catches model mistakes

Hands-On Exercise

  1. Create a Content Safety resource in your Azure subscription
  2. Analyze 5 different text samples and observe severity scores
  3. Create a custom blocklist with 10 domain-specific terms
  4. Configure content filters on an Azure OpenAI deployment
  5. Test with edge-case inputs and document the results

Key Takeaways


Additional Resources


Next: Lesson 2 β€” Prompt Shields and Safety