使用自訂類別 (快速) 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>\"}'
首先,您需要安裝必要的 Python 連結庫:
pip install requests
然後,使用您自己的Azure資源詳細資料來定義必要的變數:
import requests
API_KEY = '<your_api_key>'
ENDPOINT = '<your_endpoint>'
headers = {
'Ocp-Apim-Subscription-Key': API_KEY,
'Content-Type': 'application/json'
}
下列命令會建立具有名稱和定義的事件。
import requests
import json
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview"
payload = json.dumps({
"incidentName": "<text-incident-name>",
"incidentDefinition": "string"
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("PATCH", url, headers=headers, data=payload)
print(response.text)
將範例新增至事件
使用下列命令,將文字範例新增至事件。
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>\"},
...
]
}'
import requests
import json
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:addIncidentSamples?api-version=2024-02-15-preview"
payload = json.dumps({
"IncidentSamples": [
{
"text": "<text-example-1>"
},
{
"text": "<text-example-1>"
},
...
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
部署事件
使用下列命令來部署事件,使其可供分析新內容。
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'
import requests
import json
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:deploy?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
偵測文字事件
執行下列命令來分析您剛部署的事件所出現的範例文字內容。
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>\"
]
}'
import requests
import json
url = "https://<endpoint>/contentsafety/text:detectIncidents?api-version=2024-02-15-preview"
payload = json.dumps({
"text": "<test-text>",
"incidentNames": [
"<text-incident-name>"
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
測試影像自訂類別 (快速) 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>\"
}'
請確定您已安裝必要的 Python 連結庫:
pip install requests
使用您自己的 Azure 資源詳細資料定義必要的變數:
import requests
API_KEY = '<your_api_key>'
ENDPOINT = '<your_endpoint>'
headers = {
'Ocp-Apim-Subscription-Key': API_KEY,
'Content-Type': 'application/json'
}
下列命令會在建立影像事件:
import requests
import json
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview"
payload = json.dumps({
"incidentName": "<image-incident-name>"
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("PATCH", url, headers=headers, data=payload)
print(response.text)
將範例新增至事件
使用下列命令,將範例影像新增至您的事件。 映像範例可以是指向 AzureBlob 記憶體容器中影像的 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\"
}
}
]
}'
import requests
import json
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:addIncidentSamples?api-version=2024-02-15-preview"
payload = json.dumps({
"IncidentSamples": [
{
"image": {
"content": "<base64-data>",
"bloburl": "<your-blob-storage-url>/image.png"
}
}
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
部署事件
使用下列命令來部署事件,使其可供分析新內容。
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'
import requests
import json
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:deploy?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
偵測影像事件
使用下列命令來上傳範例影像,並針對您所部署的事件進行測試。 您可以使用指向 AzureBlob 記憶體容器中映像的 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>\"
]
}
}'
import requests
import json
url = "https://<endpoint>/contentsafety/image:detectIncidents?api-version=2024-02-15-preview"
payload = json.dumps({
"image": {
"url": "<your-blob-storage-url>/image.png",
"content": "<base64-data>"
},
"incidentNames": [
"<image-incident-name>"
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
其他事件作業
下列作業適用於管理事件和事件範例。
文字事件 API
列出全部事件
curl --location GET 'https://<endpoint>/contentsafety/text/incidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'
import requests
url = "https://<endpoint>/contentsafety/text/incidents?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
取得事件詳細資料
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>'
import requests
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
刪除事件
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>'
import requests
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("DELETE", url, headers=headers, data=payload)
print(response.text)
列出事件下的全部範例
此命令會擷取與指定事件對象相關聯的全部範例所用的唯一識別碼。
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>'
import requests
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>/incidentsamples?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
取得事件範例的詳細資料
使用事件範例識別碼來查閱範例的詳細資料。
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>'
import requests
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>/incidentsamples/<your-incident-sample-id>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
刪除事件範例
使用事件範例識別碼來擷取和刪除該範例。
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>\"
]
}'
import requests
import json
url = "https://<endpoint>/contentsafety/text/incidents/<text-incident-name>:removeIncidentSamples?api-version=2024-02-15-preview"
payload = json.dumps({
"IncidentSampleIds": [
"<your-incident-sample-id>"
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
影像事件 API
取得事件清單
curl --location GET 'https://<endpoint>/contentsafety/image/incidents?api-version=2024-02-15-preview' \
--header 'Ocp-Apim-Subscription-Key: <your-content-safety-key>'
import requests
url = "https://<endpoint>/contentsafety/image/incidents?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
取得事件詳細資料
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>'
import requests
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
刪除事件
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>'
import requests
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("DELETE", url, headers=headers, data=payload)
print(response.text)
列出事件下的全部範例
此命令會擷取與指定事件對象相關聯的全部範例所用的唯一識別碼。
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>'
import requests
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>/incidentsamples?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
取得事件範例詳細資料
使用事件範例識別碼來查閱範例的詳細資料。
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>'
import requests
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>/incidentsamples/<your-incident-sample-id>?api-version=2024-02-15-preview"
payload = {}
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
刪除事件範例
使用事件範例識別碼來擷取和刪除該範例。
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>\"
]
}'
import requests
import json
url = "https://<endpoint>/contentsafety/image/incidents/<image-incident-name>:removeIncidentSamples?api-version=2024-02-15-preview"
payload = json.dumps({
"IncidentSampleIds": [
"<your-incident-sample-id>"
]
})
headers = {
'Ocp-Apim-Subscription-Key': '<your-content-safety-key>',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
相關內容