Skip to content

Commit de553a9

Browse files
committed
add csharp to blocklist how-to
1 parent 87944da commit de553a9

File tree

1 file changed

+263
-7
lines changed

1 file changed

+263
-7
lines changed

articles/cognitive-services/content-safety/how-to/use-blocklist.md

Lines changed: 263 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ The default AI classifiers are sufficient for most content moderation needs. How
2727
* An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/)
2828
* Once you have your Azure subscription, <a href="https://aka.ms/acs-create" title="Create a Content Safety resource" target="_blank">create a Content Safety resource </a> in the Azure portal to get your key and endpoint. Enter a unique name for your resource, select the subscription you entered on the application form, select a resource group, supported region, and supported pricing tier. Then select **Create**.
2929
* The resource takes a few minutes to deploy. After it finishes, Select **go to resource**. In the left pane, under **Resource Management**, select **Subscription Key and Endpoint**. The endpoint and either of the keys are used to call APIs.
30-
* [cURL](https://curl.haxx.se/) or * [Python 3.x](https://www.python.org/) installed
31-
* Your Python installation should include [pip](https://pip.pypa.io/en/stable/). You can check if you have pip installed by running `pip --version` on the command line. Get pip by installing the latest version of Python.
32-
* If you're using the Python SDK, you'll need to install the Azure AI Content Safety client library for Python. Run the command `pip install azure-ai-contentsafety` in your project directory.
30+
* One of the following installed:
31+
* [cURL](https://curl.haxx.se/) for REST API calls.
32+
* [Python 3.x](https://www.python.org/) installed
33+
* Your Python installation should include [pip](https://pip.pypa.io/en/stable/). You can check if you have pip installed by running `pip --version` on the command line. Get pip by installing the latest version of Python.
34+
* If you're using Python, you'll need to install the Azure AI Content Safety client library for Python. Run the command `pip install azure-ai-contentsafety` in your project directory.
35+
* [.NET Runtime](https://dotnet.microsoft.com/download/dotnet/) installed.
36+
* [.NET 6.0](https://dotnet.microsoft.com/download/dotnet-core) SDK or above installed.
37+
* If you're using .NET, you'll need to install the Azure AI Content Safety client library for .NET. Run the command `dotnet add package Azure.AI.ContentSafety --prerelease` in your project directory.
38+
3339

3440
## Analyze text with a blocklist
3541

3642
You can create blocklists to use with the Text API. The following steps help you get started.
3743

38-
39-
4044
### Create or modify a blocklist
4145

4246
#### [REST API](#tab/rest)
@@ -60,6 +64,40 @@ curl --location --request PATCH '<endpoint>/contentsafety/text/blocklists/<your_
6064

6165
The response code should be `201`(created a new list) or `200`(updated an existing list).
6266

67+
#### [C#](#tab/csharp)
68+
69+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
70+
71+
```csharp
72+
string endpoint = "<endpoint>";
73+
string key = "<enter_your_key_here>";
74+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
75+
76+
var blocklistName = "<your_list_id>";
77+
var blocklistDescription = "<description>";
78+
79+
var data = new
80+
{
81+
description = blocklistDescription,
82+
};
83+
84+
var createResponse = client.CreateOrUpdateTextBlocklist(blocklistName, RequestContent.Create(data));
85+
if (createResponse.Status == 201)
86+
{
87+
Console.WriteLine("\nBlocklist {0} created.", blocklistName);
88+
}
89+
else if (createResponse.Status == 200)
90+
{
91+
Console.WriteLine("\nBlocklist {0} updated.", blocklistName);
92+
}
93+
```
94+
95+
1. Replace `<endpoint>` with your endpoint URL.
96+
1. Replace `<enter_your_key_here>` with your key.
97+
1. Replace `<your_list_id>` with a custom name for your list. Also replace the last term of the REST URL with the same name. Allowed characters: 0-9, A-Z, a-z, `- . _ ~`.
98+
1. Optionally replace `<description>` with a custom description.
99+
1. Run the script.
100+
63101
#### [Python](#tab/python)
64102

65103
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -160,6 +198,40 @@ The response code should be `200`.
160198
}
161199
```
162200

201+
#### [C#](#tab/csharp)
202+
203+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
204+
205+
```csharp
206+
string endpoint = "<endpoint>";
207+
string key = "<enter_your_key_here>";
208+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
209+
210+
var blocklistName = "<your_list_id>";
211+
212+
string blockItemText1 = "k*ll";
213+
string blockItemText2 = "h*te";
214+
215+
var blockItems = new TextBlockItemInfo[] { new TextBlockItemInfo(blockItemText1), new TextBlockItemInfo(blockItemText2) };
216+
var addedBlockItems = client.AddBlockItems(blocklistName, new AddBlockItemsOptions(blockItems));
217+
218+
if (addedBlockItems != null && addedBlockItems.Value != null)
219+
{
220+
Console.WriteLine("\nBlockItems added:");
221+
foreach (var addedBlockItem in addedBlockItems.Value.Value)
222+
{
223+
Console.WriteLine("BlockItemId: {0}, Text: {1}, Description: {2}", addedBlockItem.BlockItemId, addedBlockItem.Text, addedBlockItem.Description);
224+
}
225+
}
226+
```
227+
228+
1. Replace `<endpoint>` with your endpoint URL.
229+
1. Replace `<enter_your_key_here>` with your key.
230+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
231+
1. Replace the value of the `block_item_text_1` field with the item you'd like to add to your blocklist. The maximum length of a blockItem is 128 characters.
232+
1. Optionally add more blockItem strings to the `blockItems` parameter.
233+
1. Run the script.
234+
163235
#### [Python](#tab/python)
164236

165237
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -172,7 +244,6 @@ from azure.ai.contentsafety.models import TextBlockItemInfo, AddBlockItemsOption
172244
from azure.core.exceptions import HttpResponseError
173245
import time
174246

175-
176247
endpoint = "<endpoint>"
177248
key = "<enter_your_key_here>"
178249

@@ -268,6 +339,50 @@ The JSON response will contain a `"blocklistMatchResults"` that indicates any ma
268339
}
269340
```
270341

342+
#### [C#](#tab/csharp)
343+
344+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
345+
346+
```csharp
347+
string endpoint = "<endpoint>";
348+
string key = "<enter_your_key_here>";
349+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
350+
351+
var blocklistName = "<your_list_id>";
352+
353+
// After you edit your blocklist, it usually takes effect in 5 minutes, please wait some time before analyzing with blocklist after editing.
354+
var request = new AnalyzeTextOptions("I h*te you and I want to k*ll you");
355+
request.BlocklistNames.Add(blocklistName);
356+
request.BreakByBlocklists = true;
357+
358+
Response<AnalyzeTextResult> response;
359+
try
360+
{
361+
response = client.AnalyzeText(request);
362+
}
363+
catch (RequestFailedException ex)
364+
{
365+
Console.WriteLine("Analyze text failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
366+
throw;
367+
}
368+
369+
if (response.Value.BlocklistsMatchResults != null)
370+
{
371+
Console.WriteLine("\nBlocklist match result:");
372+
foreach (var matchResult in response.Value.BlocklistsMatchResults)
373+
{
374+
Console.WriteLine("Blockitem was hit in text: Offset: {0}, Length: {1}", matchResult.Offset, matchResult.Length);
375+
Console.WriteLine("BlocklistName: {0}, BlockItemId: {1}, BlockItemText: {2}, ", matchResult.BlocklistName, matchResult.BlockItemId, matchResult.BlockItemText);
376+
}
377+
}
378+
```
379+
380+
1. Replace `<endpoint>` with your endpoint URL.
381+
1. Replace `<enter_your_key_here>` with your key.
382+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
383+
1. Replace the `request` input text with whatever text you want to analyze.
384+
1. Run the script.
385+
271386
#### [Python](#tab/python)
272387

273388
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -356,6 +471,30 @@ The status code should be `200` and the response body should look like this:
356471
}
357472
```
358473

474+
#### [C#](#tab/csharp)
475+
476+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
477+
478+
```csharp
479+
string endpoint = "<endpoint>";
480+
string key = "<enter_your_key_here>";
481+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
482+
483+
var blocklistName = "<your_list_id>";
484+
485+
var allBlockitems = client.GetTextBlocklistItems(blocklistName);
486+
Console.WriteLine("\nList BlockItems:");
487+
foreach (var blocklistItem in allBlockitems)
488+
{
489+
Console.WriteLine("BlockItemId: {0}, Text: {1}, Description: {2}", blocklistItem.BlockItemId, blocklistItem.Text, blocklistItem.Description);
490+
}
491+
```
492+
493+
1. Replace `<endpoint>` with your endpoint URL.
494+
1. Replace `<enter_your_key_here>` with your key.
495+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
496+
1. Run the script.
497+
359498
#### [Python](#tab/python)
360499

361500
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -401,7 +540,6 @@ if __name__ == "__main__":
401540
1. Run the script.
402541

403542

404-
405543
---
406544

407545
### Get all blocklists
@@ -431,6 +569,28 @@ The status code should be `200`. The JSON response looks like this:
431569
]
432570
```
433571

572+
#### [C#](#tab/csharp)
573+
574+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
575+
576+
```csharp
577+
string endpoint = "<endpoint>";
578+
string key = "<enter_your_key_here>";
579+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
580+
581+
582+
var blocklists = client.GetTextBlocklists();
583+
Console.WriteLine("\nList blocklists:");
584+
foreach (var blocklist in blocklists)
585+
{
586+
Console.WriteLine("BlocklistName: {0}, Description: {1}", blocklist.BlocklistName, blocklist.Description);
587+
}
588+
```
589+
590+
1. Replace `<endpoint>` with your endpoint URL.
591+
1. Replace `<enter_your_key_here>` with your key.
592+
1. Run the script.
593+
434594
#### [Python](#tab/python)
435595

436596
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -500,6 +660,29 @@ The status code should be `200`. The JSON response looks like this:
500660
}
501661
```
502662

663+
#### [C#](#tab/csharp)
664+
665+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
666+
667+
```csharp
668+
string endpoint = "<endpoint>";
669+
string key = "<enter_your_key_here>";
670+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
671+
672+
var blocklistName = "<your_list_id>";
673+
674+
var getBlocklist = client.GetTextBlocklist(blocklistName);
675+
if (getBlocklist != null && getBlocklist.Value != null)
676+
{
677+
Console.WriteLine("\nGet blocklist:");
678+
Console.WriteLine("BlocklistName: {0}, Description: {1}", getBlocklist.Value.BlocklistName, getBlocklist.Value.Description);
679+
}
680+
```
681+
1. Replace `<endpoint>` with your endpoint URL.
682+
1. Replace `<enter_your_key_here>` with your key.
683+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
684+
1. Run the script.
685+
503686
#### [Python](#tab/python)
504687

505688
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -574,6 +757,29 @@ The status code should be `200`. The JSON response looks like this:
574757
}
575758
```
576759

760+
#### [C#](#tab/csharp)
761+
762+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
763+
764+
```csharp
765+
string endpoint = "<endpoint>";
766+
string key = "<enter_your_key_here>";
767+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
768+
769+
var blocklistName = "<your_list_id>";
770+
771+
var getBlockItemId = addedBlockItems.Value.Value[0].BlockItemId;
772+
var getBlockItem = client.GetTextBlocklistItem(blocklistName, getBlockItemId);
773+
Console.WriteLine("\nGet BlockItem:");
774+
Console.WriteLine("BlockItemId: {0}, Text: {1}, Description: {2}", getBlockItem.Value.BlockItemId, getBlockItem.Value.Text, getBlockItem.Value.Description);
775+
```
776+
777+
1. Replace `<endpoint>` with your endpoint URL.
778+
1. Replace `<enter_your_key_here>` with your key.
779+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
780+
1. Optionally change the value of `getBlockItemId` to the ID of a previously added item.
781+
1. Run the script.
782+
577783
#### [Python](#tab/python)
578784

579785
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -646,6 +852,33 @@ curl --location --request DELETE '<endpoint>/contentsafety/text/blocklists/<your
646852
647853
The response code should be `204`.
648854

855+
#### [C#](#tab/csharp)
856+
857+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
858+
859+
```csharp
860+
string endpoint = "<endpoint>";
861+
string key = "<enter_your_key_here>";
862+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
863+
864+
var blocklistName = "<your_list_id>";
865+
866+
var removeBlockItemId = addedBlockItems.Value.Value[0].BlockItemId;
867+
var removeBlockItemIds = new List<string> { removeBlockItemId };
868+
var removeResult = client.RemoveBlockItems(blocklistName, new RemoveBlockItemsOptions(removeBlockItemIds));
869+
870+
if (removeResult != null && removeResult.Status == 204)
871+
{
872+
Console.WriteLine("\nBlockItem removed: {0}.", removeBlockItemId);
873+
}
874+
```
875+
876+
1. Replace `<endpoint>` with your endpoint URL.
877+
1. Replace `<enter_your_key_here>` with your key.
878+
1. Replace `<your_list_id>` with the ID value you used in the list creation step.
879+
1. Optionally change the value of `removeBlockItemId` to the ID of a previously added item.
880+
1. Run the script.
881+
649882
#### [Python](#tab/python)
650883

651884
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.
@@ -718,6 +951,29 @@ curl --location --request DELETE '<endpoint>/contentsafety/text/blocklists/<your
718951

719952
The response code should be `204`.
720953

954+
#### [C#](#tab/csharp)
955+
956+
Create a new C# console app and open it in your preferred editor or IDE. Paste in the following code.
957+
958+
```csharp
959+
string endpoint = "<endpoint>";
960+
string key = "<enter_your_key_here>";
961+
ContentSafetyClient client = new ContentSafetyClient(new Uri(endpoint), new AzureKeyCredential(key));
962+
963+
var blocklistName = "<your_list_id>";
964+
965+
var deleteResult = client.DeleteTextBlocklist(blocklistName);
966+
if (deleteResult != null && deleteResult.Status == 204)
967+
{
968+
Console.WriteLine("\nDeleted blocklist.");
969+
}
970+
```
971+
972+
1. Replace `<endpoint>` with your endpoint URL.
973+
1. Replace `<enter_your_key_here>` with your key.
974+
1. Replace `<your_list_id>` (in the request URL) with the ID value you used in the list creation step.
975+
1. Run the script.
976+
721977
#### [Python](#tab/python)
722978

723979
Create a new Python script and open it in your preferred editor or IDE. Paste in the following code.

0 commit comments

Comments
 (0)