

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

# アプリケーションで ApplyGuardrail API を使用する
<a name="guardrails-use-independent-api"></a>

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

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

`ApplyGuardrail` API の機能は次のとおりです。
+ **コンテンツ検証** – 任意のテキスト入力または出力を `ApplyGuardrail` API に送信して、定義済みのトピック回避ルール、コンテンツフィルター、PII ディテクター、単語ブロックリストと比較照合できます。ユーザーの入力と、FM が生成した出力を個別に評価できます。
+ **柔軟なデプロイ** - アプリケーションフローの任意の位置に `ApplyGuardrail` API を統合して、結果を処理またはユーザーに提示する前にデータを検証できます。例えば、RAG アプリケーションを使用している場合、最終レスポンスの生成を待つ代わりに、取得を実行する前にユーザー入力を評価できるようになりました。
+ **基盤モデルから分離** – `ApplyGuardrail` API は基盤モデルから分離されます。基盤モデルを呼び出すことなく、ガードレールを使用できます。評価結果を使用して、生成 AI アプリケーションのエクスペリエンスを設計できます。

**Topics**
+ [アプリケーションフローで ApplyGuardrail を呼び出す](#guardrails-use-independent-api-call)
+ [ApplyGuardrail で使用するガードレールを指定する](#guardrails-use-indepedent-api-call-configure)
+ [ApplyGuardrail のユースケースの例](#guardrails-use-independent-api-call-message)
+ [ApplyGuardrail レスポンスで完全な出力を返す](#guardrails-use-return-full-assessment)

## アプリケーションフローで ApplyGuardrail を呼び出す
<a name="guardrails-use-independent-api-call"></a>

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

## ApplyGuardrail で使用するガードレールを指定する
<a name="guardrails-use-indepedent-api-call-configure"></a>

`ApplyGuardrail` を使用する場合は、使用するガードレールの `{{guardrailVersion}}` と `{{guardrailIdentifier}}` を指定します。また、ガードレールのトレースを有効にして、ガードレールがブロックしたコンテンツに関する情報を得ることもできます。

------
#### [ 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 のユースケースの例
<a name="guardrails-use-independent-api-call-message"></a>

`ApplyGuardrail` リクエストの出力は、渡されたコンテンツに対してガードレールが行ったアクションによって異なります。
+ ガードレールが介入し、コンテンツのマスク処理のみが行われた場合は、素のコンテンツがマスク処理を施した状態で返されます。
+ ガードレールが介入し、リクエストコンテンツをブロックした場合は、出力フィールドは 1 つのテキスト (ガードレール設定に基づく定型メッセージ) になります。
+ リクエストコンテンツに対してガードレールが何もアクションを実行しなかった場合、出力配列は空です。

------
#### [ Guardrails takes no action ]

**リクエストの例**

```
{
    "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 blocks content ]

**レスポンスの例**

```
{
    "usage": {
        "topicPolicyUnitsProcessed": 1,
        "contentPolicyUnitsProcessed": 1,
        "wordPolicyUnitsProcessed": 0,
        "sensitiveInformationPolicyFreeUnits": 0
    },
    "action": "GUARDRAIL_INTERVENED",
    "outputs": [{
        "text": "Configured guardrail canned message (i.e., can't respond)"
    }],
    "assessments": [{
        "topicPolicy": {
            "topics": [{
                "name": "Cars",
                "type": "DENY",
                "action": "BLOCKED"
            }]
        },
        "sensitiveInformationPolicy": {
            "piiEntities": [{
                "type": "NAME",
                "match": "ZAID",
                "action": "ANONYMIZED"
            }],
            "regexes": []
        }
    }]
}
```

------
#### [ Guardrails masks content ]

**レスポンスの例**

ガードレールは、名前 `ZAID` をマスクすることで介入します。

```
{
    "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": "ANONYMIZED"
            }],
            "regexes": []
        }
    }]
}
```

------
#### [ AWS CLI example ]

**入力例**

```
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"
                    }
                ]
            }
        }
    ]
}
```

------

## ApplyGuardrail レスポンスで完全な出力を返す
<a name="guardrails-use-return-full-assessment"></a>

コンテンツは、ガードレール設定に違反すると検出されたとみなされます。例えば、グラウンディングスコアまたは関連性スコアが対応するしきい値を下回っている場合、コンテキストグラウンディングは検出されたとみなされます。

デフォルトでは、[ApplyGuardrail](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ApplyGuardrail.html) オペレーションは検出されたコンテンツのみをレスポンスで返します。`outputScope` フィールドに `FULL` 値を指定すると、完全な出力を返すことができます。この場合、レスポンスには、検出されなかったエントリも拡張デバッグ用に含まれます。

トレースを有効フルオプションに設定することで、`Invoke` および `Converse` オペレーションで同じ動作を設定できます。

**注記**  
フル出力スコープは、機密情報フィルターのワードフィルターや正規表現には適用されません。これは、個人を特定できる情報 (PII) を検出できるフィルターを含む機密情報など、その他すべてのフィルタリングポリシーに適用されます。