

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

# 在 Amazon Bedrock 中向代理添加操作组
<a name="agents-action-add"></a>

为操作组设置 OpenAPI 架构和 Lambda 函数后，您就可以创建操作组。选择与您的首选方法对应的选项卡，然后按照以下步骤操作：

**注意**  
如果您使用的是 Anthropic Claude 3.5 Sonnet，请确保您的工具名称（格式为 `httpVerb__actionGroupName__apiName`）符合 Anthropic 工具名称格式 `^[a-zA-Z0-9_-]{1,64}$`。actionGroupName 和 apiName 不得包含双下划线 `'__'`。

------
#### [ Console ]

[创建代理](agents-create.md)时，您可以在工作草稿中添加操作组。

创建代理后，您可以通过以下步骤向其添加操作组：

**向代理添加操作组**

1. 采用有权使用 Amazon Bedrock 控制台的 IAM 身份登录 AWS 管理控制台。然后，通过以下网址打开 Amazon Bedrock 控制台：[https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock)。

1. 从左侧导航窗格中选择**代理**。然后，在**代理**部分选择一个代理。

1. 选择**在代理生成器中编辑**。

1. 在**操作组**部分，选择**添加**。

1. （可选）在**操作组详细信息**部分，更改自动生成的**名称**，并为操作组提供一个可选的**描述**。

1. 在**操作组类型**部分，选择以下一种方法来定义代理可以从用户处获取的参数，用于帮助执行操作：

   1. **通过函数详细信息来定义** – 定义代理需要从用户处获取的参数，以便执行操作。有关添加函数的更多信息，请参阅 [在 Amazon Bedrock 中为代理的操作组定义函数详细信息](agents-action-function.md)。

   1. **通过 API 架构来定义** – 定义代理可以调用的 API 操作和相关参数。使用您创建的 OpenAPI 架构，或使用控制台文本编辑器来创建架构。有关设置 OpenAPI 架构的更多信息，请参阅 [在 Amazon Bedrock 中为代理的操作组定义 OpenAPI 架构](agents-api-schema.md)

1. 在**操作组调用**部分中，您可以设置代理在预测应调用的 API 或函数并收到所需的参数之后执行的操作。请选择以下选项之一：
   + **快速创建新的 Lambda 函数（*建议***）– 让 Amazon Bedrock 为您的代理创建一个基本 Lambda 函数，您随后可以在 AWS Lambda 中根据自己的使用场景对其进行修改。代理会根据会话内容，将其预测的 API 或函数以及参数传递给 Lambda 函数。
   + **选择现有的 Lambda 函数** - 选择[您之前在 AWS Lambda 中创建的 Lambda 函数](agents-lambda.md)以及要使用的函数版本。代理会根据会话内容，将其预测的 API 或函数以及参数传递给 Lambda 函数。
**注意**  
要允许 Amazon Bedrock 服务主体访问 Lambda 函数，请[向 Lambda 函数附加一项基于资源的策略](agents-permissions.md#agents-permissions-lambda)，以允许 Amazon Bedrock 服务主体访问 Lambda 函数。
   + **返回控制权** - 代理不会将其预测的 API 或函数的参数传递给 Lambda 函数，而会在 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) 响应中传递其预测应调用的操作以及从会话中确定的操作参数和信息，从而将控制权返回给应用程序。有关更多信息，请参阅 [通过在 InvokeAgent 响应中发送从用户那里获取的信息，将控制权交还给代理开发人员](agents-returncontrol.md)。

1. 根据您选择的**操作组类型**，您将看到以下部分：
   + 如果您选择**通过函数详细信息来定义**，则会看到**操作组函数**部分。执行以下操作来定义函数：

     1. 提供一个**名称**和可选（但建议提供）的**描述**。

     1. 要在调用函数之前请求用户进行确认，请选择**启用**。在调用函数之前请求确认，可以防止您的应用程序因恶意提示注入而执行操作。

     1. 在**参数**子部分中，选择**添加参数**。定义以下字段：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/agents-action-add.html)

     1. 要添加其他参数，请选择**添加参数**。

     1. 要编辑参数中的字段，请选择该字段并根据需要对其进行编辑。

     1. 要删除参数，请在包含参数的行中选择删除图标（![\[Trapezoid-shaped diagram showing data flow from source to destination through AWS Transfer Family.\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/trash.png)）。

     如果您更喜欢使用 JSON 对象来定义函数，请选择 **JSON 编辑器**而不是**表**。JSON 对象格式如下（`parameters` 对象中的每个键都是您提供的参数名称）：

     ```
     {
         "name": "string",
         "description": "string",
         "parameters": [
             {
                 "name": "string",
                 "description": "string",
                 "required": "True" | "False",
                 "type": "string" | "number" | "integer" | "boolean" | "array"
             }
         ]
     }
     ```

     要通过定义另一组参数向操作组添加其他函数，请选择**添加操作组函数**。
   + 如果您选择了**通过 API 架构来定义**，则会看到包含以下选项的**操作组架构**部分：
     + 要使用您之前创建并且包含操作组的 API 描述、结构和参数的 OpenAPI 架构，请选择**选择 API 架构**，并提供指向架构的 Amazon S3 URI 的链接。
     + 要使用内联架构编辑器定义 OpenAPI 架构，请选择**通过内联架构编辑器来定义**。此时将出现一个您可以编辑的示例架构。

       1. 使用**格式**旁边的下拉菜单选择架构的格式。

       1. 要从 S3 导入现有架构进行编辑，请选择**导入架构**，提供 S3 URI，然后选择**导入**。

       1. 要将架构恢复为原始示例架构，请选择**重置**，然后再次选择**重置**来确认显示的消息。

1. 创建完操作组后，选择**添加**。如果您定义了 API 架构并且没有出现问题，则会显示绿色成功横幅。如果验证架构时出现问题，将会出现红色横幅。您有以下选项：
   + 滚动浏览架构，查看存在有关格式的错误或警告的行。X 表示存在格式错误，而感叹号表示存在有关格式的警告。
   + 选择红色横幅中的**查看详细信息**，查看有关 API 架构内容的错误列表。

1. 务必选择**准备**以应用对代理所做的更改，然后再测试代理。

------
#### [ API ]

要创建操作组，请使用 [Amazon Bedrock 代理构建时端点](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)发送 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) 请求。您必须提供一个[函数架构](agents-action-function.md)或 [OpenAPI](agents-api-schema.md) 架构。

下表介绍了请求中的字段。
+ 以下字段是必填字段：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/agents-action-add.html)
+ 要定义操作组的参数，您必须指定以下字段之一（不能同时指定两个字段）。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/agents-action-add.html)

  下面显示了 `functionSchema` 和 `apiSchema` 的一般格式：
  + `functionSchema` 数组中的每一项都是一个 [FunctionSchema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_FunctionSchema.html) 对象。针对每个函数，指定以下内容：
    + 提供一个 `name` 和可选（但建议提供）的 `description`。
    + （可选）将 `requireConfirmation` 字段指定为 `ENABLED`，以便在调用函数之前请求用户确认。在调用函数之前请求确认，可以防止您的应用程序因恶意提示注入而执行操作。
    + `parameters` 对象中每个键都是一个参数名称，映射到 [ParameterDetail](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_ParameterDetail.html) 对象中有关它的详细信息。

    `functionSchema` 的一般格式如下：

    ```
    "functionSchema": [
        {
            "name": "string",
            "description": "string",
            "requireConfirmation": ENABLED | DISABLED,
            "parameters": {
                "<string>": {
                    "type": "string" | number | integer | boolean | array,
                    "description": "string",
                    "required": boolean
                },
                ... // up to 5 parameters
            }
        },
        ... // up to 11 functions
    ]
    ```
  + [APISchema](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_APISchema.html) 可以采用以下格式之一：

    1. 对于以下格式，您可以直接粘贴 JSON 或 YAML 格式的 OpenAPI 架构作为值。

       ```
       "apiSchema": {
           "payload": "string"
       }
       ```

    1. 对于以下格式，请指定存储 OpenAPI 架构的 Amazon S3 存储桶名称和对象键。

       ```
       "apiSchema": {
           "s3": {
               "s3BucketName": "string",
               "s3ObjectKey": "string"
           }
       }
       ```
+ 要配置操作组在从用户获取参数后处理操作组调用的方式，您必须在 `actionGroupExecutor` 字段中指定以下字段之一。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/agents-action-add.html)
+ 以下字段是可选字段：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/agents-action-add.html)

  ```
      def create_agent_action_group(
              self, name, description, agent_id, agent_version, function_arn, api_schema
      ):
          """
          Creates an action group for an agent. An action group defines a set of actions that an
          agent should carry out for the customer.
  
          :param name: The name to give the action group.
          :param description: The description of the action group.
          :param agent_id: The unique identifier of the agent for which to create the action group.
          :param agent_version: The version of the agent for which to create the action group.
          :param function_arn: The ARN of the Lambda function containing the business logic that is
                               carried out upon invoking the action.
          :param api_schema: Contains the OpenAPI schema for the action group.
          :return: Details about the action group that was created.
          """
          try:
              response = self.client.create_agent_action_group(
                  actionGroupName=name,
                  description=description,
                  agentId=agent_id,
                  agentVersion=agent_version,
                  actionGroupExecutor={"lambda": function_arn},
                  apiSchema={"payload": api_schema},
              )
              agent_action_group = response["agentActionGroup"]
          except ClientError as e:
              logger.error(f"Error: Couldn't create agent action group. Here's why: {e}")
              raise
          else:
              return agent_action_group
  ```

  有关更多信息，请参阅 [开始使用 Amazon Bedrock 代理](bedrock-agent_example_bedrock-agent_Hello_section.md)。

------