Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows how to handle content filtering in a .NET app. Azure OpenAI Service includes a content filtering system that works alongside core models. It runs both the prompt and completion through an ensemble of classification models to detect and take 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.
For a deeper exploration of content filtering concepts and concerns, see the Content Filtering documentation.
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 model.
Add the
Azure.AI.OpenAINuGet package to your project.dotnet add package Azure.AI.OpenAIOr, in .NET 10+:
dotnet package add Azure.AI.OpenAICreate a simple chat completion flow in your .NET app using the
AzureOpenAiClient. Replace theYOUR_MODEL_ENDPOINTandYOUR_MODEL_DEPLOYMENT_NAMEvalues with your own.using Azure.AI.OpenAI; using Azure.Identity; using Microsoft.Extensions.AI; IChatClient client = new AzureOpenAIClient( new Uri("YOUR_MODEL_ENDPOINT"), new DefaultAzureCredential()).GetChatClient("YOUR_MODEL_DEPLOYMENT_NAME").AsIChatClient(); try { ChatResponse completion = await client.GetResponseAsync("YOUR_PROMPT"); Console.WriteLine(completion.Messages.Single()); } catch (Exception e) { Console.WriteLine(e.Message); }Replace the
YOUR_PROMPTplaceholder with your own message and run the app to experiment with content filtering results. If you enter a prompt the AI considers unsafe, Azure OpenAI returns a400 Bad Requestcode. The app prints a message in the console similar to the following:
The response was filtered due to the prompt triggering Azure OpenAI's content management policy...