From 4b0e52c2cb911a5c98ce7634f8058d70cd612f2d Mon Sep 17 00:00:00 2001 From: Alex Wolf Date: Mon, 13 May 2024 10:57:00 -0400 Subject: [PATCH] NET content filtering --- docs/ai/how-to/content-filtering.md | 44 +++++++++++++++++++ .../AIContentFiltering.csproj | 15 +++++++ .../snippets/content-filtering/Program.cs | 35 +++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 docs/ai/how-to/content-filtering.md create mode 100644 docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj create mode 100644 docs/ai/how-to/snippets/content-filtering/Program.cs diff --git a/docs/ai/how-to/content-filtering.md b/docs/ai/how-to/content-filtering.md new file mode 100644 index 0000000000000..704e689430eb4 --- /dev/null +++ b/docs/ai/how-to/content-filtering.md @@ -0,0 +1,44 @@ +--- +title: "Manage OpenAI Content Filtering in a .NET app" +description: "Learn how to manage OpenAI content filtering programmatically in a .NET app using the OpenAI client library." +ms.custom: devx-track-dotnet, devx-track-dotnet-ai +author: alexwolfmsft +ms.author: alexwolf +ms.topic: how-to +ms.date: 05/13/2024 + +#customer intent: As a .NET developer, I want to manage OpenAI Content Filtering in a .NET app + +--- + +# Manage 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](/azure/ai-services/openai/concepts/content-filter) documentation provides a deeper exploration of content filtering concepts and concerns. This article provides examples of how to work with content filtering features in a .NET app. + +## Prerequisites + +* An Azure account that has an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). +* [.NET SDK](https://dotnet.microsoft.com/download/visual-studio-sdks) +* [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource) + +## Configure and test the content filter + +To utilize the sample code in this article, you'll need to create and assign a content filter to your OpenAI model. + +1. [Create and assign a content filter](/azure/ai-services/openai/how-to/content-filters) to a GPT-35 or GPT-4 model. + +1. Create a simple chat completion flow in your .NET app using the `OpenAiClient`. Replace the `YOUR_OPENAI_ENDPOINT`, `YOUR_OPENAI_KEY`, and `YOUR_OPENAI_DEPLOYMENT` values with your own. + + :::code language="csharp" source="./snippets/content-filtering/program.cs" id="chatCompletionFlow"::: + +1. Print out the content filtering results for each category. + + :::code language="csharp" source="./snippets/content-filtering/program.cs" id="printContentFilteringResult"::: + +## Related content + +* [Create and assign a content filter](/azure/ai-services/openai/how-to/content-filters) +* [Content Filtering concepts](/azure/ai-services/openai/concepts/content-filter) +* [Create a chat app](../quickstarts/quickstart-openai-summarize-text.md) diff --git a/docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj b/docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj new file mode 100644 index 0000000000000..9091d90f6d9ba --- /dev/null +++ b/docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + + + + + + + + diff --git a/docs/ai/how-to/snippets/content-filtering/Program.cs b/docs/ai/how-to/snippets/content-filtering/Program.cs new file mode 100644 index 0000000000000..39753fa5701d1 --- /dev/null +++ b/docs/ai/how-to/snippets/content-filtering/Program.cs @@ -0,0 +1,35 @@ +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", //This must match the custom deployment name you chose for your model + Messages = + { + new ChatRequestSystemMessage("You are a helpful assistant."), + new ChatRequestUserMessage("YOUR_PROMPT") + } +}; + + +Response response = client.GetChatCompletions(chatCompletionsOptions); +Console.WriteLine(response.Value.Choices[0].Message.Content); +Console.WriteLine(); +// + +// +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."); +} +// \ No newline at end of file