azure-ai-contentsafety-py
microsoft/skills
使用 Azure AI 內容安全 SDK(Python 版),偵測文字和圖片中由使用者產生及由 AI 產生的有害內容。
...展開全部Azure AI 內容安全 SDK(Python 版)
在應用程式中偵測有害的用戶生成內容及 AI 生成內容。
安裝
pip install azure-ai-contentsafety
環境變數
CONTENT_SAFETY_ENDPOINT=https://.cognitiveservices.azure.com # 所有驗證方法皆需此設定
AZURE_TOKEN_CREDENTIALS=prod # 僅當在生產環境中使用 DefaultAzureCredential 時才需要
CONTENT_SAFETY_KEY= # 僅適用於下方的舊版 API 金鑰驗證路徑
驗證與生命週期
🔑 以下每個程式碼範例皆適用以下兩項規則:
- 優先使用
DefaultAzureCredential。它可在本地端(Azure CLI / VS Code / 開發者 CLI)及 Azure 環境(託管身分識別、工作負載身分識別)中運作,且無需修改程式碼。請避免使用連線字串、帳戶/API 金鑰——這些會繞過 Entra 的稽核與輪替機制。
- 本地開發:
DefaultAzureCredential可直接使用。- 生產環境:請設定
AZURE_TOKEN_CREDENTIALS=prod(或AZURE_TOKEN_CREDENTIALS=),以將憑證鏈限制為符合生產環境安全標準的憑證。- 將每個客戶端封裝在上下文管理器中,以便 HTTP 傳輸、套接字和憑證快取能以確定性方式釋放:
- 同步模式:
使用 `(...)` 作為 `client`: - 非同步:
使用 `,(...)` 作為 `client` 的 `async` 以及使用 `DefaultAzureCredential()` 作為 `credential` 的 `async`(來自azure.identity.aio)程式碼片段可能會簡化此設定,但生產環境的程式碼應始終遵循這兩項規則。
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions
# 本地開發環境:DefaultAzureCredential。生產環境:設定 AZURE_TOKEN_CREDENTIALS=prod 或 AZURE_TOKEN_CREDENTIALS=
credential = DefaultAzureCredential(require_envvar=True)
# 或者在生產環境中直接使用特定的憑證:
# 參見 https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
with ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=credential,
) as client:
response = client.analyze_text(AnalyzeTextOptions(text="Hello, world!"))
舊版:API 金鑰(現有的基於金鑰的部署)
新程式碼應使用上方的DefaultAzureCredential。僅當您擁有尚未遷移至 Entra ID 的既有金鑰部署時,才應使用AzureKeyCredential—— 例如,仍在完成 Entra 部署的受監管環境。
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions
with ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=AzureKeyCredential(os.environ["CONTENT_SAFETY_KEY"]),
) as client:
response = client.analyze_text(AnalyzeTextOptions(text="Hello, world!"))
若您也需要使用金鑰來管理封鎖清單,BlocklistClient亦可接受相同的AzureKeyCredential。
分析文字
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.identity import DefaultAzureCredential
with ContentSafetyClient(endpoint, DefaultAzureCredential()) as client:
request = AnalyzeTextOptions(text="待分析的文字內容")
response = client.analyze_text(request)
# 檢查每個類別
for category in [TextCategory.HATE, TextCategory.SELF_HARM,
TextCategory.SEXUAL, TextCategory.VIOLENCE]:
result = next((r for r in response.categories_analysis
if r.category == category), None)
if result:
print(f"{category}: 嚴重程度 {result.severity}")
分析圖片
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
from azure.identity import DefaultAzureCredential
import base64
with ContentSafetyClient(endpoint, DefaultAzureCredential()) as client:
# 從檔案讀取
with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
request = AnalyzeImageOptions(
image=ImageData(content=image_data)
)
response = client.analyze_image(request)
for result in response.categories_analysis:
print(f"{result.category}: 嚴重性 {result.severity}")
來自 URL 的圖片
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
request = AnalyzeImageOptions(
image=ImageData(blob_url="https://example.com/image.jpg")
)
response = client.analyze_image(request)
文字黑名單管理
建立封鎖清單
from azure.ai.contentsafety import BlocklistClient
from azure.ai.contentsafety.models import TextBlocklist
from azure.identity import DefaultAzureCredential
with BlocklistClient(endpoint, DefaultAzureCredential()) as blocklist_client:
blocklist = TextBlocklist(
blocklist_name="my-blocklist",
description="要封鎖的自訂詞彙"
)
result = blocklist_client.create_or_update_text_blocklist(
blocklist_name="my-blocklist",
options=blocklist
)
新增封鎖項目
from azure.ai.contentsafety.models import AddOrUpdateTextBlocklistItemsOptions, TextBlocklistItem
items = AddOrUpdateTextBlocklistItemsOptions(
blocklist_items=[
TextBlocklistItem(text="blocked-term-1"),
TextBlocklistItem(text="blocked-term-2")
]
)
result = blocklist_client.add_or_update_blocklist_items(
blocklist_name="my-blocklist",
options=items
)
使用封鎖清單進行分析
from azure.ai.contentsafety.models import AnalyzeTextOptions
request = AnalyzeTextOptions(
text="包含 blocked-term-1 的文字",
blocklist_names=["my-blocklist"],
halt_on_blocklist_hit=True
)
response = client.analyze_text(request)
if response.blocklists_match:
for match in response.blocklists_match:
print(f"遭封鎖:{match.blocklist_item_text}")
嚴重性等級
文字分析預設會返回 4 個嚴重性等級(0、2、4、6)。若要設定 8 個等級(0-7):
from azure.ai.contentsafety.models import AnalyzeTextOptions, AnalyzeTextOutputType
request = AnalyzeTextOptions(
text="您的文字",
output_type=AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS
)
危害類別
| 類別 | 描述 |
|---|---|
仇恨 |
基於身分(種族、宗教、性別等)的攻擊 |
性 |
性內容、性關係、人體解剖結構 |
暴力 |
身體傷害、武器、受傷 |
自殘 |
自殘、自殺、進食障礙 |
嚴重程度量表
| 等級 | 文字範圍 | 圖片範圍 | 含義 |
|---|---|---|---|
| 0 | 安全 | 安全 | 無有害內容 |
| 2 | 低 | 低 | 輕微提及 |
| 4 | 中等 | 中等 | 內容程度中等 |
| 6 | 高 | 高 | 嚴重的內容 |
客戶類型
| 客戶 | 目的 |
|---|---|
內容安全客戶端 |
分析文字與圖片 |
阻擋清單用戶端 |
管理自訂封鎖清單 |
最佳實務
- 請選擇「同步」或「非同步」模式,並保持一致。請勿在同一呼叫路徑中混合使用
azure.ai.contentsafety的同步客戶端與azure.ai.contentsafety.aio的非同步客戶端。每個模組應選擇一種模式。 - 請務必為客戶端和非同步憑證使用上下文管理器。將每個客戶端
以 ContentSafetyClient(...) as client:(sync)(同步模式)或ContentSafetyClient(...) as client:(async)(非同步模式)進行封裝。 對於來自azure.identity.aio的非同步DefaultAzureCredential,也請搭配 async 模式並使用credential:,以確保代幣和傳輸資料能被妥善清理。 - 針對特定領域的術語,請使用封鎖清單
- 根據您的使用情境設定適當的嚴重性閾值
- 處理多種類別— 內容可能以多種方式造成危害
- 使用 halt_on_blocklist_hit進行即時拒絕
- 記錄分析結果以供稽核與改進
- 考慮採用 8 級嚴重性模式以實現更細緻的控制
- 在向使用者顯示前,先對AI 產出進行預先審核
---
name: azure-ai-contentsafety-py
description: Detect harmful user-generated and AI-generated content in text and images using Azure AI Content Safety SDK for Python.
license: MIT
---
# Azure AI Content Safety SDK for Python
Detect harmful user-generated and AI-generated content in applications.
## Installation
```bash
pip install azure-ai-contentsafety
```
## Environment Variables
```bash
CONTENT_SAFETY_ENDPOINT=https://<resource>.cognitiveservices.azure.com # Required for all auth methods
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
CONTENT_SAFETY_KEY=<your-api-key> # Only required for the legacy API-key auth path below
```
## Authentication & Lifecycle
> **🔑 Two rules apply to every code sample below:**
>
> 1. **Prefer `DefaultAzureCredential`.** It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
> - Local dev: `DefaultAzureCredential` works as-is.
> - Production: set `AZURE_TOKEN_CREDENTIALS=prod` (or `AZURE_TOKEN_CREDENTIALS=<specific_credential>`) to constrain the credential chain to production-safe credentials.
> 2. **Wrap every client in a context manager** so HTTP transports, sockets, and token caches are released deterministically:
> - Sync: `with <Client>(...) as client:`
> - Async: `async with <Client>(...) as client:` **and** `async with DefaultAzureCredential() as credential:` (from `azure.identity.aio`)
>
> Snippets may abbreviate this setup, but production code should always follow both rules.
```python
import os
from azure.identity import DefaultAzureCredential, ManagedIdentityCredential
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions
# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
with ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=credential,
) as client:
response = client.analyze_text(AnalyzeTextOptions(text="Hello, world!"))
```
### Legacy: API Key (existing keyed deployments)
New code should use `DefaultAzureCredential` above. Use `AzureKeyCredential` only if you have an existing keyed deployment that hasn't been migrated to Entra ID yet — for example, regulated environments still completing their Entra rollout.
```python
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions
with ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=AzureKeyCredential(os.environ["CONTENT_SAFETY_KEY"]),
) as client:
response = client.analyze_text(AnalyzeTextOptions(text="Hello, world!"))
```
The `BlocklistClient` accepts the same `AzureKeyCredential` if you also need to manage blocklists with a key.
## Analyze Text
```python
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.identity import DefaultAzureCredential
with ContentSafetyClient(endpoint, DefaultAzureCredential()) as client:
request = AnalyzeTextOptions(text="Your text content to analyze")
response = client.analyze_text(request)
# Check each category
for category in [TextCategory.HATE, TextCategory.SELF_HARM,
TextCategory.SEXUAL, TextCategory.VIOLENCE]:
result = next((r for r in response.categories_analysis
if r.category == category), None)
if result:
print(f"{category}: severity {result.severity}")
```
## Analyze Image
```python
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
from azure.identity import DefaultAzureCredential
import base64
with ContentSafetyClient(endpoint, DefaultAzureCredential()) as client:
# From file
with open("image.jpg", "rb") as f:
image_data = base64.b64encode(f.read()).decode("utf-8")
request = AnalyzeImageOptions(
image=ImageData(content=image_data)
)
response = client.analyze_image(request)
for result in response.categories_analysis:
print(f"{result.category}: severity {result.severity}")
```
### Image from URL
```python
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
request = AnalyzeImageOptions(
image=ImageData(blob_url="https://example.com/image.jpg")
)
response = client.analyze_image(request)
```
## Text Blocklist Management
### Create Blocklist
```python
from azure.ai.contentsafety import BlocklistClient
from azure.ai.contentsafety.models import TextBlocklist
from azure.identity import DefaultAzureCredential
with BlocklistClient(endpoint, DefaultAzureCredential()) as blocklist_client:
blocklist = TextBlocklist(
blocklist_name="my-blocklist",
description="Custom terms to block"
)
result = blocklist_client.create_or_update_text_blocklist(
blocklist_name="my-blocklist",
options=blocklist
)
```
### Add Block Items
```python
from azure.ai.contentsafety.models import AddOrUpdateTextBlocklistItemsOptions, TextBlocklistItem
items = AddOrUpdateTextBlocklistItemsOptions(
blocklist_items=[
TextBlocklistItem(text="blocked-term-1"),
TextBlocklistItem(text="blocked-term-2")
]
)
result = blocklist_client.add_or_update_blocklist_items(
blocklist_name="my-blocklist",
options=items
)
```
### Analyze with Blocklist
```python
from azure.ai.contentsafety.models import AnalyzeTextOptions
request = AnalyzeTextOptions(
text="Text containing blocked-term-1",
blocklist_names=["my-blocklist"],
halt_on_blocklist_hit=True
)
response = client.analyze_text(request)
if response.blocklists_match:
for match in response.blocklists_match:
print(f"Blocked: {match.blocklist_item_text}")
```
## Severity Levels
Text analysis returns 4 severity levels (0, 2, 4, 6) by default. For 8 levels (0-7):
```python
from azure.ai.contentsafety.models import AnalyzeTextOptions, AnalyzeTextOutputType
request = AnalyzeTextOptions(
text="Your text",
output_type=AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS
)
```
## Harm Categories
| Category | Description |
|----------|-------------|
| `Hate` | Attacks based on identity (race, religion, gender, etc.) |
| `Sexual` | Sexual content, relationships, anatomy |
| `Violence` | Physical harm, weapons, injury |
| `SelfHarm` | Self-injury, suicide, eating disorders |
## Severity Scale
| Level | Text Range | Image Range | Meaning |
|-------|------------|-------------|---------|
| 0 | Safe | Safe | No harmful content |
| 2 | Low | Low | Mild references |
| 4 | Medium | Medium | Moderate content |
| 6 | High | High | Severe content |
## Client Types
| Client | Purpose |
|--------|---------|
| `ContentSafetyClient` | Analyze text and images |
| `BlocklistClient` | Manage custom blocklists |
## Best Practices
1. **Pick sync OR async and stay consistent.** Do not mix `azure.ai.contentsafety` sync clients with `azure.ai.contentsafety.aio` async clients in the same call path. Choose one mode per module.
2. **Always use context managers for clients and async credentials.** Wrap every client in `with ContentSafetyClient(...) as client:` (sync) or `async with ContentSafetyClient(...) as client:` (async). For async `DefaultAzureCredential` from `azure.identity.aio`, also use `async with credential:` so tokens and transports are cleaned up.
3. **Use blocklists** for domain-specific terms
4. **Set severity thresholds** appropriate for your use case
5. **Handle multiple categories** — content can be harmful in multiple ways
6. **Use halt_on_blocklist_hit** for immediate rejection
7. **Log analysis results** for audit and improvement
8. **Consider 8-severity mode** for finer-grained control
9. **Pre-moderate AI outputs** before showing to users
所有檔案
0 個檔案安裝 azure-ai-contentsafety-py
請下載並將技能檔案解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/azure-ai-contentsafety-py # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
