forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08c44e5
commit 4b0e52c
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
15 changes: 15 additions & 0 deletions
15
docs/ai/how-to/snippets/content-filtering/AIContentFiltering.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.17" /> | ||
<PackageReference Include="Azure.Core" Version="1.39.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Azure; | ||
using Azure.AI.OpenAI; | ||
|
||
// <chatCompletionFlow> | ||
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<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions); | ||
Console.WriteLine(response.Value.Choices[0].Message.Content); | ||
Console.WriteLine(); | ||
// </chatCompletionFlow> | ||
|
||
// <printContentFilteringResult> | ||
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."); | ||
} | ||
// </printContentFilteringResult> |