Work with OpenAI content filtering in a .NET app
This article demonstrates how to handle content filtering concerns in a .NET app. Azure OpenAI Service includes a content filtering system that works alongside core models. This system works by running both the prompt and completion through an ensemble of classification models aimed at detecting and preventing the output of harmful content. The content filtering system detects and takes action on specific categories of potentially harmful content in both input prompts and output completions. Variations in API configurations and application design might affect completions and thus filtering behavior.
The Content Filtering documentation provides a deeper exploration of content filtering concepts and concerns. This article provides examples of how to work with content filtering features programmatically in a .NET app.
Prerequisites
- An Azure account that has an active subscription. Create an account for free.
- .NET SDK
- Create and deploy an Azure OpenAI Service resource
Configure and test the content filter
To use the sample code in this article, you need to create and assign a content filter to your OpenAI model.
Create and assign a content filter to your provisioned GPT-35 or GPT-4 model.
Add the
Azure.AI.OpenAI
NuGet package to your project.dotnet add package Azure.AI.OpenAI
Create a simple chat completion flow in your .NET app using the
OpenAiClient
. Replace theYOUR_OPENAI_ENDPOINT
,YOUR_OPENAI_KEY
, andYOUR_OPENAI_DEPLOYMENT
values with your own.using Azure; using Azure.AI.OpenAI; string endpoint = "YOUR_OPENAI_ENDPOINT"; string key = "YOUR_OPENAI_KEY"; OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key)); var chatCompletionsOptions = new ChatCompletionsOptions() { DeploymentName = "YOUR_DEPLOYMENT_NAME", Messages = { new ChatRequestSystemMessage("You are a helpful assistant."), new ChatRequestUserMessage("YOUR_PROMPT") } }; Response<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions); Console.WriteLine(response.Value.Choices[0].Message.Content); Console.WriteLine();
Print out the content filtering results for each category.
foreach (var promptFilterResult in response.Value.PromptFilterResults) { var results = promptFilterResult.ContentFilterResults; Console.WriteLine(@$"Hate category is filtered: {results.Hate.Filtered} with {results.Hate.Severity} severity."); Console.WriteLine(@$"Self-harm category is filtered: {results.SelfHarm.Filtered} with {results.SelfHarm.Severity} severity."); Console.WriteLine(@$"Sexual category is filtered: {results.Sexual.Filtered} with {results.Sexual.Severity} severity."); Console.WriteLine(@$"Violence category is filtered: {results.Violence.Filtered} with {results.Violence.Severity} severity."); }
Replace the
YOUR_PROMPT
placeholder with your own message and run the app to experiment with content filtering results. The following output shows an example of a prompt that triggers a low severity content filtering result:I am sorry if I have done anything to upset you. Is there anything I can do to assist you and make things better? Hate category is filtered: False with low severity. SelfHarm category is filtered: False with safe severity. Sexual category is filtered: False with safe severity. Violence category is filtered: False with low severity.