In this challenge, you will learn how to enhance the safety and reliability of your AI-powered applications by leveraging content filters and protection mechanisms available in Azure AI Foundry. Azure AI provides powerful tools to help developers create ethical AI solutions by filtering out harmful or inappropriate content from model outputs. You will explore how to work with these filters in the Azure AI Foundry Studio, adjust settings, and even create custom filters to block specific words or phrases.
Content filters are essential in ensuring that your AI models meet your organization's ethical guidelines and regulatory requirements. By the end of this challenge, you will be able to configure filters that align with your application's needs and ensure safer interactions.
- Access to Azure AI Foundry Studio (See step 1 below).
- An Azure AI Foundry resource created in an earlier challenge.
NOTE: Make sure you have access to the Azure AI Foundry Studio and an existing Azure AI Foundry resource, as these are required to complete this challenge.
This challenge will guide you through configuring and testing content filters using Azure AI Foundry Studio.
-
Azure Portal
-
Direct link: https://ai.azure.com/
-
Navigate to the "Safety + security" tab at the lower left.
-
Click "Create content filter", then enter a name for your new filter.
-
Experiment with the threshold sliders for filtering out offensive language or specific categories of content.
- The first screen of sliders controls the "input filter", meaning the content a user enters into the prompt.
- The subsequent screen of sliders controls the "output filter", which is applied to content returned from the LLM.
-
On the final page, you will be presented with a list of existing model deployments that your content policy can be applied to. Select the row that matches the model in your program's app configuration, then click next. If prompted, click "Replace" to replace the existing default content filtering policy.
-
Test the changes by running prompts through the model and observing the impact of the filters.
💡 Blocklists can be used to create a custom filter to block a specific word or phrase that you believe should be filtered in your application.
-
Return to the Safety + security tab, then on the resulting screen, click the Blocklists (Preview) tab.
-
Click Create blocklist, then enter a name & description (optional).
-
After you are returned to the list, click the newly created blocklist to access the term list.
- Proceed to add a term you would like to be excluded. You could use a random test word like "unicorn", or something more realistic—like "social security number."
- It also supports using regex to generically filter input that matches a predefined pattern.
- Consider how these filters could be integrated into your broader AI application or workflow.
- Think about scenarios where custom filters might be essential for maintaining ethical standards. Keep these in mind when developing with AI solutions in the future.
💡 The Azure AI Service will throw an exception when your user's request is rejected due to content safety filters. You can catch and handle that exception and handle it to let the user know why they didn't get a response. 💡
// In your AIService.java file, you can handle content filter exceptions like this:
try {
List<ChatMessageContent<?>> responses = chatCompletionService.getChatMessageContentsAsync(
chatHistory,
kernel,
invocationContext
).block();
return responses;
} catch (Exception e) {
// Check if the exception is related to content filtering
if (e.getMessage().contains("content_filter") ||
e.getMessage().contains("ResponsibleAIPolicyViolation")) {
// Return a user-friendly message about content filtering
System.out.println("Content filtered: " + e.getMessage());
throw new RuntimeException("Your request was blocked by our content safety filters. Please rephrase your request.", e);
} else {
// Handle other exceptions normally
System.out.println("AI response error: " + e.getMessage());
throw new RuntimeException("Error processing AI response", e);
}
}- You have successfully navigated the Azure AI Foundry Studio and adjusted the default content filter settings.
- You experimented and observed the results of different filter settings.
- You created and tested a custom blocklist to filter specific terms, phrases, or patterns.
- How to configure content filters with Azure AI Foundry Service - Detailed steps for configuring filtering in AOAI
- Use a blocklist in Azure AI Foundry - Help with explicitly filtering additional predefined terms, are specific to one's use case
- Data, privacy, and security for Azure AI Foundry Service#Preventing abuse & harmful content generation - General outline of content safety & abuse monitoring functionality
- Azure AI Content Safety Java SDK - Java SDK documentation for implementing content safety in your applications
While Java Semantic Kernel doesn't have the same built-in filtering capabilities as the .NET version, you can implement custom content filtering in your Java application by creating filters that intercept requests and responses.
Here's an example of how you could implement a simple content filter in your Java application:
-
Create a ContentFilter interface:
public interface ContentFilter { boolean isContentAllowed(String content); String getFilteredMessage(); }
-
Implement a simple word-based filter:
@Component public class SimpleContentFilter implements ContentFilter { private final List<String> blockedWords = Arrays.asList( "social security number", "sensitive-term", "blocked-word" ); @Override public boolean isContentAllowed(String content) { String lowerContent = content.toLowerCase(); return blockedWords.stream() .noneMatch(lowerContent::contains); } @Override public String getFilteredMessage() { return "Your request contains content that is not allowed. Please rephrase your request."; } }
-
Integrate the filter into your AIService:
@Autowired private ContentFilter contentFilter; public List<ChatMessageContent<?>> getAIResponse(ChatRequest chatRequest) throws IOException, ServiceNotFoundException { // Check user input before processing String userMessage = chatRequest.getMessages().get(chatRequest.getMessages().size() - 1).getContent(); if (!contentFilter.isContentAllowed(userMessage)) { throw new RuntimeException(contentFilter.getFilteredMessage()); } // Continue with normal processing... try { Kernel kernel = kernelBuilder(); // ... rest of your existing code } catch (Exception e) { // ... existing exception handling } }
This approach allows you to implement custom filtering logic directly in your Java application, giving you fine-grained control over what content is allowed.
