Getting started with the Azure Content Safety API.
Tags: azure, ai, csharp, blazor
Published: 2023-12-23 05:00:00

_This article was published as part of the C# Advent 2023, you can check out the rest of the articles [here](https://csadvent.christmas/)._

Earlier this year Microsoft added a new API to the Azure AI suite called the Content Safety API. This API replaces the Content Moderator API which will be deprecated next year and eventually retired in 2027. As the name suggests, the purpose of this API is to detect harmful content to allow organizations to comply with regulations and / or provide the best experience to their users.

The API provides 4 different types of analysis:

- 
**Analyze Text.** Scan text for sexual content, violence, hate, and self-harm, the API supports multiple languages and different severity levels.

- 
**Analyze Image.** Scan images for sexual content, violence, hate, and self-harm, the API supports different severity levels.

- 
**Jailbreak risk detection.** Scan text for the risk of a jailbreak attack on a LLM.

- 
**Protected material text detection.** Scans AI-generated text for known text content.

## API integration

In this article we will focus on the image and text analysis since they are the only methods available via the C# SDK at the moment, the code samples are from a demo application built using Blazor (you can find the code [here](https://github.com/drkclw/ContentSafetyDemo)). In order to access the API we need to create a content safety resource in the Azure Portal and retrieve the endpoint and keys:


![Creating an Content Safety resource.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smuw51b1p8ym1q16phte.png)

![Retrieving endpoint and keys.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/85zico1jq5xww9y0ppen.png)

Next, we add the C# SDK which is available as a nuget package:


![Content Safety nuget package.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yaucwmtqa8chjmesmhxr.png)

Once the package is added, we can add a ContentSafetyClient to the application:

```
builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddContentSafetyClient(new Uri(builder.Configuration["contentSafetyEndpoint"]),
        new Azure.AzureKeyCredential(builder.Configuration["contentSafetyKey"]));
});
```

The client has 2 methods: AnalyzeImage and AnalyzeText (with corresponding async versions). The AnalyzeImage method takes an AnalyzeImageOption object as a parameter, this object includes the image data:

```
public async Task<AnalyzeImageResult> AnalyzeImage(Stream imageStream)
{
    try
    {
        var imageData = await BinaryData.FromStreamAsync(imageStream);
        ContentSafetyImageData image = new ContentSafetyImageData(imageData);

        var request = new AnalyzeImageOptions(image);
        Response<AnalyzeImageResult> response;

        response = await _contentSafetyClient.AnalyzeImageAsync(request);
        return response;
    }
    catch (RequestFailedException ex)
    {
        Console.WriteLine("Analyze image failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
        throw;
    }
}
```

The AnalyzeImageResult object returned by the method includes the severity per category, which we can then display in our application:


![Displaying image analysis results.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtb0t0ctkpb4o2k4bdsl.png)

Below are the results when running a test with an image from a "Fatality" from the Mortal Kombat videogame:


![Content Safety API result.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31hnr25b5jf6o2jz4zak.png)

The AnalyzeText method takes an AnalyzeTextOptions object as a parameter, which allows you to specify the text and the output type. Changing the output type updates the severity scale so you can filter at a more granular level. In addition to that, you can specify a "blocklist" which is a list of terms that might not be identified by the base model or that are specific to your use case.

```
public async Task<AnalyzeTextResult> AnalyzeText(string text, int outputType)
{
    try
    {
        var request = new AnalyzeTextOptions(text);
        request.BlocklistNames.Add("DemoBlocklist");
        request.OutputType = outputType == 1 ? AnalyzeTextOutputType.FourSeverityLevels :
            AnalyzeTextOutputType.EightSeverityLevels;
        Response<AnalyzeTextResult> response = await _contentSafetyClient.AnalyzeTextAsync(
            request);
        return response;
    }
    catch (RequestFailedException ex)
    {
        Console.WriteLine("Analyze image failed.\nStatus code: {0}, Error code: {1}, Error message: {2}", ex.Status, ex.ErrorCode, ex.Message);
        throw;
    }
}
```

On the application side, we can test this with any text we want. Below are the results when analyzing text with different output types:


![Four level analysis.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmhvrkmdn8kiotqp1lvq.png)


![Eight level analysis.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnnqql9dn7gh3szpf21o.png)

The content safety resource used during testing is using a blocklist with the term "developer" in it. This is an example of the result:


![Blocklist example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9czzay4aqnqizhmafruc.png)

As you can see, the severity and blocklist results are independent of each other, so even if the text is not flagged in any of the 4 existing categories it will still be flagged if it contains any of the terms in the blocklist.


## Content safety studio.


If you don't want to create a POC application to evaluate the API's capabilities Microsoft has you covered! **Content Safety Studio** is an online tool that you can use to do that and more, additional functionality includes:

- Create and edit blocklists.

- Test preview functionality.

- Monitor activity (image below).


![Blocklist statistics.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtltry1fcmftigxe1v4q.png)


![Severity statistics.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w0mfiz2kj3r92bl8ai1.png)




## Summary.


The Azure Content Safety API is a great option to easily identify harmful content in text and images across 4 categories: sexual content, violence, hate, and self-harm. The results are also scored according to the severity of the content so the appropriate actions can be taken. Additional functionality currently in preview allows for content filtering in LLM models and is available through the Content Safety Studio tool.


**Additional resources:**

- [Content Safety documentation](https://learn.microsoft.com/en-us/azure/ai-services/content-safety/overview).

- [Content Safety studio](https://contentsafety.cognitive.azure.com/).

- [Azure AI studio](https://ai.azure.com/).

- [Demo app repo](https://github.com/drkclw/ContentSafetyDemo).