View a markdown version of this page

在应用程序中部署自动推理策略 - Amazon Bedrock

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在应用程序中部署自动推理策略

在测试自动推理策略并对其性能感到满意之后,您可以使用 Amazon Bedrock 护栏将其部署到应用程序中使用。本页介绍了完整的部署工作流程:保存不可变版本、将其附加到护栏上、使用 CloudFormation自动部署以及集成到管道中。 CI/CD

保存自动推理策略的版本

测试完策略后,创建一个不可变的版本。不可变版本可确保在您继续编辑草稿时,附加到护栏的政策不会意外更改。每个版本都由数字版本号(1、2、3、...)标识,创建后无法修改。

使用控制台

  1. 在左侧导航窗格中,选择自动推理

  2. 选择要用于您的应用程序的自动推理策略。

  3. 选择另存为新版本。您可以将此版本的策略与护栏一起使用。

使用 API

使用 CreateAutomatedReasoningPolicyVersion API 创建自动推理策略的不可变版本。

请求参数

policyArn(必需)

要为其创建版本的自动推理策略的 Amazon 资源名称(ARN)。

lastUpdatedDefinitionHash(必需)

新版本的策略定义的哈希值。从 GetAutomatedReasoningPolicy API 中检索此哈希值。这样可以确保您对所测试的策略定义进行版本控制。

示例

# 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 响应的运行时组件。您可以将自动推理策略添加到新的或现有的护栏中。

使用控制台

  1. 在左侧导航栏中,选择护栏,然后选择创建护栏(或选择现有护栏并选择编辑)。

  2. 进入添加自动推理检查功能屏幕后,选择启用自动推理策略

  3. 策略名称中,选择自动推理策略的已保存版本,然后选择下一步

  4. 完成护栏的创建或更新。

使用 API

使用CreateGuardrailUpdateGuardrail API 向您的护栏添加自动推理策略。将automatedReasoningConfig参数包含在版本化策略 ARN 中。

请求参数

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

重要

使用自动推理检查的防护栏需要跨区域推理配置文件。使用与您的地区前缀guardrailProfileIdentifier匹配的--cross-region-config参数(us.guardrail.v1:0例如,针对美国地区或eu.guardrail.v1:0欧洲地区)。如果您省略此参数,API 将返回。ValidationException

导出策略版本进行部署

要通过 CloudFormation 或 CI/CD 管道部署策略,您需要策略定义 JSON。使用 ExportAutomatedReasoningPolicyVersion API 从保存的版本中导出完整的策略定义(包括所有规则、变量和自定义类型)。

导出的定义与 CloudFormation AWS::Bedrock::AutomatedReasoningPolicy资源PolicyDefinition属性接受的格式相同。这使得将策略从交互式控制台工作流程转移到自动部署变得非常简单。

# 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::AutomatedReasoning策略

示例:部署策略和护栏

以下 CloudFormation 模板创建了一个自动推理策略,其中包含策略定义和引用该策略的护栏。将策略定义替换为从测试过的策略中导出的 JSON。

AWSTemplateFormatVersion: '2010-09-09' Description: Deploy an Automated Reasoning policy and guardrail Parameters: PolicyName: Type: String Default: MyHRPolicy Description: Name of the Automated Reasoning policy GuardrailName: Type: String Default: HR-Policy-Guardrail Description: 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: A1B2C3D4E5F6 Expression: >- (=> (and isFullTime (> tenureMonths 12)) eligibleForParentalLeave) - Id: G7H8I9J0K1L2 Expression: >- (=> (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 响应。有关更多信息,请参阅 在您的应用程序中集成自动推理检查