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 / Developer CLI)使用,也可在 Azure(托管身份、工作负载身份)中使用,且无需修改代码。请避免使用连接字符串、账户/API 密钥——它们会绕过 Entra 审计和轮换机制。
- 本地开发:
DefaultAzureCredential可直接使用。- 生产环境:将
AZURE_TOKEN_CREDENTIALS设置为prod(或AZURE_TOKEN_CREDENTIALS=),以将凭据链限制为生产环境安全的凭据。- 将每个客户端封装在上下文管理器中,以确保 HTTP 传输、套接字和令牌缓存能以确定性方式释放:
- 同步模式:
使用 ``(...) as client: - 异步:
async with和(...) as client: async with DefaultAzureCredential() as credential:(来自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,也请使用异步模式并指定 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
复制





首页
