azure-ai-contentsafety-py – skill Viết lách & Nội dung cho agent AI
SDK Azure AI Content Safety cho Python. Dùng để phát hiện nội dung có hại trong văn bản và hình ảnh với phân loại nhiều mức độ nghiêm trọng.
Cài skill azure-ai-contentsafety-py bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-ai-contentsafety-py-1cf17.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /azure-ai-contentsafety-py – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-ai-contentsafety-py-1cf17.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $azure-ai-contentsafety-py – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-ai-contentsafety-py-1cf17.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /azure-ai-contentsafety-py – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/azure-ai-contentsafety-py-1cf17.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /azure-ai-contentsafety-py – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Azure AI Content Safety SDK for Python
Detect harmful user-generated and AI-generated content in applications.
Installation
pip install azure-ai-contentsafetyEnvironment Variables
CONTENT_SAFETY_ENDPOINT=https://<resource>.cognitiveservices.azure.com
CONTENT_SAFETY_KEY=<your-api-key>Authentication
API Key
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()
)Use only an approved endpoint/account, content and cost/retention policy; uploads disclose text/images to a hosted classifier. Bound input bytes and use reviewed SDK models/imports (azure-identity is additionally needed for Entra ID). Classification can miss harm or flag benign context: severity zero is not a universal safety guarantee. Validate thresholds on representative languages/domains, define outage/ambiguous-result handling and human appeal/review. Blocklist mutations can affect real moderation and require explicit scope; redact content in logs and close clients/credentials.
Analyze Text
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}")Analyze Image
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}")Image from 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)Text Blocklist Management
Create Blocklist
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
)Add Block Items
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
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):
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 | Not detected | Not detected | Model did not detect harm in supported categories |
| 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. Use blocklists for domain-specific terms
2. Set severity thresholds appropriate for your use case
3. Handle multiple categories — content can be harmful in multiple ways
4. Use halt_on_blocklist_hit for immediate rejection
5. Log analysis results for audit and improvement
6. Consider 8-severity mode for finer-grained control
7. Pre-moderate AI outputs before showing to users
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Skill liên quan
- azure-ai-contentsafety-ts – Phân tích văn bản và hình ảnh để phát hiện nội dung có hại, với danh sách chặn…
- azure-ai-contentunderstanding-py – SDK Azure AI Content Understanding cho Python. Dùng để trích xuất nội dung đa phương…
- azure-ai-translation-text-py – SDK Azure AI Text Translation để dịch văn bản thời gian thực, phiên âm…
- azure-ai-translation-ts – Dịch văn bản và tài liệu bằng client kiểu REST.
- content-creator – Skill chuyển hướng đã lỗi thời, định tuyến các yêu cầu 'content creator' cũ đến chuyên…
- content-humanizer – Làm cho nội dung do AI tạo ra nghe thật sự giống người — không chỉ được chỉnh sửa gọn…
- content-production – Quy trình sản xuất nội dung đầy đủ — đi từ trang trắng đến bài viết sẵn sàng xuất bản…
- content-strategy – Khi người dùng muốn lập chiến lược nội dung, quyết định nên tạo nội dung gì hoặc chọn…