你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用自定义类别(快速)API

自定义类别(快速)API 可让你快速响应新出现的有害内容事件。 可以使用特定主题中的几个示例定义事件,然后该服务将开始检测类似的内容。

按照以下步骤使用一些文本内容示例定义事件,然后分析新文本内容,看它是否与事件匹配。

重要

此功能仅在选定的 Azure 区域可用。 请参阅上市区域

注意

本指南中的示例数据可能包含冒犯性内容。 建议用户自行决定。

先决条件

  • Azure 订阅 - 免费创建订阅
  • 拥有 Azure 订阅后,请在 Azure 门户中创建 Content Safety 资源 ,以获取密钥和终结点。 输入资源的唯一名称,选择订阅,并选择资源组、支持的区域(请参阅上市区域)和支持的定价层。 然后选择“创建”。
    • 部署资源需要几分钟时间。 完成后,选择“转到资源”。 在左侧窗格中的“资源管理”下,选择“订阅密钥和终结点”。 终结点和任一密钥都用于调用 API。
  • 如果想要上传图像,还可以创建 Blob 存储容器。 或者,可以将图像编码为 Base64 字符串,并直接在 API 调用中使用它们。
  • 已安装以下项之一:

测试文本自定义类别(快速)API

使用本节中的示例代码创建文本事件、向事件添加示例、部署事件,然后检测文本事件。

创建事件对象

在以下命令中,用自己的值替换 <your_api_key><your_endpoint> 和其他必要参数。

以下命令可创建一个具有名称和定义的事件。

curl --location --request PATCH 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{ \"incidentName\": \"<test-incident>\", \"incidentDefinition\": \"<string>\"}'

向事件添加示例

使用以下命令向事件添加文本示例。

curl --location 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:addIncidentSamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
  \"IncidentSamples\": [
    { \"text\":    \"<text-example-1>\"},
    { \"text\":    \"<text-example-2>\"},
    ...
  ]
}'

部署事件

使用以下命令部署事件,使其可用于分析新内容。

curl --location 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:deploy?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' 

检测文本事件

运行以下命令,分析刚刚部署的事件的示例文本内容。

curl --location 'https://<endpoint>/contentsafety/text:detectIncidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
  \"text\":  \"<test-text>\",
  \"incidentNames\": [
    \"<text-incident-name>\"
  ]
}'

测试图像自定义类别(快速)API

使用本节中的示例代码创建图像事件、将示例添加到事件、部署事件,然后检测映像事件。

创建事件

在以下命令中,用自己的值替换 <your_api_key><your_endpoint> 和其他必要参数。

以下命令可创建图像事件:

curl --location --request PATCH 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
  \"incidentName\": \"<image-incident-name>\"
}'

向事件添加示例

使用以下命令向事件添加示例图像。 图像示例可以是指向 Azure Blob 存储容器中的图像的 URL,也可以是 Base64 字符串。

curl --location 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:addIncidentSamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
  \"IncidentSamples\": [
    {
      \"image\": {
        \"content\": \"<base64-data>\",
        \"bloburl\": \"<your-blob-storage-url>.png\"
      }
    }
  ]
}'

部署事件

使用以下命令部署事件,使其可用于分析新内容。

curl --location 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:deploy?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' 

检测图像事件

使用以下命令上传示例图像,并针对部署的事件对其进行测试。 可以使用指向 Azure Blob 存储容器中的图像的 URL,也可以将图像数据添加为 Base64 字符串。

curl --location 'https://<endpoint>/contentsafety/image:detectIncidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
    \"image\": {
      \"url\": \"<your-blob-storage-url>/image.png\",
      "content": "<base64-data>"
    },
    \"incidentNames\": [
      \"<image-incident-name>\"
    ]
  }
}'

其他事件操作

以下操作可用于管理事件和事件示例。

文本事件 API

列出所有事件

curl --location GET 'https://<endpoint>/contentsafety/text/incidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

获取事件详细信息

curl --location GET 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

删除事件

curl --location --request DELETE 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

列出事件下的所有示例

此命令检索与给定事件对象关联的所有示例的唯一 ID。

curl --location GET 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>/incidentsamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

获取事件示例的详细信息

使用事件示例 ID 查找有关示例的详细信息。

curl --location GET 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>/incidentsamples/<your-incident-sample-id>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

删除事件示例

使用事件示例 ID 检索并删除该示例。

curl --location 'https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:removeIncidentSamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
  \"IncidentSampleIds\": [
    \"<your-incident-sample-id>\"
  ]
}'

图像事件 API

获取事件列表

curl --location GET 'https://<endpoint>/contentsafety/image/incidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

获取事件详细信息

curl --location GET 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

删除事件

curl --location --request DELETE 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

列出事件下的所有示例

此命令检索与给定事件对象关联的所有示例的唯一 ID。

curl --location GET 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>/incidentsamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

获取事件示例详细信息

使用事件示例 ID 查找有关示例的详细信息。

curl --location GET 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>/incidentsamples/<your-incident-sample-id>?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'

删除事件示例

使用事件示例 ID 检索并删除该示例。

curl --location 'https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:removeIncidentSamples?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>' \
--header 'Content-Type: application/json' \
--data '{
  \"IncidentSampleIds\": [
    \"<your-incident-sample-id>\"
  ]
}'