This sample demonstrates how to use Azure Content Safety to filter LLM interactions in a Semantic Kernel-powered application. It shows how to implement a PromptRenderFilter to block unsafe inputs or outputs based on:
- ✅ Built-in safety categories:
Sexual,Self-Harm,Hate,Violence - 🔧 Custom safety category support (e.g., personal-data , internal policy )
📌 Technologies Used:
- .NET 8 / C#
- Azure Content Safety API
- Microsoft Semantic Kernel
- PromptRenderFilter (for input/output filtering)
When a user prompt is sent to the LLM (like GPT-4), the app:
- Intercepts the prompt using a
PromptRenderFilter - Calls Azure Content Safety API to check the prompt
- Blocks or allows the request based on severity thresholds
- Supports out-of-the-box categories and custom categories simultaneously
To enforce organization-specific safety filters (e.g., personal-data or sensitive project names), you can create and train a Custom Category in Azure Content Safety.
Follow these steps:
-
Go to Azure AI Foundry
Navigate to Azure AI Studio or your Azure AI resource in the Azure Portal. -
Open
Guardrails + Controls
From the left-hand navigation pane, click Guardrails + Controls. -
Select the
Custom Categoriestab
Scroll to the bottom and click the Create custom category button. -
Add New Category Details
- Click Add new category
- Provide a Name (e.g.,
personal-data) - Add a Description
- Enter a public URL pointing to your
.jsonltraining file stored in Azure Blob Storage , Sample datagrant Data Reader and Contributor roles for both Azure AI Foundry and Azure Content Safety services
Click + Add → Add role assignment twice for each identity:
Purpose: Allows reading your training data (the .jsonl file)
Assign to:
- Azure AI Foundry resource (type: managed identity)
- Azure AI Content Safety resource (type: managed identity)
Purpose: Allows writing temporary artifacts or metadata during training
Assign to:
- The same two resources listed above.
✅ Result:
These two roles together allow Azure AI Foundry and Azure Content Safety to securely read and process your training files from Azure Blob Storage.
- Click
Create and Train
Azure will begin training your category. This may take a few minutes.