本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在應用程式中部署自動推理政策
在測試自動推理政策並對其效能感到滿意之後,您可以將其部署為與應用程式和 Amazon Bedrock 防護機制搭配使用。此頁面涵蓋完整的部署工作流程:儲存不可變版本、將其連接至護欄、使用 自動化部署 CloudFormation,以及整合到 CI/CD 管道。
儲存自動推理政策的版本
完成政策測試後,請建立不可變的版本。不可變版本可確保在您繼續編輯 DRAFT 時,連接到護欄的政策不會意外變更。每個版本都由數字版本編號 (1、2、3、...) 識別,且在建立後無法修改。
使用主控台
-
在左側導覽中,選擇自動推理。
-
選擇您要與應用程式搭配使用的自動推理政策。
-
選擇儲存為新版本。您可以將此版本的政策與防護機制搭配使用。
使用 API
使用 CreateAutomatedReasoningPolicyVersion API 建立自動化理由政策的不可變版本。
請求參數
policyArn(必要)-
要為其建立版本的自動推理政策的 Amazon Resource Name (ARN)。
lastUpdatedDefinitionHash(必要)-
新版本政策定義的雜湊。從
GetAutomatedReasoningPolicyAPI 擷取此雜湊。這可確保您正在版本控制所測試的確切政策定義。
範例
# Get the current definition hash aws bedrock get-automated-reasoning-policy \ --policy-arn "arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk" \ --query "definitionHash" --output text # Create the version aws bedrock create-automated-reasoning-policy-version \ --policy-arn "arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk" \ --last-updated-definition-hash "583463f067a8a4f49fc1206b4642fd40..."
回應範例:
{ "policyArn": "arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk", "version": "1", "name": "MyHRPolicy" }
將自動推理政策新增至防護機制
一旦您儲存了自動化理由政策的版本,請將其新增至護欄。護欄是應用程式用來驗證 LLM 回應的執行期元件。您可以將自動化理由政策新增至新的或現有的護欄。
使用主控台
-
在左側導覽中,選擇護欄,然後選擇建立護欄 (或選取現有的護欄,然後選擇編輯)。
-
當您進入新增自動推理檢查畫面時,選擇啟用自動推理政策。
-
針對政策名稱,選擇儲存的自動推理政策版本,然後選擇下一步。
-
完成建立或更新您的護欄。
使用 API
使用 CreateGuardrail或 UpdateGuardrail API 將自動推理政策新增至護欄。在版本控制政策 ARN 中包含 automatedReasoningConfig 參數。
請求參數
automatedReasoningConfig-
Amazon Bedrock 防護機制中自動推理檢查的組態。
policyArn(必要)-
要與護欄搭配使用的自動推理政策版本的 ARN。使用版本控制的 ARN (以
:1、 等結尾):2,而不是未版本的 ARN。
範例
aws bedrock create-guardrail \ --name "HR-Policy-Guardrail" \ --description "Guardrail for HR policy validation" \ --automated-reasoning-policy-config policies="arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk:1" \ --cross-region-config '{"guardrailProfileIdentifier": "us.guardrail.v1:0"}' \ --blocked-input-messaging "I cannot process this request." \ --blocked-outputs-messaging "I cannot provide this response."
重要
使用版本控制政策 ARN (例如,arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk:1)。如果您使用未版本的 ARN,API 會傳回錯誤。請先使用 建立版本CreateAutomatedReasoningPolicyVersion。
重要
使用自動推理檢查的護欄需要跨區域推論設定檔。將 --cross-region-config 參數包含guardrailProfileIdentifier與區域字首相符的 (例如,us.guardrail.v1:0美國區域或eu.guardrail.v1:0歐洲區域)。如果您省略此參數,API 會傳回 ValidationException。
匯出政策版本以進行部署
若要透過 CloudFormation 或 CI/CD 管道部署政策,您需要政策定義 JSON。使用 ExportAutomatedReasoningPolicyVersion API 從儲存的版本匯出完整的政策定義,包括所有規則、變數和自訂類型。
匯出的定義與AWS::Bedrock::AutomatedReasoningPolicy資源PolicyDefinition屬性接受 CloudFormation 的格式相同。這可讓您直接將政策從互動式主控台工作流程移至自動化部署。
# Export the policy definition from version 1 aws bedrock export-automated-reasoning-policy-version \ --policy-arn "arn:aws:bedrock:us-east-1:111122223333:automated-reasoning-policy/lnq5hhz70wgk:1" \ --query "policyDefinition" \ --output json >policy-definition.json
匯出的 JSON 包含下列結構:
{ "version": "1.0", "variables": [ { "name": "isFullTime", "type": "BOOL", "description": "Whether the employee works full-time (true) or part-time (false)." }, { "name": "tenureMonths", "type": "INT", "description": "The number of complete months the employee has been continuously employed." } ], "rules": [ { "id": "A1B2C3D4E5F6", "expression": "(=> (and isFullTime (> tenureMonths 12)) eligibleForParentalLeave)" } ], "types": [] }
將此檔案與您的 CloudFormation 範本一起存放在版本控制中。當您更新政策時,請匯出新版本並更新 檔案以觸發部署。
使用 自動化部署 CloudFormation
使用 CloudFormation 將自動化推理政策和護欄部署為基礎設施做為程式碼。AWS::Bedrock::AutomatedReasoningPolicy 資源會建立政策,其中包含您從 API 或主控台匯出的政策定義。結合 AWS::Bedrock::Guardrail,您可以在單一範本中部署完整的驗證堆疊。
注意
CloudFormation 會使用您提供的政策定義來建立政策資源。它不會執行建置工作流程或從來源文件擷取規則。您必須先以互動方式建立和測試政策 (使用主控台、API 或 Kiro CLI),然後匯出測試的政策定義,以便在範本中使用。如需詳細資訊,請參閱匯出政策版本以進行部署。
如需政策資源的完整屬性參考,請參閱 CloudFormation 範本參考中的 AWS::Bedrock::AutomatedReasoningPolicy。
範例:部署政策和護欄
下列 CloudFormation 範本會使用政策定義和參考它的護欄來建立自動化理由政策。將政策定義取代為從測試政策匯出的 JSON。
AWSTemplateFormatVersion: '2010-09-09' Description: Deploy an Automated Reasoning policy and guardrail Parameters: PolicyName: Type: String Default:MyHRPolicyDescription: Name of the Automated Reasoning policy GuardrailName: Type: String Default:HR-Policy-GuardrailDescription: Name of the guardrail Resources: AutomatedReasoningPolicy: Type: AWS::Bedrock::AutomatedReasoningPolicy Properties: Name: !Ref PolicyName Description: Validates HR chatbot responses about leave eligibility PolicyDefinition: Version: '1.0' Variables: - Name: isFullTime Type: BOOL Description: >- Whether the employee works full-time (true) or part-time (false). Set to true when users mention being full-time or working 40+ hours per week. - Name: tenureMonths Type: INT Description: >- The number of complete months the employee has been continuously employed. When users mention years of service, convert to months (for example, 2 years = 24 months). - Name: eligibleForParentalLeave Type: BOOL Description: >- Whether the employee is eligible for parental leave based on employment status and tenure. Rules: - Id:A1B2C3D4E5F6Expression: >- (=> (and isFullTime (> tenureMonths 12)) eligibleForParentalLeave) - Id:G7H8I9J0K1L2Expression: >- (=> (or (not isFullTime) (<= tenureMonths 12)) (not eligibleForParentalLeave)) Types: [] Tags: - Key: Environment Value: Production - Key: Team Value: HR Guardrail: Type: AWS::Bedrock::Guardrail Properties: Name: !Ref GuardrailName Description: Guardrail with Automated Reasoning checks for HR policy BlockedInputMessaging: I cannot process this request. BlockedOutputsMessaging: I cannot provide this response. AutomatedReasoningPolicyConfig: Policies: - !GetAtt AutomatedReasoningPolicy.PolicyArn CrossRegionConfig: GuardrailProfileArn: !Sub "arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:guardrail-profile/us.guardrail.v1:0" Outputs: PolicyArn: Description: ARN of the Automated Reasoning policy Value: !GetAtt AutomatedReasoningPolicy.PolicyArn PolicyId: Description: ID of the Automated Reasoning policy Value: !GetAtt AutomatedReasoningPolicy.PolicyId GuardrailId: Description: ID of the guardrail Value: !Ref Guardrail
提示
對於生產部署,請將政策定義保留在單獨的 JSON 檔案中,並使用 Fn::Include或 將其載入為範本參數來參考。這可讓範本保持乾淨,並讓您更輕鬆地獨立更新政策定義。
重要
使用自動推理檢查的護欄需要跨區域推論設定檔。CrossRegionConfig 屬性會指定您區域的護欄設定檔 ARN。將區域字首 (us) 取代為您的部署區域 (例如,eu歐洲區域) 適用的字首。如果您省略此屬性,護欄建立會失敗。
範例:使用客戶受管 KMS 金鑰部署
若要使用客戶受管 KMS 金鑰加密政策,請新增 KmsKeyId 屬性。您也必須設定金鑰政策,以允許 Amazon Bedrock 使用金鑰。如需必要的金鑰政策許可,請參閱 自動推理政策的 KMS 許可。
AutomatedReasoningPolicy: Type: AWS::Bedrock::AutomatedReasoningPolicy Properties: Name: !Ref PolicyName Description: Validates HR chatbot responses about leave eligibility KmsKeyId: !GetAtt PolicyEncryptionKey.Arn PolicyDefinition: # ... policy definition ... Tags: - Key: Environment Value: Production
重要
變更 KmsKeyId 屬性需要取代 資源。 CloudFormation 會刪除現有的政策,並使用新的 ARN 建立新的政策。更新參考舊政策 ARN 的任何護欄。
後續步驟
部署政策和護欄之後, 會將自動化理由檢查整合到您的應用程式中,以在執行時間驗證 LLM 回應。如需詳細資訊,請參閱在您的應用程式中整合自動推理檢查。