

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 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 管理主控台。接著，開啟位於 [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock) 的 Amazon 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_tw/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_tw/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_tw/bedrock/latest/userguide/agents-action-add.html)
+ 若要定義動作群組的參數，必須指定下列其中一個欄位 (無法同時指定兩個欄位)。  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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_tw/bedrock/latest/userguide/agents-action-add.html)
+ 以下是選填欄位：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/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
  ```

  如需詳細資訊，請參閱 [Hello Amazon Bedrock 代理人](bedrock-agent_example_bedrock-agent_Hello_section.md)。

------