mcpField
A
azure-ai-contentsafety-py

azure-ai-contentsafety-py

github.com/sickn33其他48.7k77 次安装今天更新
代码仓库

关于 azure-ai-contentsafety-py

供 Python 应用使用的 Azure AI Content Safety SDK,用于分析用户生成或 AI 生成的文本与图像,并管理文本屏蔽列表。适合需要在内容审核流程中调用托管分类服务的开发者。

主要功能

  1. 01分析文本中的有害内容
  2. 02分析图像内容
  3. 03管理自定义文本屏蔽列表
  4. 04配置严重程度级别
  5. 05支持 API 密钥和 Entra ID 身份验证

适用场景

  1. 01审核用户提交的文本
  2. 02检查应用展示的图像
  3. 03按领域词项拦截文本
  4. 04在展示前筛查 AI 输出

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-ai-contentsafety-py

安装后在 Claude Code 中输入/azure-ai-contentsafety-py确认已加载。

详细介绍

Azure AI Content Safety SDK for Python

在应用中检测用户生成和 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()
)

仅使用经过批准的终结点/账户,并遵守内容、费用和保留策略;上传内容会将文本/图像披露给托管分类器。限制输入字节数,并使用经过审核的 SDK 模型/导入项(使用 Entra ID 还需要 azure-identity)。分类可能漏掉有害内容,也可能将良性语境标记为有害:严重性为零并不代表普遍安全。请使用有代表性的语言/领域验证阈值,明确处理服务中断/结果含糊的方式,并提供人工申诉/审核流程。修改屏蔽列表可能影响实际内容审核,必须明确操作范围;日志中应对内容进行脱敏,并关闭客户端/凭据。

分析文本

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)

# 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}")

分析图像

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))

# From file
with open("image.jpg", "rb") as f:
    image_data = f.read()  # SDK serializes bytes to the REST base64 representation

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 输出之前进行预先审核

适用场景

当任务涉及执行概述中描述的工作流或操作时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审核的替代品。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。

评论

支持 Markdown