

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

# 操作的完成处理
<a name="action-handle"></a>

在配置操作组时，您还选择了以下选项之一，让代理传递从用户处收到的信息和参数：
+ 将用户输入添加到代理的操作组。如果代理没有足够的信息来完成任务，那么通过用户输入，代理可以[请求用户提供更多信息](agents-user-input.md)。
+ 传递给[您创建的 Lambda 函数](agents-lambda.md)，以便定义操作组的业务逻辑。
+ 不使用 Lambda 函数，而是通过在 `InvokeAgent` 响应中传递来自用户的信息和参数来[返回控制权](agents-returncontrol.md)。这些信息和参数可以被发送到您自己的系统以生成结果，而这些结果可以在另一个 [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) 请求的 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_SessionState.html#bedrock-Type-agent-runtime_SessionState](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_SessionState.html#bedrock-Type-agent-runtime_SessionState) 中发送。
+ 为操作启用用户确认。启用用户确认可以在调用操作组函数之前[请求应用程序用户进行确认](agents-userconfirmation.md)，从而保护您的应用程序免受恶意提示注入的影响。

选择一个主题，了解在用户提供必要信息后，如何配置操作组的完成处理方式。

**Topics**
+ [配置 Lambda 函数以发送 Amazon Bedrock 代理从用户那里获取的信息](agents-lambda.md)
+ [通过在 InvokeAgent 响应中发送从用户那里获取的信息，将控制权交还给代理开发人员](agents-returncontrol.md)
+ [在调用操作组函数之前获得用户确认](agents-userconfirmation.md)

# 配置 Lambda 函数以发送 Amazon Bedrock 代理从用户那里获取的信息
<a name="agents-lambda"></a>

您可以定义一个 Lambda 函数，用于编写操作组的业务逻辑。在 Amazon Bedrock 代理确定需要在操作组中调用的 API 操作后，它会将来自 API 架构的信息以及相关的元数据作为输入事件发送到 Lambda 函数。要编写函数，您必须了解 Lambda 函数的以下组成部分：
+ **输入事件** – 包含 API 操作请求正文中的相关元数据和已填充字段，或者代理确定必须调用的操作的函数参数。
+ **响应** – 包含从 API 操作或函数返回的响应正文的相关元数据和已填充字段。

您可以编写 Lambda 函数来定义如何处理操作组，并自定义 API 响应的返回方式。您可以使用输入事件中的变量来定义函数，并向代理返回响应。

**注意**  
一个操作组最多可以包含 11 个 API 操作，但您只能编写一个 Lambda 函数。由于 Lambda 函数一次只能接收一个输入事件并针对一个 API 操作返回响应，因此在编写函数时，您应该考虑到可能被调用的不同 API 操作。

要让代理使用 Lambda 函数，您必须将基于资源的策略附加到该函数，以便为代理提供权限。有关更多信息，请参阅 [基于资源的策略，允许 Amazon Bedrock 调用操作组 Lambda 函数](agents-permissions.md#agents-permissions-lambda) 中的步骤。有关 Lambda 中基于资源的策略的更多信息，[请参阅开发人员指南中的对 Lambda 使用基于资源的策略](https://docs.aws.amazon.com/lambda/latest/dg/access-control-resource-based.html)。 AWS Lambda 

要了解如何在创建操作组时定义函数，请参阅 [在 Amazon Bedrock 中向代理添加操作组](agents-action-add.md)。

**Topics**
+ [来自 Amazon Bedrock 的 Lambda 输入事件](#agents-lambda-input)
+ [对 Amazon Bedrock 的 Lambda 响应事件](#agents-lambda-response)
+ [操作组 Lambda 函数示例](#agents-lambda-example)

## 来自 Amazon Bedrock 的 Lambda 输入事件
<a name="agents-lambda-input"></a>

调用使用 Lambda 函数的操作组时，Amazon Bedrock 会发送以下一般格式的 Lambda 输入事件。您可以定义 Lambda 函数，使用任何输入事件字段来处理函数中的业务逻辑，以便成功执行操作。有关 Lambda 函数的更多信息，请参阅开发人员指南中的[事件驱动调用](https://docs.aws.amazon.com/lambda/latest/dg/lambda-services.html#event-driven-invocation)。 AWS Lambda 

输入事件的格式取决于您是使用 API 架构还是使用函数详细信息定义操作组：
+ 如果您使用 API 架构定义操作组，则输入事件的格式如下：

  ```
  {
      "messageVersion": "1.0",
      "agent": {
          "name": "string",
          "id": "string",
          "alias": "string",
          "version": "string"
      },
      "inputText": "string",
      "sessionId": "string",
      "actionGroup": "string",
      "apiPath": "string",
      "httpMethod": "string",
      "parameters": [
          {
              "name": "string",
              "type": "string",
              "value": "string"
          },
      ...
      ],
      "requestBody": {
          "content": {
              "<content_type>": {
                  "properties": [
                     {
                         "name": "string",
                         "type": "string",
                         "value": "string"
                      },
                              ...
                  ]
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      }
  }
  ```
+ 如果您使用函数详细信息定义操作组，则输入事件的格式如下：

  ```
  {
      "messageVersion": "1.0",
      "agent": {
          "name": "string",
          "id": "string",
          "alias": "string",
          "version": "string"
      },
      "inputText": "string",
      "sessionId": "string",
      "actionGroup": "string",
      "function": "string",
      "parameters": [
          {
              "name": "string",
              "type": "string",
              "value": "string"
          },
      ...
      ],
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      }
  }
  ```

下面的列表介绍了输入事件字段：
+ `messageVersion` – 消息的版本，用于标识进入 Lambda 函数的事件数据格式以及 Lambda 函数的预期响应格式。Amazon Bedrock 仅支持版本 1.0。
+ `agent` – 包含操作组所属代理的名称、ID、别名和版本的相关信息。
+ `inputText` – 用户在对话回合中的输入。
+ `sessionId` – 代理会话的唯一标识符。
+ `actionGroup` – 操作组的名称。
+ `parameters` – 包含对象列表。每个对象都包含 API 操作中参数的名称、类型和值，如 OpenAPI 架构或函数中所定义。
+ 如果您使用 API 架构定义操作组，则输入事件包含以下字段：
  + `apiPath` – API 操作的路径，如 OpenAPI 架构中所定义。
  + `httpMethod` – API 操作的方法，如 OpenAPI 架构中所定义。
  + `requestBody` – 包含请求正文及其属性，如操作组的 OpenAPI 架构中所定义。
+ 如果您使用函数详细信息定义操作组，则输入事件包含以下字段：
  + `function` – 操作组的函数详细信息中定义的函数名称。
+ `sessionAttributes` – 包含[会话属性](agents-session-state.md)及其值。这些属性在[会话](advanced-prompts.md#advanced-prompts-terminology)期间存储，并为代理提供上下文。
+ `promptSessionAttributes` – 包含[提示会话属性](agents-session-state.md)及其值。这些属性在[回合](advanced-prompts.md#advanced-prompts-terminology)中存储，并为代理提供上下文。

## 对 Amazon Bedrock 的 Lambda 响应事件
<a name="agents-lambda-response"></a>

Amazon Bedrock 希望从 Lambda 函数获得符合以下格式的响应。响应由从 API 操作返回的参数组成。代理可以使用 Lambda 函数的响应进行进一步编排或借助它向客户返回响应。

**注意**  
最大有效载荷响应大小与 Lambda 函数的同步响应的最大大小相匹配。有关更多信息，请参阅《 AWS Lambda 开发人员指南》中的*调用负载*资源[配额](https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html#function-configuration-deployment-and-execution)。

输入事件的格式取决于您是使用 API 架构还是使用函数详细信息定义操作组：
+ 如果您使用 API 架构定义操作组，则响应格式如下：

  ```
  {
      "messageVersion": "1.0",
      "response": {
          "actionGroup": "string",
          "apiPath": "string",
          "httpMethod": "string",
          "httpStatusCode": number,
          "responseBody": {
              "<contentType>": {
                  "body": "JSON-formatted string" 
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
          ...
      },
      "promptSessionAttributes": {
          "string": "string",
          ...
      },
      "knowledgeBasesConfiguration": [
          {
              "knowledgeBaseId": "string",
              "retrievalConfiguration": {
                  "vectorSearchConfiguration": {
                      "numberOfResults": int,
                      "overrideSearchType": "HYBRID | SEMANTIC",
                      "filter": [RetrievalFilter](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html) object
                  }
              }
          },
          ...
      ]
  }
  ```
+ 如果您使用函数详细信息定义操作组，则响应格式如下：

  ```
  {
      "messageVersion": "1.0",
      "response": {
          "actionGroup": "string",
          "function": "string",
          "functionResponse": {
              "responseState": "FAILURE | REPROMPT",
              "responseBody": {
                  "<functionContentType>": { 
                      "body": "JSON-formatted string"
                  }
              }
          }
      },
      "sessionAttributes": {
          "string": "string",
      },
      "promptSessionAttributes": {
          "string": "string"
      },
      "knowledgeBasesConfiguration": [
          {
              "knowledgeBaseId": "string",
              "retrievalConfiguration": {
                  "vectorSearchConfiguration": {
                      "numberOfResults": int,
                      "filter": {
                          [RetrievalFilter](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievalFilter.html) object
                      }
                  }
              }
          },
          ...
      ]
  }
  ```

下面的列表介绍了响应字段：
+ `messageVersion` – 消息的版本，用于标识进入 Lambda 函数的事件数据格式以及 Lambda 函数的预期响应格式。Amazon Bedrock 仅支持版本 1.0。
+ `response` – 包含有关 API 响应的以下信息。
  + `actionGroup` – 操作组的名称。
  + 如果您使用 API 架构定义操作组，则响应中可能包含以下字段：
    + `apiPath` – API 操作的路径，如 OpenAPI 架构中所定义。
    + `httpMethod` – API 操作的方法，如 OpenAPI 架构中所定义。
    + `httpStatusCode` – 从 API 操作返回的 HTTP 状态代码。
    + `responseBody` – 包含响应正文，如 OpenAPI 架构中所定义。
  + 如果您使用函数详细信息定义操作组，则响应中可能包含以下字段：
    + `responseState`（可选）– 设置为以下状态之一，以定义代理在处理操作后的行为：
      + FAILURE – 代理抛出当前会话的 `DependencyFailedException`。适用于由于依赖项故障而导致函数执行失败的情况。
      + REPROMPT – 代理向模型传递响应字符串，以重新提示模型。适用于由于输入无效而导致函数执行失败的情况。
    + `responseBody` – 包含一个对象，该对象定义了函数执行后的响应。键是内容类型（目前仅支持 `TEXT`），值是包含响应 `body` 的对象。
+ （可选）`sessionAttributes` – 包含会话属性及其值。有关更多信息，请参阅 [会话和提示会话属性](agents-session-state.md#session-state-attributes)。
+ （可选）`promptSessionAttributes` – 包含提示属性及其值。有关更多信息，请参阅 [会话和提示会话属性](agents-session-state.md#session-state-attributes)。
+ （可选）`knowledgeBasesConfiguration` – 包含附加到代理的知识库的查询配置列表。有关更多信息，请参阅 [知识库检索配置](agents-session-state.md#session-state-kb)。

## 操作组 Lambda 函数示例
<a name="agents-lambda-example"></a>

以下是如何在中定义 Lambda 函数的最小示例。Python根据您是使用 OpenAPI 架构还是函数详细信息定义操作组来选择相应的选项卡：

------
#### [ OpenAPI schema ]

```
def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    api_path = event['apiPath']
    # get parameters
    get_parameters = event.get('parameters', [])
    # post parameters
    post_parameters = event['requestBody']['content']['application/json']['properties']

    response_body = {
        'application/json': {
            'body': "sample response"
        }
    }
    
    action_response = {
        'actionGroup': event['actionGroup'],
        'apiPath': event['apiPath'],
        'httpMethod': event['httpMethod'],
        'httpStatusCode': 200,
        'responseBody': response_body
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    api_response = {
        'messageVersion': '1.0', 
        'response': action_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return api_response
```

------
#### [ Function details ]

```
def lambda_handler(event, context):

    agent = event['agent']
    actionGroup = event['actionGroup']
    function = event['function']
    parameters = event.get('parameters', [])

    response_body = {
        'TEXT': {
            'body': "sample response"
        }
    }
    
    function_response = {
        'actionGroup': event['actionGroup'],
        'function': event['function'],
        'functionResponse': {
            'responseBody': response_body
        }
    }
    
    session_attributes = event['sessionAttributes']
    prompt_session_attributes = event['promptSessionAttributes']
    
    action_response = {
        'messageVersion': '1.0', 
        'response': function_response,
        'sessionAttributes': session_attributes,
        'promptSessionAttributes': prompt_session_attributes
    }
        
    return action_response
```

------

# 通过在 InvokeAgent 响应中发送从用户那里获取的信息，将控制权交还给代理开发人员
<a name="agents-returncontrol"></a>

您可以选择通过在 [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) 响应中发送信息将控制权交还给代理开发人员，而不是将您的代理从用户那里获取的信息发送到 Lambda 函数进行处理。在创建或更新操作组时，您可以配置将控制权交还给代理开发人员。通过 API，您可以在 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) 或 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html) 请求中的 `actionGroupExecutor` 对象中，将 `customControl` 的值指定为 `RETURN_CONTROL`。有关更多信息，请参阅 [在 Amazon Bedrock 中向代理添加操作组](agents-action-add.md)。

如果您为操作组配置了控制权交还，并且代理确定应该调用操作组中的操作，那么从用户那里获取的 API 或函数详细信息将在 [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) 响应中的 `invocationInputs` 字段中返回，同时，系统会返回唯一的 `invocationId`。然后，您可执行以下操作：
+ 根据 `invocationInputs` 中返回的信息，设置您的应用程序以调用您定义的 API 或函数。
+ 在另一个 [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) 请求中发送应用程序调用的结果，并将结果放入 `sessionState` 字段中，以便为代理提供上下文。您必须使用在 [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) 响应中返回的 `invocationId` 和 `actionGroup`。这些信息可用作进一步编排的上下文，发送到后处理阶段，以便代理对响应进行格式化，或者直接用于代理对用户的响应。
**注意**  
如果您在 `sessionState` 字段中包含 `returnControlInvocationResults`，则 `inputText` 字段将被忽略。

要了解如何在创建操作组时配置将控制权交还给代理开发人员，请参阅 [在 Amazon Bedrock 中向代理添加操作组](agents-action-add.md)。

## 将控制权交还给代理开发人员的示例
<a name="agents-returncontrol-ex"></a>

例如，您可能设置了以下操作组：
+ 一个名为 `PlanTrip` 的操作组，其中包含一个 `suggestActivities` 操作，帮助用户在旅途中查找要进行的活动。该操作的 `description` 为：`This action suggests activities based on retrieved weather information`。
+ 一个名为 `WeatherAPIs` 的操作组，其中包含一个 `getWeather` 操作，帮助用户获取特定位置的天气。该操作必需的参数为 `location` 和 `date`。您将操作组配置为将控制权交还给代理开发人员。

下面提供了一个假设的操作流程：

1. 用户向您的代理发出以下查询：**What should I do today?**。该查询通过 [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) 请求中的 `inputText` 字段发送。

1. 您的代理确定应该调用 `suggestActivities` 操作，但是根据其描述，代理预测应该首先调用 `getWeather` 操作，以获取上下文信息来帮助完成 `suggestActivities` 操作。

1. 代理知道当前 `date` 为 `2024-09-15`，但获取天气信息所需的参数是用户的 `location`。代理会再次向用户提问“您在哪里？”

1. 用户提供的回复是 **Seattle**。

1. 代理在以下 [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) 响应中返回 `getWeather` 所需的参数（请选择一个选项卡，查看使用该方法定义的操作组的示例）：

------
#### [ Function details ]

   ```
   HTTP/1.1 200
   x-amzn-bedrock-agent-content-type: application/json
   x-amz-bedrock-agent-session-id: session0
   Content-type: application/json
    
   {
       "returnControl": {
           "invocationInputs": [{
               "functionInvocationInput": {
                   "actionGroup": "WeatherAPIs",
                   "function": "getWeather",
                   "parameters": [
                       {
                           "name": "location",
                           "type": "string",
                           "value": "seattle"
                       },
                       {
                           "name": "date",
                           "type": "string",
                           "value": "2024-09-15"
                       }
                   ]
               }
           }],
           "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172"
       }
   }
   ```

------
#### [ OpenAPI schema ]

   ```
   HTTP/1.1 200
   x-amzn-bedrock-agent-content-type: application/json
   x-amz-bedrock-agent-session-id: session0
   Content-type: application/json
   
   {
       "invocationInputs": [{
           "apiInvocationInput": {
               "actionGroup": "WeatherAPIs",
               "apiPath": "/get-weather",
               "httpMethod": "get",
               "parameters": [
                   {
                       "name": "location",
                       "type": "string",
                       "value": "seattle"
                   },
                   {
                       "name": "date",
                       "type": "string",
                       "value": "2024-09-15"
                   }
               ]
           }
       }],
       "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad"
   }
   ```

------

1. 您的应用程序已配置为使用这些参数来获取与日期 `seattle` 对应的 `2024-09-15` 的天气。天气情况为下雨。

1. 您在另一个 [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) 请求的 `sessionState` 字段中发送这些结果，使用与之前的响应相同的 `invocationId`、`actionGroup` 和 `function`。请选择一个选项卡，查看使用该方法定义的操作组的示例：

------
#### [ Function details ]

   ```
   POST https://bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
               
   {
       "enableTrace": true,
       "sessionState": {
           "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172",
           "returnControlInvocationResults": [{
               "functionResult": {
                   "actionGroup": "WeatherAPIs",
                   "function": "getWeather",
                   "responseBody": {
                       "TEXT": {
                           "body": "It's rainy in Seattle today."
                       }
                   }
               }
           }]
       }
   }
   ```

------
#### [ OpenAPI schema ]

   ```
   POST https: //bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
    
   {
       "enableTrace": true,
       "sessionState": {
           "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad",
           "returnControlInvocationResults": [{
               "apiResult": {
                   "actionGroup": "WeatherAPIs",
                   "httpMethod": "get",
                   "apiPath": "/get-weather",
                   "responseBody": {
                       "application/json": {
                           "body": "It's rainy in Seattle today."
                       }
                   }
               }
           }]
       }
   }
   ```

------

1. 代理预测应该调用 `suggestActivities` 操作。它使用当天下雨的上下文信息，并在响应中建议用户进行室内活动，而不是室外活动。

### 将控制权交还给协作者代理的示例
<a name="collaborator-agent-returncontrol-ex"></a>

如果您正在使用[多代理协作](agents-multi-agent-collaboration.md)，并且代理协作者选择通过在 [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) 响应中发送信息来交还控制权，则该信息将在主管代理响应中返回，并带有其他标识符 `agentId` 和 `collaboratorName`。请选择一个选项卡，查看使用该方法定义的操作组的示例：

------
#### [ Function details ]

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: application/json
x-amz-bedrock-agent-session-id: session0
Content-type: application/json
 
{
    "returnControl": {
        "invocationInputs": [{
            "functionInvocationInput": {
                "agentId": "AGENTID",
                "collaboratorName": "WeatherAgent"
                "actionGroup": "WeatherAPIs",
                "function": "getWeather",
                "parameters": [
                    {
                        "name": "location",
                        "type": "string",
                        "value": "seattle"
                    },
                    {
                        "name": "date",
                        "type": "string",
                        "value": "2024-09-15"
                    }
                ]
            }
        }],
        "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172"
    }
}
```

------
#### [ OpenAPI Schema ]

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: application/json
x-amz-bedrock-agent-session-id: session0
Content-type: application/json

{
    "invocationInputs": [{
        "apiInvocationInput": {
            "actionGroup": "WeatherAPIs",
            "agentId": "AGENTID",
            "collaboratorName": "WeatherAgent"
            "apiPath": "/get-weather",
            "httpMethod": "get",
            "parameters": [
                {
                    "name": "location",
                    "type": "string",
                    "value": "seattle"
                },
                {
                    "name": "date",
                    "type": "string",
                    "value": "2024-09-15"
                }
            ]
        }
    }],
    "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad"
}
```

------

主管代理的调用者会将 `sessionState` 字段中的结果发送回主管代理，并附上相应的 `agentId`，以便将其传播给正确的代理协作者。

------
#### [ Function details ]

```
POST https://bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
            
{
    "enableTrace": true,
    "sessionState": {
        "invocationId": "79e0feaa-c6f7-49bf-814d-b7c498505172",
        "returnControlInvocationResults": [{
            "functionResult": {
                "agentId": "AGENTID",
                "actionGroup": "WeatherAPIs",
                "function": "getWeather",
                "responseBody": {
                    "TEXT": {
                        "body": "It's rainy in Seattle today."
                    }
                }
            }
        }]
    }
}
```

------
#### [ OpenAPI Schema ]

```
POST https: //bedrock-agent-runtime.us-east-1.amazonaws.com/agents/AGENT12345/agentAliases/TSTALIASID/sessions/abb/text
 
{
    "enableTrace": true,
    "sessionState": {
        "invocationId": "337cb2f6-ec74-4b49-8141-00b8091498ad",
        "returnControlInvocationResults": [{
            "apiResult": {
                "agentId": "AGENTID",
                "actionGroup": "WeatherAPIs",
                "httpMethod": "get",
                "apiPath": "/get-weather",
                "responseBody": {
                    "application/json": {
                        "body": "It's rainy in Seattle today."
                    }
                }
            }
        }]
    }
}
```

------

# 在调用操作组函数之前获得用户确认
<a name="agents-userconfirmation"></a>

您可以在调用操作组函数之前请求应用程序用户进行确认，从而保护您的应用程序免受恶意提示注入的影响。当终端用户与您的应用程序交互时，Amazon Bedrock 代理会确定需要调用的 API 或知识库，以便自动执行该用户的任务。API 或知识库中的信息可能包含潜在的破坏性数据。在每次迭代之间，如果响应中包含任何指令，代理将按照指令进行操作。如果响应中包含指令，要求模型调用意外操作，代理将继续执行该指令。为确保某些操作只有在用户明确同意后才会执行，我们建议您在调用函数之前请求终端用户进行确认。

配置操作组时，您可以选择为特定操作启用用户确认。如果您为某项操作启用了用户确认，代理会回应一个确认问题，询问终端用户是否确认或拒绝该操作。您可以通过控制台、CLI 或 SDK 启用用户确认。

要为操作启用用户确认，请参阅 [在 Amazon Bedrock 中向代理添加操作组](agents-action-add.md)。

## 用户确认的运作方式
<a name="user-confirmation-works"></a>

用户确认是由代理开发人员为操作组中的操作配置的。如果代理决定调用该操作，系统将在 [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) 响应的 `invocationInputs` 字段中返回用户提供的 API 或函数详细信息以及代理开发人员配置的用户确认，同时还会包含 `invocationType` 和唯一的 `invocationId`。

代理会调用 `invocationInputs` 中提供的 API 或函数。如果为该函数或 API 启用了用户确认，用户将看到一个选项，询问是否**确认**或**拒绝**响应中提到的操作。

代理调用函数或 API 的结果将在另一个 [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) 请求的 `sessionState` 字段中发送，以便为代理提供上下文。`InvokeAgent` 请求的参数使用 `returnControlInvocationResults`，它是映射到 `apiResult` 或 `functionResult` 对象的列表。`apiResult` 和 `functionResult` 对象还有一个附加字段 `confirmationState`。该字段包含用户确认响应。

如果用户响应为**确认**，系统会执行响应中的函数或 API。

如果用户响应为**拒绝**，系统不会执行响应中的函数或 API。

## InvokeAgent 响应和请求的示例
<a name="agents-userconfirmation-ex"></a>

**响应**

```
HTTP/1.1 200
x-amzn-bedrock-agent-content-type: contentType
x-amz-bedrock-agent-session-id: sessionId
Content-type: application/json

{
   "chunk": { 
      ...
   },
   ...
   "returnControl": { 
      "invocationId": "string",
      "invocationInputs": [ 
         { ... }
      ]
   },
   "trace": { 
      "agentAliasId": "string",
      "agentId": "string",
      "agentVersion": "string",
      "sessionId": "string",
      "trace": { ... }
   },
}
```

**请求**

```
POST /agents/agentId/agentAliases/agentAliasId/sessions/sessionId/text HTTP/1.1
Content-type: application/json

{
   "enableTrace": boolean,
   "endSession": boolean,
   "inputText": "string",
   "sessionState": { 
      "invocationId": "string",
      "promptSessionAttributes": { 
         "string" : "string" 
      },
      "returnControlInvocationResults": [ 
         { ... }
      ],
      "sessionAttributes": { 
         "string" : "string" 
      }
   }
}
```