diff --git a/sdk/contentsafety/ai-content-safety-rest/CHANGELOG.md b/sdk/contentsafety/ai-content-safety-rest/CHANGELOG.md index a58f07508e76..0b60da4abee2 100644 --- a/sdk/contentsafety/ai-content-safety-rest/CHANGELOG.md +++ b/sdk/contentsafety/ai-content-safety-rest/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 1.0.0 (Unreleased) +## 1.0.0 (2023-12-13) ### Features Added @@ -9,7 +9,37 @@ ### Breaking Changes -- Contract change for AnalyzeText, AnalyzeImage, Blocklist management related parameters +Contract change for AnalyzeText, AnalyzeImage, Blocklist management related parameters + +#### AnalyzeText + +- AnalyzeTextOptions + - Renamed `breakByBlocklists` to `haltOnBlocklistHit` + - Add `outputType` +- AnalyzeTextResultOutput + - Renamed `blocklistsMatchResults` to `blocklistsMatch` + - Replaced `TextAnalyzeSeverityResultOutput` by `TextCategoriesAnalysisOutput` + +#### AnalyzeImage + +- AnalyzeImageOptions + - Add `outputType` +- AnalyzeImageResultOutput + - Replaced `ImageAnalyzeSeverityResultOutput` by `ImageCategoriesAnalysisOutput` + +#### Blocklist management + +- Renamed `"/text/blocklists/{blocklistName}:addBlockItems"` to `"/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems"` +- Renamed `"/text/blocklists/{blocklistName}:removeBlockItems"` to `"/text/blocklists/{blocklistName}:removeBlocklistItems"` +- Renamed `"/text/blocklists/{blocklistName}/blockItems"` to `"/text/blocklists/{blocklistName}/blocklistItems"` +- Renamed `"/text/blocklists/{blocklistName}/blockItems/{blockItemId}"` to `"/text/blocklists/{blocklistName}/blocklistItems/{blocklistItemId}"` +- Renamed `AddBlockItemsOptions` to `AddOrUpdateTextBlocklistItemsOptions` + - Renamed `blockItems` to `blocklistItems` +- Renamed `AddBlockItemsResultOutput` to `AddOrUpdateTextBlocklistItemsResultOutput` + - Renamed `value` to `blocklistItems` +- Renamed `RemoveBlockItemsOptions` to `RemoveTextBlocklistItemsOptions` + - Renamed `blockItemIds` to `blocklistItemIds` +- Renamed `TextBlockItemInfo` to `TextBlocklistItem` ## 1.0.0-beta.1 (2023-09-28) diff --git a/sdk/contentsafety/ai-content-safety-rest/README.md b/sdk/contentsafety/ai-content-safety-rest/README.md index 97514d720fe4..aec9cde296a4 100644 --- a/sdk/contentsafety/ai-content-safety-rest/README.md +++ b/sdk/contentsafety/ai-content-safety-rest/README.md @@ -2,6 +2,10 @@ [Azure AI Content Safety](https://learn.microsoft.com/azure/ai-services/content-safety/overview) detects harmful user-generated and AI-generated content in applications and services. Content Safety includes text and image APIs that allow you to detect material that is harmful. +* Text Analysis API: Scans text for sexual content, violence, hate, and self-harm with multi-severity levels. +* Image Analysis API: Scans images for sexual content, violence, hate, and self-harm with multi-severity levels. +* Text Blocklist Management APIs: The default AI classifiers are sufficient for most content safety needs; however, you might need to screen for terms that are specific to your use case. You can create blocklists of terms to use with the Text API. + **Please rely heavily on our [REST client docs](https://github.com/Azure/azure-sdk-for-js/blob/main/documentation/rest-clients.md) to use this library** Key links: @@ -20,7 +24,7 @@ Key links: ### Prerequisites - You need an [Azure subscription](https://azure.microsoft.com/free/) to use this package. -- An existing [Azure AI Content Safety](https://learn.microsoft.com/azure/ai-services/content-safety/overview) instance. +- An [Azure AI Content Safety](https://learn.microsoft.com/azure/ai-services/content-safety/overview) resource, if no existing resource, you could [create a new one](https://aka.ms/acs-create). ### Install the `@azure-rest/ai-content-safety` package @@ -42,12 +46,16 @@ az cognitiveservices account show --name "resource-name" --resource-group "resou #### Create a ContentSafetyClient with AzureKeyCredential +- Step 1: Get the API key + The API key can be found in the [Azure Portal](https://ms.portal.azure.com/#home) or by running the following [Azure CLI](https://learn.microsoft.com/cli/azure/cognitiveservices/account?view=azure-cli-latest#az-cognitiveservices-account-show) command: ```bash az cognitiveservices account keys list --name "" --resource-group "" ``` +- Step 2: Create a ContentSafetyClient with AzureKeyCredential + To use an API key as the `credential` parameter, pass the key as a string into an instance of `AzureKeyCredential`. ```typescript @@ -60,21 +68,22 @@ const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); ``` -#### Create a ContentSafetyClient with Azure Active Directory (AAD) token credential +#### Create a ContentSafetyClient with Microsoft Entra ID (formerly Azure Active Directory (AAD)) token credential + +- Step 1: Enable Microsoft Entra ID for your resource + Please refer to this Cognitive Services authentication document [Authenticate with Microsoft Entra ID](https://learn.microsoft.com/azure/ai-services/authentication?tabs=powershell#authenticate-with-microsoft-entra-id). for the steps to enable AAD for your resource. -To use an [Azure Active Directory (AAD) token credential](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-with-a-pre-fetched-access-token), -provide an instance of the desired credential type obtained from the -[@azure/identity](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) library. + The main steps are: + - Create resource with a custom subdomain. + - Create Service Principal and assign Cognitive Services User role to it. -To authenticate with AAD, you must first `npm` install [`@azure/identity`](https://www.npmjs.com/package/@azure/identity) +- Step 2: Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: +AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET -After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use. +To authenticate with AAD, you must first `npm` install [`@azure/identity`](https://www.npmjs.com/package/@azure/identity). After setup, you can choose which type of [credential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#credentials) from `@azure/identity` to use. As an example, [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential) can be used to authenticate the client. -Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: -AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET - ```typescript import ContentSafetyClient from "@azure-rest/ai-content-safety"; import { DefaultAzureCredential } from "@azure/identity"; @@ -90,8 +99,8 @@ There are different types of analysis available from this service. The following | Feature | Description | | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| Text Analysis API | Scans text for sexual content, violence, hate, and self harm with multi-severity levels. | -| Image Analysis API | Scans images for sexual content, violence, hate, and self harm with multi-severity levels. | +| Text Analysis API | Scans text for sexual content, violence, hate, and self-harm with multi-severity levels. | +| Image Analysis API | Scans images for sexual content, violence, hate, and self-harm with multi-severity levels. | | Text Blocklist Management APIs | The default AI classifiers are sufficient for most content safety needs. However, you might need to screen for terms that are specific to your use case. You can create blocklists of terms to use with the Text API. | ### Harm categories @@ -110,14 +119,14 @@ Classification can be multi-labeled. For example, when a text sample goes throug Every harm category the service applies also comes with a severity level rating. The severity level is meant to indicate the severity of the consequences of showing the flagged content. -**Text**: The current version of the text model supports the full 0-7 severity scale. The classifier detects amongst all severities along this scale. If the user specifies, it can return severities in the trimmed scale of 0, 2, 4, and 6; each two adjacent levels are mapped to a single level. You can refer [text content severity levels definitions][text_severity_levels] for details. +**Text**: The current version of the text model supports the **full 0-7 severity scale**. By default, the response will output 4 values: 0, 2, 4, and 6. Each two adjacent levels are mapped to a single level. Users could use "outputType" in request and set it as "EightSeverityLevels" to get 8 values in output: 0,1,2,3,4,5,6,7. You can refer [text content severity levels definitions](https://learn.microsoft.com/azure/ai-services/content-safety/concepts/harm-categories?tabs=definitions#text-content) for details. - [0,1] -> 0 - [2,3] -> 2 - [4,5] -> 4 - [6,7] -> 6 -**Image**: The current version of the image model supports the trimmed version of the full 0-7 severity scale. The classifier only returns severities 0, 2, 4, and 6; each two adjacent levels are mapped to a single level. You can refer [image content severity levels definitions][image_severity_levels] for details. +**Image**: The current version of the image model supports the **trimmed version of the full 0-7 severity scale**. The classifier only returns severities 0, 2, 4, and 6; each two adjacent levels are mapped to a single level. You can refer [image content severity levels definitions](https://learn.microsoft.com/azure/ai-services/content-safety/concepts/harm-categories?tabs=definitions#image-content) for details. - [0,1] -> 0 - [2,3] -> 2 @@ -139,7 +148,7 @@ You can set the blocklists you want to use when analyze text, then you can get b ## Examples -The following section provides several code snippets covering some of the most common Content Safety service tasks, including: +The following section provides several code snippets covering some of the most common Content Safety service tasks in both **TypeScript** and **JavaScript**, including: - [Analyze text](#analyze-text) - [Analyze image](#analyze-image) @@ -149,360 +158,1152 @@ The following section provides several code snippets covering some of the most c #### Analyze text without blocklists +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +async function main() { + const endpoint = "https://.cognitiveservices.azure.com/"; + const key = ""; -const credential = new AzureKeyCredential(key); -const client = ContentSafetyClient(endpoint, credential); + const credential = new AzureKeyCredential(key); + const client = ContentSafetyClient(endpoint, credential); -const text = "This is a sample text"; -const analyzeTextOption = { text: text }; -const analyzeTextParameters = { body: analyzeTextOption }; + const text = "This is a sample text"; + const analyzeTextOption = { text: text }; + const analyzeTextParameters = { body: analyzeTextOption }; -const result = await client.path("/text:analyze").post(analyzeTextParameters); + const result = await client.path("/text:analyze").post(analyzeTextParameters); -if (isUnexpected(result)) { - throw result; + if (isUnexpected(result)) { + throw result; + } + + for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { + const textCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; + console.log(textCategoriesAnalysisOutput.category, " severity: ", textCategoriesAnalysisOutput.severity) + } } -for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { - const textCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; - console.log(textCategoriesAnalysisOutput.category, " severity: ", textCategoriesAnalysisOutput.severity) +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +async function main() { + const endpoint = "https://.cognitiveservices.azure.com/"; + const key = ""; + + const credential = new AzureKeyCredential(key); + const client = ContentSafetyClient(endpoint, credential); + + const text = "This is a sample text"; + const analyzeTextOption = { text: text }; + const analyzeTextParameters = { body: analyzeTextOption }; + + const result = await client.path("/text:analyze").post(analyzeTextParameters); + + if (isUnexpected(result)) { + throw result; + } + + for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { + const textCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; + console.log( + textCategoriesAnalysisOutput.category, + " severity: ", + textCategoriesAnalysisOutput.severity + ); + } } + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Analyze text with blocklists +TypeScript + ```typescript -import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; +import ContentSafetyClient, { CreateOrUpdateTextBlocklistParameters, isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; -const inputText = "This is a sample to test text with blocklist."; -const analyzeTextParameters = { - body: { - text: inputText, - blocklistNames: [blocklistName], - haltOnBlocklistHit: false +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters: CreateOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + } } -}; -const result = await client.path("/text:analyze").post(analyzeTextParameters); + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).patch(createOrUpdateTextBlocklistParameters); -if (isUnexpected(result)) { - throw result; + if (isUnexpected(result)) { + throw result; + } + + console.log("Blocklist created or updated: Name", result.body.blocklistName, ", Description: ", result.body.description); +} + +async function addBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText1 = "sample"; + const blockItemText2 = "text"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText1 + }, + { + description: "Test block item 2", + text: blockItemText2 + } + ] + } + }; + + const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Block items added: "); + if (result.body.blocklistItems) { + for (const blockItem of result.body.blocklistItems) { + console.log("BlockItemId: ", blockItem.blocklistItemId, ", Text: ", blockItem.text, ", Description: ", blockItem.description); + } + } } -console.log("Blocklist match results: "); -if (result.body.blocklistsMatch) { - for (const blocklistMatchResult of result.body.blocklistsMatch) { - console.log("BlocklistName: ", blocklistMatchResult.blocklistName, ", BlockItemId: ", blocklistMatchResult.blocklistItemId, ", BlockItemText: ", blocklistMatchResult.blocklistItemText); +async function analyzeTextWithBlocklists() { + const blocklistName = "TestBlocklist"; + const inputText = "This is a sample to test text with blocklist."; + const analyzeTextParameters = { + body: { + text: inputText, + blocklistNames: [blocklistName], + haltOnBlocklistHit: false + } + }; + + const result = await client.path("/text:analyze").post(analyzeTextParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Blocklist match results: "); + if (result.body.blocklistsMatch) { + for (const blocklistMatchResult of result.body.blocklistsMatch) { + console.log("BlocklistName: ", blocklistMatchResult.blocklistName, ", BlockItemId: ", blocklistMatchResult.blocklistItemId, ", BlockItemText: ", blocklistMatchResult.blocklistItemText); + } } } + +(async () => { + await createOrUpdateTextBlocklist(); + await addBlockItems(); + await analyzeTextWithBlocklists(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + }, + }; + + const result = await client + .path("/text/blocklists/{blocklistName}", blocklistName) + .patch(createOrUpdateTextBlocklistParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log( + "Blocklist created or updated: Name", + result.body.blocklistName, + ", Description: ", + result.body.description + ); +} + +async function addBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText1 = "sample"; + const blockItemText2 = "text"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText1, + }, + { + description: "Test block item 2", + text: blockItemText2, + }, + ], + }, + }; + + const result = await client + .path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName) + .post(addOrUpdateBlocklistItemsParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Block items added: "); + if (result.body.blocklistItems) { + for (const blockItem of result.body.blocklistItems) { + console.log( + "BlockItemId: ", + blockItem.blocklistItemId, + ", Text: ", + blockItem.text, + ", Description: ", + blockItem.description + ); + } + } +} + +async function analyzeTextWithBlocklists() { + const blocklistName = "TestBlocklist"; + const inputText = "This is a sample to test text with blocklist."; + const analyzeTextParameters = { + body: { + text: inputText, + blocklistNames: [blocklistName], + haltOnBlocklistHit: false, + }, + }; + + const result = await client.path("/text:analyze").post(analyzeTextParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Blocklist match results: "); + if (result.body.blocklistsMatch) { + for (const blocklistMatchResult of result.body.blocklistsMatch) { + console.log( + "BlocklistName: ", + blocklistMatchResult.blocklistName, + ", BlockItemId: ", + blocklistMatchResult.blocklistItemId, + ", BlockItemText: ", + blocklistMatchResult.blocklistItemText + ); + } + } +} + +(async () => { + await createOrUpdateTextBlocklist(); + await addBlockItems(); + await analyzeTextWithBlocklists(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` ### Analyze image +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; import fs from "fs"; import path from "path"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +async function main() { + const endpoint = "https://.cognitiveservices.azure.com/"; + const key = ""; -const credential = new AzureKeyCredential(key); -const client = ContentSafetyClient(endpoint, credential); + const credential = new AzureKeyCredential(key); + const client = ContentSafetyClient(endpoint, credential); -const image_path = path.resolve(__dirname, "./samples-dev/example-data/image.png"); + const image_path = path.resolve(__dirname, "./samples-dev/example-data/image.png"); -const imageBuffer = fs.readFileSync(image_path); -const base64Image = imageBuffer.toString("base64"); -const analyzeImageOption = { image: { content: base64Image } }; -const analyzeImageParameters = { body: analyzeImageOption }; + const imageBuffer = fs.readFileSync(image_path); + const base64Image = imageBuffer.toString("base64"); + const analyzeImageOption = { image: { content: base64Image } }; + const analyzeImageParameters = { body: analyzeImageOption }; -const result = await client.path("/image:analyze").post(analyzeImageParameters); + const result = await client.path("/image:analyze").post(analyzeImageParameters); + + if (isUnexpected(result)) { + throw result; + } -if (isUnexpected(result)) { - throw result; + for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { + const imageCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; + console.log(imageCategoriesAnalysisOutput.category, " severity: ", imageCategoriesAnalysisOutput.severity) + } } -for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { - const imageCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; - console.log(imageCategoriesAnalysisOutput.category, " severity: ", imageCategoriesAnalysisOutput.severity) +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); +const fs = require("fs"); +const path = require("path"); + +async function main() { + const endpoint = "https://.cognitiveservices.azure.com/"; + const key = ""; + + const credential = new AzureKeyCredential(key); + const client = ContentSafetyClient(endpoint, credential); + + const image_path = path.resolve(__dirname, "./samples-dev/example-data/image.png"); + + const imageBuffer = fs.readFileSync(image_path); + const base64Image = imageBuffer.toString("base64"); + const analyzeImageOption = { image: { content: base64Image } }; + const analyzeImageParameters = { body: analyzeImageOption }; + + const result = await client.path("/image:analyze").post(analyzeImageParameters); + + if (isUnexpected(result)) { + throw result; + } + + for (let i = 0; i < result.body.categoriesAnalysis.length; i++) { + const imageCategoriesAnalysisOutput = result.body.categoriesAnalysis[i]; + console.log( + imageCategoriesAnalysisOutput.category, + " severity: ", + imageCategoriesAnalysisOutput.severity + ); + } } + +main().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` ### Manage text blocklist #### Create or update text blocklist +TypeScript + ```typescript import ContentSafetyClient, { CreateOrUpdateTextBlocklistParameters, isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; - -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; -const blocklistDescription = "Test blocklist management."; -const createOrUpdateTextBlocklistParameters: CreateOrUpdateTextBlocklistParameters = { - contentType: "application/merge-patch+json", - body: { - description: blocklistDescription, +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters: CreateOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + } + } + + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).patch(createOrUpdateTextBlocklistParameters); + + if (isUnexpected(result)) { + throw result; } + + console.log("Blocklist created or updated: Name", result.body.blocklistName, ", Description: ", result.body.description); } -const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).patch(createOrUpdateTextBlocklistParameters); +(async () => { + await createOrUpdateTextBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` -if (isUnexpected(result)) { - throw result; +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + }, + }; + + const result = await client + .path("/text/blocklists/{blocklistName}", blocklistName) + .patch(createOrUpdateTextBlocklistParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log( + "Blocklist created or updated: Name", + result.body.blocklistName, + ", Description: ", + result.body.description + ); } -console.log("Blocklist created or updated: Name", result.body.blocklistName, ", Description: ", result.body.description); +(async () => { + await createOrUpdateTextBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### List text blocklists +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; - -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const result = await client.path("/text/blocklists").get(); +async function listTextBlocklists() { + const result = await client.path("/text/blocklists").get(); + + if (isUnexpected(result)) { + throw result; + } -if (isUnexpected(result)) { - throw result; + console.log("List blocklists: "); + if (result.body.value) { + for (const blocklist of result.body.value) { + console.log("BlocklistName: ", blocklist.blocklistName, ", Description: ", blocklist.description); + } + } } -console.log("List blocklists: "); -if (result.body.value) { - for (const blocklist of result.body.value) { - console.log("BlocklistName: ", blocklist.blocklistName, ", Description: ", blocklist.description); +(async () => { + await listTextBlocklists(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function listTextBlocklists() { + const result = await client.path("/text/blocklists").get(); + + if (isUnexpected(result)) { + throw result; + } + + console.log("List blocklists: "); + if (result.body.value) { + for (const blocklist of result.body.value) { + console.log( + "BlocklistName: ", + blocklist.blocklistName, + ", Description: ", + blocklist.description + ); + } } } + +(async () => { + await listTextBlocklists(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Get text blocklist +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function getTextBlocklist() { + const blocklistName = "TestBlocklist"; + + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).get(); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Get blocklist: "); + console.log("Name: ", result.body.blocklistName, ", Description: ", result.body.description); +} + +(async () => { + await getTextBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; +async function getTextBlocklist() { + const blocklistName = "TestBlocklist"; -const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).get(); + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).get(); -if (isUnexpected(result)) { - throw result; + if (isUnexpected(result)) { + throw result; + } + + console.log("Get blocklist: "); + console.log("Name: ", result.body.blocklistName, ", Description: ", result.body.description); } -console.log("Get blocklist: "); -console.log("Name: ", result.body.blocklistName, ", Description: ", result.body.description); +(async () => { + await getTextBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Delete text blocklist +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; +async function deleteBlocklist() { + const blocklistName = "TestBlocklist"; + + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).delete(); -const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).delete(); + if (isUnexpected(result)) { + throw result; + } -if (isUnexpected(result)) { - throw result; + console.log("Deleted blocklist: ", blocklistName); } -console.log("Deleted blocklist: ", blocklistName); +(async () => { + await deleteBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function deleteBlocklist() { + const blocklistName = "TestBlocklist"; + + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).delete(); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Deleted blocklist: ", blocklistName); +} + +(async () => { + await deleteBlocklist(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Add blockItems +TypeScript + ```typescript -import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; +import ContentSafetyClient, { CreateOrUpdateTextBlocklistParameters, isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters: CreateOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + } + } + + const result = await client.path("/text/blocklists/{blocklistName}", blocklistName).patch(createOrUpdateTextBlocklistParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Blocklist created or updated: Name", result.body.blocklistName, ", Description: ", result.body.description); +} + +async function addBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText1 = "sample"; + const blockItemText2 = "text"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText1 + }, + { + description: "Test block item 2", + text: blockItemText2 + } + ] + } + }; + + const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Block items added: "); + if (result.body.blocklistItems) { + for (const blockItem of result.body.blocklistItems) { + console.log("BlockItemId: ", blockItem.blocklistItemId, ", Text: ", blockItem.text, ", Description: ", blockItem.description); + } + } +} + +(async () => { + await createOrUpdateTextBlocklist(); + await addBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; -const blockItemText1 = "sample"; -const blockItemText2 = "text"; -const addOrUpdateBlocklistItemsParameters = { - body: { - blocklistItems: [ - { - description: "Test block item 1", - text: blockItemText1 - }, - { - description: "Test block item 2", - text: blockItemText2 - } - ] - } -}; - -const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); - -if (isUnexpected(result)) { - throw result; +async function createOrUpdateTextBlocklist() { + const blocklistName = "TestBlocklist"; + const blocklistDescription = "Test blocklist management."; + const createOrUpdateTextBlocklistParameters = { + contentType: "application/merge-patch+json", + body: { + description: blocklistDescription, + }, + }; + + const result = await client + .path("/text/blocklists/{blocklistName}", blocklistName) + .patch(createOrUpdateTextBlocklistParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log( + "Blocklist created or updated: Name", + result.body.blocklistName, + ", Description: ", + result.body.description + ); } -console.log("Block items added: "); -if (result.body.blocklistItems) { - for (const blockItem of result.body.blocklistItems) { - console.log("BlockItemId: ", blockItem.blocklistItemId, ", Text: ", blockItem.text, ", Description: ", blockItem.description); +async function addBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText1 = "sample"; + const blockItemText2 = "text"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText1, + }, + { + description: "Test block item 2", + text: blockItemText2, + }, + ], + }, + }; + + const result = await client + .path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName) + .post(addOrUpdateBlocklistItemsParameters); + + if (isUnexpected(result)) { + throw result; + } + + console.log("Block items added: "); + if (result.body.blocklistItems) { + for (const blockItem of result.body.blocklistItems) { + console.log( + "BlockItemId: ", + blockItem.blocklistItemId, + ", Text: ", + blockItem.text, + ", Description: ", + blockItem.description + ); + } } } + +(async () => { + await createOrUpdateTextBlocklist(); + await addBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### List blockItems +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; +async function listBlockItems() { + const blocklistName = "TestBlocklist"; + + const result = await client.path("/text/blocklists/{blocklistName}/blocklistItems", blocklistName).get(); -const result = await client.path("/text/blocklists/{blocklistName}/blocklistItems", blocklistName).get(); + if (isUnexpected(result)) { + throw result; + } -if (isUnexpected(result)) { - throw result; + console.log("List block items: "); + if (result.body.value) { + for (const blockItem of result.body.value) { + console.log("BlockItemId: ", blockItem.blocklistItemId, ", Text: ", blockItem.text, ", Description: ", blockItem.description); + } + } } -console.log("List block items: "); -if (result.body.value) { - for (const blockItem of result.body.value) { - console.log("BlockItemId: ", blockItem.blocklistItemId, ", Text: ", blockItem.text, ", Description: ", blockItem.description); +(async () => { + await listBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function listBlockItems() { + const blocklistName = "TestBlocklist"; + + const result = await client + .path("/text/blocklists/{blocklistName}/blocklistItems", blocklistName) + .get(); + + if (isUnexpected(result)) { + throw result; + } + + console.log("List block items: "); + if (result.body.value) { + for (const blockItem of result.body.value) { + console.log( + "BlockItemId: ", + blockItem.blocklistItemId, + ", Text: ", + blockItem.text, + ", Description: ", + blockItem.description + ); + } } } + +(async () => { + await listBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Get blockItem +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; -const blockItemText = "sample"; -const addOrUpdateBlocklistItemsParameters = { - body: { - blocklistItems: [ - { - description: "Test block item 1", - text: blockItemText - } - ] - } -}; -const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); -if (isUnexpected(result) || result.body.blocklistItems === undefined) { - throw new Error("Block item not added."); +async function getBlockItem() { + const blocklistName = "TestBlocklist"; + const blockItemText = "sample"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText + } + ] + } + }; + const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); + if (isUnexpected(result) || result.body.blocklistItems === undefined) { + throw new Error("Block item not added."); + } + const blockItemId = result.body.blocklistItems[0].blocklistItemId; + + const blockItem = await client.path("/text/blocklists/{blocklistName}/blocklistItems/{blocklistItemId}", blocklistName, blockItemId).get(); + + if (isUnexpected(blockItem)) { + throw blockItem; + } + + console.log("Get blockitem: "); + console.log("BlockItemId: ", blockItem.body.blocklistItemId, ", Text: ", blockItem.body.text, ", Description: ", blockItem.body.description); } -const blockItemId = result.body.blocklistItems[0].blocklistItemId; -const blockItem = await client.path("/text/blocklists/{blocklistName}/blocklistItems/{blocklistItemId}", blocklistName, blockItemId).get(); +(async () => { + await getBlockItem(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function getBlockItem() { + const blocklistName = "TestBlocklist"; + const blockItemText = "sample"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText, + }, + ], + }, + }; + const result = await client + .path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName) + .post(addOrUpdateBlocklistItemsParameters); + if (isUnexpected(result) || result.body.blocklistItems === undefined) { + throw new Error("Block item not added."); + } + const blockItemId = result.body.blocklistItems[0].blocklistItemId; + + const blockItem = await client + .path( + "/text/blocklists/{blocklistName}/blocklistItems/{blocklistItemId}", + blocklistName, + blockItemId + ) + .get(); + + if (isUnexpected(blockItem)) { + throw blockItem; + } -if (isUnexpected(blockItem)) { - throw blockItem; + console.log("Get blockitem: "); + console.log( + "BlockItemId: ", + blockItem.body.blocklistItemId, + ", Text: ", + blockItem.body.text, + ", Description: ", + blockItem.body.description + ); } -console.log("Get blockitem: "); -console.log("BlockItemId: ", blockItem.body.blocklistItemId, ", Text: ", blockItem.body.text, ", Description: ", blockItem.body.description); +(async () => { + await getBlockItem(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` #### Remove blockItems +TypeScript + ```typescript import ContentSafetyClient, { isUnexpected } from "@azure-rest/ai-content-safety"; import { AzureKeyCredential } from "@azure/core-auth"; -const endpoint = process.env["CONTENT_SAFETY_ENDPOINT"] || ""; -const key = process.env["CONTENT_SAFETY_API_KEY"] || ""; +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; const credential = new AzureKeyCredential(key); const client = ContentSafetyClient(endpoint, credential); -const blocklistName = "TestBlocklist"; -const blockItemText = "sample"; -const addOrUpdateBlocklistItemsParameters = { - body: { - blocklistItems: [ - { - description: "Test block item 1", - text: blockItemText - } - ] - } -}; -const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); -if (isUnexpected(result) || result.body.blocklistItems === undefined) { - throw new Error("Block item not added."); +async function removeBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText = "sample"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText + } + ] + } + }; + const result = await client.path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName).post(addOrUpdateBlocklistItemsParameters); + if (isUnexpected(result) || result.body.blocklistItems === undefined) { + throw new Error("Block item not added."); + } + const blockItemId = result.body.blocklistItems[0].blocklistItemId; + + const removeBlocklistItemsParameters = { + body: { + blocklistItemIds: [blockItemId] + } + }; + const removeBlockItem = await client.path("/text/blocklists/{blocklistName}:removeBlocklistItems", blocklistName).post(removeBlocklistItemsParameters); + + if (isUnexpected(removeBlockItem)) { + throw removeBlockItem; + } + + console.log("Removed blockItem: ", blockItemText); } -const blockItemId = result.body.blocklistItems[0].blocklistItemId; -const removeBlocklistItemsParameters = { - body: { - blocklistItemIds: [blockItemId] +(async () => { + await removeBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); +``` + +JavaScript + +```javascript +const ContentSafetyClient = require("@azure-rest/ai-content-safety").default, + { isUnexpected } = require("@azure-rest/ai-content-safety"); +const { AzureKeyCredential } = require("@azure/core-auth"); + +const endpoint = "https://.cognitiveservices.azure.com/"; +const key = ""; + +const credential = new AzureKeyCredential(key); +const client = ContentSafetyClient(endpoint, credential); + +async function removeBlockItems() { + const blocklistName = "TestBlocklist"; + const blockItemText = "sample"; + const addOrUpdateBlocklistItemsParameters = { + body: { + blocklistItems: [ + { + description: "Test block item 1", + text: blockItemText, + }, + ], + }, + }; + const result = await client + .path("/text/blocklists/{blocklistName}:addOrUpdateBlocklistItems", blocklistName) + .post(addOrUpdateBlocklistItemsParameters); + if (isUnexpected(result) || result.body.blocklistItems === undefined) { + throw new Error("Block item not added."); + } + const blockItemId = result.body.blocklistItems[0].blocklistItemId; + + const removeBlocklistItemsParameters = { + body: { + blocklistItemIds: [blockItemId], + }, + }; + const removeBlockItem = await client + .path("/text/blocklists/{blocklistName}:removeBlocklistItems", blocklistName) + .post(removeBlocklistItemsParameters); + + if (isUnexpected(removeBlockItem)) { + throw removeBlockItem; } -}; -const removeBlockItem = await client.path("/text/blocklists/{blocklistName}:removeBlocklistItems", blocklistName).post(removeBlocklistItemsParameters); -if (isUnexpected(removeBlockItem)) { - throw removeBlockItem; + console.log("Removed blockItem: ", blockItemText); } -console.log("Removed blockItem: ", blockItemText); +(async () => { + await removeBlockItems(); +})().catch((err) => { + console.error("The sample encountered an error:", err); +}); ``` ## Troubleshooting