Share via


ContentSafetyClient.AnalyzeTextAsync Method

Definition

Overloads

AnalyzeTextAsync(String, CancellationToken)

Analyze Text Async.

AnalyzeTextAsync(RequestContent, RequestContext)

[Protocol Method] Analyze Text

AnalyzeTextAsync(AnalyzeTextOptions, CancellationToken)

Analyze Text.

AnalyzeTextAsync(String, CancellationToken)

Source:
ContentSafetyClient.cs

Analyze Text Async.

public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>> AnalyzeTextAsync (string text, System.Threading.CancellationToken cancellationToken = default);
abstract member AnalyzeTextAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
override this.AnalyzeTextAsync : string * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
Public Overridable Function AnalyzeTextAsync (text As String, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of AnalyzeTextResult))

Parameters

text
String
cancellationToken
CancellationToken

Returns

Applies to

AnalyzeTextAsync(RequestContent, RequestContext)

Source:
ContentSafetyClient.cs
Source:
ContentSafetyClient.cs

[Protocol Method] Analyze Text

public virtual System.Threading.Tasks.Task<Azure.Response> AnalyzeTextAsync (Azure.Core.RequestContent content, Azure.RequestContext context = default);
abstract member AnalyzeTextAsync : Azure.Core.RequestContent * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
override this.AnalyzeTextAsync : Azure.Core.RequestContent * Azure.RequestContext -> System.Threading.Tasks.Task<Azure.Response>
Public Overridable Function AnalyzeTextAsync (content As RequestContent, Optional context As RequestContext = Nothing) As Task(Of Response)

Parameters

content
RequestContent

The content to send as the body of the request.

context
RequestContext

The request context, which can override default behaviors of the client pipeline on a per-call basis.

Returns

The response returned from the service.

Exceptions

content is null.

Service returned a non-success status code.

Examples

This sample shows how to call AnalyzeTextAsync and parse the result.

Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    text = "<text>",
});
Response response = await client.AnalyzeTextAsync(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("category").ToString());

This sample shows how to call AnalyzeTextAsync with all request content and parse the result.

Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);

using RequestContent content = RequestContent.Create(new
{
    text = "<text>",
    categories = new object[]
    {
        "Hate"
    },
    blocklistNames = new object[]
    {
        "<blocklistNames>"
    },
    haltOnBlocklistHit = true,
    outputType = "FourSeverityLevels",
});
Response response = await client.AnalyzeTextAsync(content);

JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistName").ToString());
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistItemId").ToString());
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistItemText").ToString());
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("category").ToString());
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("severity").ToString());

Applies to

AnalyzeTextAsync(AnalyzeTextOptions, CancellationToken)

Source:
ContentSafetyClient.cs
Source:
ContentSafetyClient.cs

Analyze Text.

public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>> AnalyzeTextAsync (Azure.AI.ContentSafety.AnalyzeTextOptions options, System.Threading.CancellationToken cancellationToken = default);
public virtual System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>> AnalyzeTextAsync (Azure.AI.ContentSafety.AnalyzeTextOptions body, System.Threading.CancellationToken cancellationToken = default);
abstract member AnalyzeTextAsync : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
override this.AnalyzeTextAsync : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
abstract member AnalyzeTextAsync : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
override this.AnalyzeTextAsync : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task<Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>>
Public Overridable Function AnalyzeTextAsync (options As AnalyzeTextOptions, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of AnalyzeTextResult))
Public Overridable Function AnalyzeTextAsync (body As AnalyzeTextOptions, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Response(Of AnalyzeTextResult))

Parameters

optionsbody
AnalyzeTextOptions

The text analysis request.

cancellationToken
CancellationToken

The cancellation token to use.

Returns

Exceptions

options is null.

Examples

This sample shows how to call AnalyzeTextAsync.

Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);

AnalyzeTextOptions options = new AnalyzeTextOptions("<text>");
Response<AnalyzeTextResult> response = await client.AnalyzeTextAsync(options);

This sample shows how to call AnalyzeTextAsync with all parameters.

Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);

AnalyzeTextOptions options = new AnalyzeTextOptions("<text>")
{
    Categories = { TextCategory.Hate },
    BlocklistNames = { "<blocklistNames>" },
    HaltOnBlocklistHit = true,
    OutputType = AnalyzeTextOutputType.FourSeverityLevels,
};
Response<AnalyzeTextResult> response = await client.AnalyzeTextAsync(options);

Remarks

A synchronous API for the analysis of potentially harmful text content. Currently, it supports four categories: Hate, SelfHarm, Sexual, and Violence.

Applies to