カスタム カテゴリ (急速) API を使用する

カスタム カテゴリ (急速) API を使用すると、新たに発生する有害なコンテンツ インシデントに迅速に対応できます。 特定のトピック内でいくつかの例を使用してインシデントを定義すると、このサービスによって同様のコンテンツの検出が開始されます。

次の手順に従って、いくつかのテキスト コンテンツの例を含むインシデントを定義してから、新しいテキスト コンテンツを分析して、それが当該インシデントと一致するかどうかを確認します。

重要

この新機能は、一部の Azure リージョンでのみ利用可能です。 利用可能なリージョンに関するページを参照してください。

注意事項

このガイドのサンプル データには、不快なコンテンツが含まれている可能性があります。 ユーザーの裁量が推奨されます。

前提条件

  • Azure サブスクリプション - 無料アカウントを作成します
  • Azure サブスクリプションを入手したら、Azure portal で Content Safety リソースを作成し、キーとエンドポイントを取得します。 リソースの一意の名前を入力し、サブスクリプションを選択して、リソース グループ、サポートされているリージョン (「利用可能なリージョン」を参照)、サポートされている価格レベルを選択します。 [作成] を選択します。
    • リソースのデプロイには数分かかります。 完了したら、[リソースに移動] を選びます。 左ペインの [リソース管理] で、[サブスクリプション キーとエンドポイント] を選びます。 エンドポイントといずれかのキーが、API の呼び出しに使われます。
  • また画像をアップロードしたい場合は、BLOB ストレージ コンテナーを作成します。 あるいは、画像を Base64 文字列としてエンコードし、それを API 呼び出し内で直接使用することもできます。
  • 次のいずれかがインストールされていること。
    • REST API 呼び出し用の cURL
    • Python 3.x がインストールされていること

テキスト カスタム カテゴリ (急速) 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>\"
  ]
}'