アプリケーションで ApplyGuardrail API を使用する - Amazon Bedrock

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

アプリケーションで ApplyGuardrail API を使用する

ガードレールを使用して、責任ある AI ポリシーに則って、ユースケースに合わせてカスタマイズした保護対策を生成 AI アプリケーションに実装できます。ガードレールでは、拒否対象のトピックを設定し、有害なコンテンツをフィルターで除外して、機密情報を削除することができます。

ApplyGuardrail API を使用して、基盤モデルを呼び出すことなく、事前設定された Amazon Bedrock ガードレールを使用してテキストを評価できます。

ApplyGuardrail API の機能:

  • コンテンツ検証 – 任意のテキスト入力または出力を ApplyGuardrail API に送信して、定義済みのトピック回避ルール、コンテンツフィルター、PII ディテクター、単語ブロックリストと比較照合できます。ユーザーの入力と、FM が生成した出力を個別に評価できます。

  • 柔軟なデプロイ - アプリケーションフローの任意の位置に ApplyGuardrail API を統合して、結果を処理またはユーザーに提示する前にデータを検証できます。例えば、RAG アプリケーションを使用している場合、最終レスポンスが生成されるまで待たずに、検索の実行前にユーザー入力を評価することができます。

  • FM からの分離 – ApplyGuardrail API は基盤モデルから分離されています。基盤モデルを呼び出すことなく、ガードレールを使用できます。評価結果を使用して、生成 AI アプリケーションのエクスペリエンスを設計できます。

アプリケーションフローで ApplyGuardrail API を呼び出す

このリクエストを使用して、定義済みのガードレールで保護すべきコンテンツをすべて渡すことができます。評価対象のコンテンツの提供元がユーザーである場合は (通常は LLM プロンプト)、source フィールドを「INPUT」に設定する必要があります。モデル出力のガードレールを適用する必要がある場合 (通常は LLM レスポンス)、source は「OUTPUT」に設定する必要があります。

トピック

    ApplyGuardrail API で使用するガードレールを設定する

    guardrailConfig 入力パラメータでガードレールの設定情報を指定します。設定情報には、使用するガードレールの ID やバージョンなどが該当します。また、ガードレールのトレースを有効にして、ガードレールがブロックしたコンテンツに関する情報を得ることもできます。

    ApplyGuardrail API Request
    POST /guardrail/{guardrailIdentifier}/version/{guardrailVersion}/apply HTTP/1.1 { "source": "INPUT" | "OUTPUT", "content": [ { "text": { "text": "string", } }, ] }
    ApplyGuardrail API Response
    { "usage": { "topicPolicyUnits": "integer", "contentPolicyUnits": "integer", "wordPolicyUnits": "integer", "sensitiveInformationPolicyUnits": "integer", "sensitiveInformationPolicyFreeUnits": "integer", "contextualGroundingPolicyUnits": "integer" }, "action": "GUARDRAIL_INTERVENED" | "NONE", "output": [ // if guardrail intervened and output is masked we return request in same format // with masking // if guardrail intervened and blocked, output is a single text with canned message // if guardrail did not intervene, output is empty array { "text": "string", }, ], "assessments": [{ "topicPolicy": { "topics": [{ "name": "string", "type": "DENY", "action": "BLOCKED", }] }, "contentPolicy": { "filters": [{ "type": "INSULTS | HATE | SEXUAL | VIOLENCE | MISCONDUCT |PROMPT_ATTACK", "confidence": "NONE" | "LOW" | "MEDIUM" | "HIGH", "filterStrength": "NONE" | "LOW" | "MEDIUM" | "HIGH", "action": "BLOCKED" }] }, "wordPolicy": { "customWords": [{ "match": "string", "action": "BLOCKED" }], "managedWordLists": [{ "match": "string", "type": "PROFANITY", "action": "BLOCKED" }] }, "sensitiveInformationPolicy": { "piiEntities": [{ // for all types see: https://docs.aws.amazon.com/bedrock/latest/APIReference/API_GuardrailPiiEntityConfig.html#bedrock-Type-GuardrailPiiEntityConfig-type "type": "ADDRESS" | "AGE" | ..., "match": "string", "action": "BLOCKED" | "ANONYMIZED" }], "regexes": [{ "name": "string", "regex": "string", "match": "string", "action": "BLOCKED" | "ANONYMIZED" }], "contextualGroundingPolicy": { "filters": [{ "type": "GROUNDING | RELEVANCE", "threshold": "double", "score": "double", "action": "BLOCKED | NONE" }] }, "invocationMetrics": { "guardrailProcessingLatency": "integer", "usage": { "topicPolicyUnits": "integer", "contentPolicyUnits": "integer", "wordPolicyUnits": "integer", "sensitiveInformationPolicyUnits": "integer", "sensitiveInformationPolicyFreeUnits": "integer", "contextualGroundingPolicyUnits": "integer" }, "guardrailCoverage": { "textCharacters": { "guarded":"integer", "total": "integer" } } } }, "guardrailCoverage": { "textCharacters": { "guarded": "integer", "total": "integer" } } ] }

    ApplyGuardrail API ユースケースの例

    ApplyGuardrail リクエストの出力は、渡されたコンテンツに対してガードレールが行ったアクションによって異なります。

    • ガードレールが介入し、コンテンツのマスク処理のみが行われた場合は、素のコンテンツがマスク処理を施した状態で返されます。

    • ガードレールが介入し、リクエストコンテンツをブロックした場合は、出力フィールドは 1 つのテキスト (ガードレール設定に基づく定型メッセージ) になります。

    • リクエストコンテンツに対してガードレールが何もアクションを実行しなかった場合、出力配列は空です。

    No guardrail intervention

    リクエストの例

    { "source": "OUTPUT", "content": [ "text": { "text": "Hi, my name is Zaid. Which car brand is reliable?", } ] }

    ガードレールが介入しなかった場合のレスポンス

    { "usage": { "topicPolicyUnitsProcessed": 1, "contentPolicyUnitsProcessed": 1, "wordPolicyUnitsProcessed": 0, "sensitiveInformationPolicyFreeUnits": 0 }, "action": "NONE", "outputs": [], "assessments": [{}] }
    Guardrails intervened with BLOCKED action

    レスポンスの例

    { "usage": { "topicPolicyUnitsProcessed": 1, "contentPolicyUnitsProcessed": 1, "wordPolicyUnitsProcessed": 0, "sensitiveInformationPolicyFreeUnits": 0 }, "action": "GUARDRAIL_INTERVENED", "outputs": [{ "text": "Configured guardrial canned message, i.e cannot respond", }], "assessments": [{ "topicPolicy": { "topics": [{ "name": "Cars", "type": "DENY", "action": "BLOCKED" }] }, "sensitiveInformationPolicy": { "piiEntities": [{ "type": "NAME", "match": "ZAID", "action": "ANONYMIZED" }], "regexes": [] } }] }
    Guardrails intervened with MASKED action

    レスポンスの例

    ガードレールが介入して名前をマスク処理した場合 (名前はマスク処理済み)

    { "usage": { "topicPolicyUnitsProcessed": 1, "contentPolicyUnitsProcessed": 1, "wordPolicyUnitsProcessed": 0, "sensitiveInformationPolicyFreeUnits": 0 }, "action": "GUARDRAIL_INTERVENED", "outputs": [ { "text": "Hi, my name is {NAME}. Which car brand is reliable?" }, { "text": "Hello {NAME}, ABC Cars are reliable..", } ], "assessments": [{ "sensitiveInformationPolicy": { "piiEntities": [{ "type": "NAME", "match": "ZAID", "action": "MASKED" }], "regexes": [] } }] }
    AWS CLI Example

    入力例

    # Make sure preview CLI is downloaded and setup aws bedrock-runtime apply-guardrail \ --cli-input-json '{ "guardrailIdentifier": "someGuardrailId", "guardrailVersion": "DRAFT", "source": "INPUT", "content": [ { "text": { "text": "How should I invest for my retirement? I want to be able to generate $5,000 a month" } } ] }' \ --region us-east-1 \ --output json

    出力例

    { "usage": { "topicPolicyUnits": 1, "contentPolicyUnits": 1, "wordPolicyUnits": 1, "sensitiveInformationPolicyUnits": 1, "sensitiveInformationPolicyFreeUnits": 0 }, "action": "GUARDRAIL_INTERVENED", "outputs": [ { "text": "I apologize, but I am not able to provide fiduciary advice. =" } ], "assessments": [ { "topicPolicy": { "topics": [ { "name": "Fiduciary Advice", "type": "DENY", "action": "BLOCKED" } ] } } ] }