メインコンテンツまでスキップ

セキュリティガイド

SDK の Security モジュールを使って、エージェントの入出力に対するコンテンツモデレーション、プロンプトシールド、PII(個人情報)検出を実装する方法を解説します。

概要

Security モジュールは 3 つのクラウドプロバイダーに対応したコンテンツ安全性チェック機能を提供します。

プロバイダーコンテンツモデレーションプロンプトシールドPII 検出
AzureAzure AI Content SafetyPrompt Shield APIPII Detection API
AWSBedrock GuardrailsBedrock GuardrailsAmazon Comprehend
GCPModel ArmorModel ArmorCloud DLP
ユーザー入力


┌────────────────────────┐
│ SecurityClientBase │ ← check_security() で一括実行
└────────┬───────────────┘

┌────┴────┐
▼ ▼
コンテンツ PII
モデレーション 検出
│ │
└────┬────┘

allowed / blocked 判定


エージェント処理 or ブロック

プロバイダーの設定

config.tomlToml
[security]
provider = "azure"
content_safety_endpoint = "https://your-content-safety.cognitiveservices.azure.com/"
content_safety_api_key = "${AZURE_CONTENT_SAFETY_KEY}"
language_endpoint = "https://your-language.cognitiveservices.azure.com/"
language_api_key = "${AZURE_LANGUAGE_KEY}"
moderation_threshold = 2
pii_confidence_threshold = 0.7
Python
from agenticstar_platform.security import AzureSecurityClient, AzureSecurityConfig

config = AzureSecurityConfig(
content_safety_endpoint="https://your-content-safety.cognitiveservices.azure.com/",
content_safety_api_key="your-api-key",
language_endpoint="https://your-language.cognitiveservices.azure.com/",
language_api_key="your-language-key",
)
client = AzureSecurityClient(config)

自動検出ファクトリ

設定辞書からプロバイダーを自動検出してクライアントを生成できます。

Python
from agenticstar_platform.security import create_security_client

# 設定辞書からプロバイダーを自動検出
client = create_security_client(config_dict)

# PII 検出専用クライアントを生成
from agenticstar_platform.security import create_pii_client
pii_client = create_pii_client(config_dict)

コンテンツモデレーション

ユーザー入力やエージェント出力に対して、有害コンテンツの検出を行います。

Python
result = await client.check_content_moderation(
text="チェック対象のテキスト",
threshold=2, # severity 0-6、このしきい値以上でブロック
)

print(result.blocked) # True(ブロック)/ False(許可)
print(result.categories) # {ContentCategory.HATE: 0, ContentCategory.VIOLENCE: 4, ...}
print(result.threshold) # 適用されたしきい値

検出カテゴリと重大度

ContentCategory説明重大度 (0-6)
HATEヘイトスピーチ・差別0=安全, 2=低, 4=中, 6=高
SEXUAL性的コンテンツ同上
SELF_HARM自傷行為同上
VIOLENCE暴力的コンテンツ同上
PROFANITY不適切な表現同上
INSULT侮辱的表現同上
THREAT脅迫的表現同上

プロンプトシールド

プロンプトインジェクション攻撃を検出します。

Python
result = await client.check_prompt_shield(
user_prompt="ユーザーの入力テキスト",
documents=["参照ドキュメント1", "参照ドキュメント2"],
)

print(result.attack_detected) # True / False
print(result.attack_type) # 攻撃タイプ(検出時)
print(result.confidence) # 信頼度スコア

check_prompt_shield() は各ドキュメントを 10,000 文字に自動トリミングします(Azure API の制限対応)。

PII 検出

テキストから個人情報を検出します。日本語の PII(電話番号、メールアドレス等)にも正規表現で対応しています。

Python
result = await client.detect_pii(
text="田中太郎の電話番号は090-1234-5678です。メールはtanaka@example.comです。",
mask=True,
language="ja",
)

print(result.success) # True
print(result.masked_text) # "田中太郎の電話番号は***です。メールは***です。"
print(result.categories_detected) # [PIICategory.PHONE_NUMBER, PIICategory.EMAIL]
for entity in result.entities:
print(f" {entity.category}: {entity.text} (offset={entity.offset}, len={entity.length}, confidence={entity.confidence})")

PII カテゴリ(主要なもの)

PIICategory検出対象の例
PERSON_NAME田中太郎
PHONE_NUMBER090-1234-5678, 03-1234-5678
EMAILuser@example.com
CREDIT_CARD4111-1111-1111-1111
NATIONAL_IDマイナンバー(12桁)
PASSPORT_NUMBERTK1234567
ADDRESS東京都渋谷区...
IP_ADDRESS192.168.1.1
API_KEYsk-...
CONNECTION_STRINGDefaultEndpointsProtocol=...

統合セキュリティチェック

check_security() メソッドで、コンテンツモデレーション・プロンプトシールド・PII 検出をまとめて実行できます。

Python
result = await client.check_security(
text="チェック対象のテキスト",
check_moderation=True,
check_prompt_shield=True,
check_pii=False, # デフォルトは False
fail_on_error=True,
)

print(result.allowed) # True(許可)/ False(ブロック)
print(result.violations) # ["content_moderation: violence detected", ...]
print(result.content_moderation) # ContentModerationResult or None
print(result.prompt_shield) # PromptShieldResult or None
print(result.pii_detection) # PIIDetectionResult or None

ContentSafetyValidator

ContentSafetyValidator は、ガードレール設定に基づいてコンテンツの安全性を一括検証するラッパークラスです。

Python
from agenticstar_platform.security import ContentSafetyValidator, GuardrailsConfig

validator = ContentSafetyValidator(
security_client=client,
guardrails=guardrails_config, # DB から取得したガードレール設定
)

# コンテンツの検証
result = await validator.validate(user_message)
print(result.allowed)
print(result.violations)

エージェントでの活用パターン

入出力両方のチェック

Python
async def safe_agent_run(user_message: str) -> str:
# 1. 入力チェック
input_result = await client.check_security(text=user_message)
if not input_result.allowed:
return f"リクエストがブロックされました: {', '.join(input_result.violations)}"

# 2. エージェント処理
response = await run_agent_logic(user_message)

# 3. 出力チェック
output_result = await client.check_security(text=response)
if not output_result.allowed:
return "回答の生成中にコンテンツポリシー違反が検出されました。"

return response

次のステップ

SDK API リファレンス — Security Module

SecurityClient / ContentSafetyValidator の完全仕様

ガイドを見る

メモリガイド

メモリに保存する前のコンテンツ検証

ガイドを見る