Azure AI 内容安全 Python SDK
Azure AI 内容安全 Python SDK
在应用程序中检测用户生成和 AI 生成的有害内容。
安装
pip install azure-ai-contentsafety环境变量
CONTENT_SAFETY_ENDPOINT=https://<resource>.cognitiveservices.azure.com
CONTENT_SAFETY_KEY=<your-api-key>身份验证
API 密钥
from azure.ai.contentsafety import ContentSafetyClient
from azure.core.credentials import AzureKeyCredential
import os
client = ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=AzureKeyCredential(os.environ["CONTENT_SAFETY_KEY"])
)
Entra ID
from azure.ai.contentsafety import ContentSafetyClient
from azure.identity import DefaultAzureCredential
client = ContentSafetyClient(
endpoint=os.environ["CONTENT_SAFETY_ENDPOINT"],
credential=DefaultAzureCredential()
)
分析文本
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.core.credentials import AzureKeyCredential
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
request = AnalyzeTextOptions(text="Your text content to analyze")
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}: severity {result.severity}")分析图像
from azure.ai.contentsafety import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeImageOptions, ImageData
from azure.core.credentials import AzureKeyCredential
import base64
client = ContentSafetyClient(endpoint, AzureKeyCredential(key))
从文件读取
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}")
通过 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.core.credentials import AzureKeyCredential
blocklist_client = BlocklistClient(endpoint, AzureKeyCredential(key))
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
)
添加阻止项
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="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}")
## 严重程度级别
文本分析默认返回 4 个严重程度级别 (0, 2, 4, 6)。如需 8 个级别 (0-7):
from azure.ai.contentsafety.models import AnalyzeTextOptions, AnalyzeTextOutputType
request = AnalyzeTextOptions(
text="Your text",
output_type=AnalyzeTextOutputType.EIGHT_SEVERITY_LEVELS
)
``
危害类别
| 类别 | 描述 |
|----------|-------------|
| Hate | 基于身份(种族、宗教、性别等)的攻击 |Sexual
| | 性内容、关系、解剖结构 |Violence
| | 身体伤害、武器、受伤 |SelfHarm
| | 自残、自杀、饮食失调 |
严重程度量表
| 级别 | 文本范围 | 图像范围 | 含义 |
|-------|------------|-------------|---------|
| 0 | 安全 | 安全 | 无有害内容 |
| 2 | 低 | 低 | 轻微提及 |
| 4 | 中 | 中 | 中度内容 |
| 6 | 高 | 高 | 严重内容 |
客户端类型
| 客户端 | 用途 |
|--------|---------|
| ContentSafetyClient | 分析文本和图像 |BlocklistClient` | 管理自定义阻止列表 |
|
最佳实践
1. 使用阻止列表处理特定领域的术语
2. 设置适合于具体用例的严重程度阈值
3. 处理多个类别 —— 内容可能在多个维度上具有危害性
4. 使用 halt_on_blocklist_hit 实现立即拒绝
5. 记录分析结果以便审计和改进
6. 考虑使用 8 级严重程度模式以获得更精细的控制
7. 在向用户展示前对 AI 输出进行预审核
适用场景
此技能适用于执行概述中描述的工作流或操作。局限性
- 仅在任务明确符合上述范围时使用此技能。
- 不要将输出视为环境特定验证、测试或专家评审的替代方案。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。