

终止支持通知：2025年9月15日， AWS 我们将停止对Amazon Lex V1的支持。2025 年 9 月 15 日之后，您将无法再访问 Amazon Lex V1 控制台或 Amazon Lex V1 资源。如果您使用的是 Amazon Lex V2，请改为参阅 [Amazon Lex V2 指南](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)。

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

# 将 Lambda 函数从 Amazon Lex V1 迁移到 Amazon Lex V2
<a name="message-lambda"></a>

Amazon Lex V2 只允许机器人中的每种语言使用一个 Lambda 函数。Lambda 函数及其设置是针对您在运行时使用的机器人别名进行配置的。

如果为意图启用了对话和履行代码挂钩，则会为该语言的所有意图调用 Lambda 函数。

Amazon Lex V2 Lambda 函数的输入和输出消息格式与 Amazon Lex V1 不同。下面是 Lambda 函数输入格式的区别。
+ Amazon Lex V2 用 `interpretations` 结构取代了 `currentIntent` 和 `alternativeIntents` 结构。每种解释都包含意图、意图的 NLU 置信度分数和可选的情绪分析。
+ Amazon Lex V2 将 Amazon Lex V1 中的 `activeContexts` 和 `sessionAttributes` 移至统一的 `sessionState` 结构。此结构提供有关对话当前状态的相关信息，包括原始请求 ID。
+ Amazon Lex V2 不会返回 `recentIntentSummaryView`。改用 `sessionState` 结构中的信息。
+ Amazon Lex V2 输入在 `bot` 属性中提供了 `botId` 和 `localeId`。
+ 输入结构包含一个 `inputMode` 属性，该属性提供有关输入类型的信息：文本、语音或 DTMF。

下面是 Lambda 函数输出格式的区别：
+ Amazon Lex V1 中的 `activeContexts` 和 `sessionAttributes` 结构被 Amazon Lex V2 中的 `sessionState` 结构所取代。
+ `recentIntentSummaryView` 不包括在输出中。
+ Amazon Lex V1 `dialogAction` 结构分为两个结构，`dialogAction` 是 `sessionState` 结构的一部分，`messages` 是 `dialogAction.type` 为 `ElicitIntent` 时必需的。Amazon Lex 从此结构中选择要向用户显示的消息。

当您使用 Amazon Lex V2 构建机器人时 APIs，每种语言的每个机器人别名只有一个 Lambda 函数，而不是每个意图都有一个 Lambda 函数。如果要继续使用单独的函数，则可以创建一个路由器函数，为每个意图激活单独的函数。以下是您可以为应用程序使用或修改的路由器函数。

```
import os
import json
import boto3

# reuse client connection as global
client = boto3.client('lambda') 

def router(event):
    intent_name = event['sessionState']['intent']['name']
    fn_name = os.environ.get(intent_name)
    print(f"Intent: {intent_name} -> Lambda: {fn_name}")
    if (fn_name):
        # invoke lambda and return result
        invoke_response = client.invoke(FunctionName=fn_name, Payload = json.dumps(event))
        print(invoke_response)
        payload = json.load(invoke_response['Payload'])
        return payload
    raise Exception('No environment variable for intent: ' + intent_name)
    
def lambda_handler(event, context):
    print(event)
    response = router(event)
    return response
```

## 已更新的字段列表
<a name="lambda-migrating"></a>

下表提供了有关 Amazon Lex V2 Lambda 请求和响应中更新字段的详细信息。您可以使用这些表在版本之间映射字段。

### 请求
<a name="migrating-lambda-request"></a>

以下字段已经以 Lambda 函数请求格式进行了更新。

#### 活动的上下文
<a name="lambda-migrating-contexts"></a>

`activeContexts` 结构现在是 `sessionState` 结构的一部分。


| V1 结构 | V2 结构 | 
| --- | --- | 
| activeContexts | sessionState.activeContexts | 
| ActiveContext [\*]。 timeToLive | sessionState.activeContexts [ timeToLive | 
| ActiveContext [\*]。 timeToLive。 timeToLiveInSeconds | sessionState.activeContexts [ timeToLive。 timeToLiveInSeconds | 
| ActiveContext [\*]。 timeToLive。 turnsToLive | sessionState.activeContexts [ timeToLive。 turnsToLive | 
| activeContexts[\*].name | sessionState.activeContexts[\*].name | 
| activeContexts[\*].parameters | sessionState.activeContexts[\*].contextAttributes | 

#### 替代意图
<a name="lambda-migrating-alternative"></a>

从索引 1 到 N 的解释列表包含 Amazon Lex V2 预测的替代意图列表及其置信度分数。`recentIntentSummaryView` 已从 Amazon Lex V2 的请求结构中删除。要从 `recentIntentSummaryView` 中查看详细信息，请使用 [GetSession](API_runtime_GetSession.md) 操作。


| V1 结构 | V2 结构 | 
| --- | --- | 
| alternativeIntents | interpretations[1:\*] | 
| recentIntentSummary查看 | 不适用 | 

#### 机器人
<a name="lambda-migrating-bot"></a>

Amazon Lex V2 中，机器人和别名都有标识符。机器人 ID 是代码挂钩输入的一部分。包含别名 ID，但不包含别名名称。Amazon Lex V2 支持同一个机器人的多个区域设置，因此包含了区域设置 ID。


| V1 结构 | V2 结构 | 
| --- | --- | 
| 自动程序 | 自动程序 | 
| bot.name | bot.name | 
| 不适用 | bot.id | 
| bot.alias | 不适用 | 
| 不适用 | bot.aliasId | 
| bot.version | bot.version | 
| 不适用 | bot.localeId | 

#### 当前意图
<a name="lambda-migrating-intent"></a>

`sessionState.intent` 结构包含活动意图的详细信息。Amazon Lex V2 还会返回 `interpretations` 结构中所有意图（包括替代意图）的列表。解释列表中的第一个元素始终与 `sessionState.intent` 相同。


| V1 结构 | V2 结构 | 
| --- | --- | 
| currentIntent | sessionState.intent 或 interpretations[0].intent | 
| currentIntent.name | sessionState.intent.name 或 interpretations[0].intent.name | 
| 当前意图。 nluConfidenceScore | interpretations[0].nluConfidence.score | 

#### 对话操作
<a name="lambda-migrating-dialog"></a>

`confirmationStatus` 字段现在是 `sessionState` 结构的一部分。


| V1 结构 | V2 结构 | 
| --- | --- | 
| currentIntent.confirmationStatus | sessionState.intent.confirmationState 或 interpretations[0].intent.confirmationState | 
| 不适用 | sessionState.intent.state 或 interpretations[\*].intent.state | 

#### Amazon Kendra
<a name="lambda-migrating-kendra"></a>

`kendraResponse` 字段现在是 `sessionState` 和 `interpretations` 结构的一部分。


| V1 结构 | V2 结构 | 
| --- | --- | 
| kendraResponse | sessionState.intent.kendraResponse 或 interpretations[0].intent.kendraResponse | 

#### 情绪
<a name="lambda-migrating-sentiment"></a>

`sentimentResponse` 结构已移至新 `interpretations` 结构。


| V1 结构 | V2 结构 | 
| --- | --- | 
| sentimentResponse | interpretations[0].sentimentResponse | 
| sentimentResponse.sentimentLabel | interpretations[0].sentimentResponse.sentiment | 
| sentimentResponse.sentimentScore | interpretations[0].sentimentResponse.sentimentScore | 

#### 槽值
<a name="lambda-migrating-slots"></a>

Amazon Lex V2 在 `sessionState.intent` 结构中提供了一个 `slots` 对象，其中包含用户所说内容的解析值、解释值和原始值。Amazon Lex V2 还通过将 `slotShape` 设置为 `List` 和设置 `values` 列表来支持多值插槽。`value` 字段支持单值插槽，假设它们的形状为 `Scalar`。


| V1 结构 | V2 结构 | 
| --- | --- | 
| currentIntent.slots | sessionState.intent.slots 或 interpretations[0].intent.slots | 
| currentIntent.slots[\*].value | sessionState.intent.slots[\*].value.interpretedValue 或 interpretations[0].intent.slots[\*].value.interpretedValue | 
| 不适用 | sessionState.intent.slots[\*].value.shape 或 interpretations[0].intent.slots[\*].shape | 
| 不适用 | sessionState.intent.slots[\*].values 或 interpretations[0].intent.slots[\*].values | 
| currentIntent.slotDetails | sessionState.intent.slots 或 interpretations[0].intent.slots | 
| currentIntent.slotDetails[\*].resolutions | sessionState.intent.slots[\*].resolvedValues 或 interpretations[0].intent.slots[\*].resolvedValues | 
| currentIntent.slotDetails[\*].originalValue | sessionState.intent.slots[\*].originalValue 或 interpretations[0].intent.slots[\*].originalValue | 

#### 其他
<a name="lambda-migrating-other"></a>

Amazon Lex V2 `sessionId` 字段与 Amazon Lex V1 中的 `userId` 字段相同。Amazon Lex V2 还会发送呼叫方的 `inputMode`：文本、DTMF 或语音。


| V1 结构 | V2 结构 | 
| --- | --- | 
| userId | sessionId | 
| inputTranscript | inputTranscript | 
| invocationSource | invocationSource | 
| outputDialogMode | responseContentType | 
| messageVersion | messageVersion | 
| sessionAttributes | sessionState.sessionAttributes | 
| requestAttributes | requestAttributes | 
| 不适用 | inputMode | 
| 不适用 | originatingRequestId | 

### 响应
<a name="migrating-lambda-response"></a>

以下字段已经以 Lambda 函数响应消息格式进行了更新。

#### 活动的上下文
<a name="lambda-migrating-resonse-context"></a>

`activeContexts` 结构已移至 `sessionState` 结构。


| V1 结构 | V2 结构 | 
| --- | --- | 
| activeContexts | sessionState.activeContexts | 
| ActiveContext [\*]。 timeToLive | sessionState.activeContexts [ timeToLive | 
| ActiveContext [\*]。 timeToLive。 timeToLiveInSeconds | sessionState.activeContexts [ timeToLive。 timeToLiveInSeconds | 
| ActiveContext [\*]。 timeToLive。 turnsToLive | sessionState.activeContexts [ timeToLive。 turnsToLive | 
| activeContexts[\*].name | sessionState.activeContexts[\*].name | 
| activeContexts[\*].parameters | sessionState.activeContexts[\*].contextAttributes | 

#### 对话操作
<a name="lambda-migrating-response-dialog"></a>

`dialogAction` 结构已移至 `sessionState` 结构。现在，您可以在对话框操作中指定多条消息，`genericAttachments` 结构现在是 `imageResponseCard` 结构。


| V1 结构 | V2 结构 | 
| --- | --- | 
| dialogAction | sessionState.dialogAction | 
| dialogAction.type | sessionState.dialogAction.type | 
| DialogAction。 slotToElicit | sessionState.intent.dialog slotToElicit | 
| dialogAction.type.fulfillmentState | sessionState.intent.state | 
| dialogAction.message | 消息 | 
| dialogAction.message.contentType | messages[\*].contentType | 
| dialogAction.message.content | messages[\*].content | 
| dialogAction.responseCard | 消息 [\*]。 imageResponseCard | 
| dialogAction.responseCard.version | 不适用 | 
| dialogAction.responseCard.contentType | messages[\*].contentType | 
| dialogAction.responseCard.genericAttachments | 不适用 | 
| dialogAction.responseCard.genericAttachments[\*].title | 消息 [\*]。 imageResponseCard. 标题 | 
| dialogAction.responseCard.genericAttachments[\*].subTitle | 消息 [\*]。 imageResponseCard.subtitle | 
| dialogAction.responseCard.genericAttachments[\*].imageUrl | 消息 [\*]。 imageResponseCard.imageUrl | 
| dialogAction.responseCard.genericAttachments[\*].buttons | 消息 [\*]。 imageResponseCard. 按钮 | 
| dialogAction.responseCard.genericAttachments[\*].buttons[\*].value | 消息 [\*]。 imageResponseCard.buttons [\*] .value | 
| dialogAction.responseCard.genericAttachments[\*].buttons[\*].text | 消息 [\*]。 imageResponseCard.buttons [\*] .text | 
| DialogAction。 kendraQueryRequest有效载荷 | DialogAction。 kendraQueryRequest有效载荷 | 
| DialogAction。 kendraQueryFilter字符串 | DialogAction。 kendraQueryFilter字符串 | 

#### 意图和插槽
<a name="lambda-migrating-response-slots"></a>

作为 `dialogAction` 结构一部分的意图和插槽字段现在已成为 `sessionState` 结构的一部分。


| V1 结构 | V2 结构 | 
| --- | --- | 
| dialogAction.intentName | sessionState.intent.name | 
| dialogAction.slots | sessionState.intent.slots | 
| dialogAction.slots[\*].key | sessionState.intent.slots[\*].key | 
| dialogAction.slots[\*].value | sessionState.intent.slots[\*].value.interpretedValue | 
| 不适用 | sessionState.intent.slots[\*].value.shape | 
| 不适用 | sessionState.intent.slots[\*].values | 

#### 其他
<a name="lambda-migrating-response-other"></a>

`sessionAttributes` 结构现在是 `sessionState` 结构的一部分。`recentIntentSummaryReview` 结构已删除。


| V1 结构 | V2 结构 | 
| --- | --- | 
| sessionAttributes | sessionState.sessionAttributes | 
| recentIntentSummary查看 | 不适用 | 