本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在應用程式中使用 ApplyGuardrail API
Guardrails 是用來為生成式 AI 應用程式實作保護措施,這些應用程式是針對您的使用案例自訂的,並與您的負責 AI 政策保持一致。護欄可讓您設定拒絕的主題、篩選有害內容,以及移除敏感資訊。
您可以使用 ApplyGuardrail
API,使用預先設定的 Amazon Bedrock Guardrails 來評估任何文字,而無需叫用基礎模型。
ApplyGuardrail
API 的功能:
-
內容驗證 – 您可以將任何文字輸入或輸出傳送至
ApplyGuardrail
API,以與定義的主題迴避規則、內容篩選條件、PII 偵測器和字詞區塊清單進行比較。您可以獨立評估使用者輸入和 FM 產生的輸出。 -
彈性部署 – 您可以在應用程式流程中的任意位置整合
ApplyGuardrail
API,以在處理或提供結果給使用者之前驗證資料。例如,如果您使用的是 RAG 應用程式,您現在可以在執行擷取之前評估使用者輸入,而不是等到產生最終回應。 -
從 FMs 解耦。–
ApplyGuardrail
API 會從基礎模型解耦。您現在可以使用 Guardrail,而無需叫用 Foundation Models。您可以使用評估結果來設計生成式 AI 應用程式的體驗。
在您的應用程式流程中呼叫 ApplyGuardrail API
請求可讓客戶傳遞應使用其定義的護欄保護的所有內容。當要評估的內容來自使用者時,來源欄位應設定為「INPUT」,通常是 LLM 提示。當模型輸出護欄應強制執行時,來源應設定為「OUTPUT」,通常是 LLM 回應。
主題
設定護欄以搭配 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
請求的輸出取決於對傳遞內容採取的動作護欄。
-
如果護欄介入只遮罩內容的位置,則會傳回套用遮罩的確切內容。
-
如果護欄介入並封鎖請求內容,輸出欄位將是單一文字,這是根據護欄組態的固定訊息。
-
如果請求內容未採取任何護欄動作,輸出陣列會是空的。
- 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" } ] } } ] }