

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

# AWS LambdaLex V2 的输入事件格式
<a name="lambda-input-format"></a>

将 Lambda 函数集成到您的 Amazon Lex V2 机器人的第一步是了解 Amazon Lex V2 事件中的字段，并从这些字段中确定在编写脚本时需要使用的信息。以下 JSON 对象显示了传递给 Lambda 函数的 Amazon Lex V2 事件的一般格式：

**注意**  
输入格式可以更改，但不必对 `messageVersion` 做出相应更改。您的代码不应在有新字段时引发错误。

```
{
    "messageVersion": "1.0",
    "invocationSource": "DialogCodeHook | FulfillmentCodeHook",
    "inputMode": "DTMF | Speech | Text",
    "responseContentType": "audio/mpeg | audio/ogg | audio/pcm | text/plain; charset=utf-8",
    "sessionId": string,
    "inputTranscript": string,
    "invocationLabel": string,
    "bot": {
        "id": string,
        "name": string,
        "localeId": string,
        "version": string,
        "aliasId": string,
        "aliasName": string
    },
    "interpretations": [
        {
            "interpretationSource": "Bedrock | Lex",
            "intent": {
                // see 意图 for details about the structure
            },
            "nluConfidence": number,
            "sentimentResponse": {
                "sentiment": "MIXED | NEGATIVE | NEUTRAL | POSITIVE",
                "sentimentScore": {
                    "mixed": number,
                    "negative": number,
                    "neutral": number,
                    "positive": number
                }
            }
        },
        ...
    ],
    "proposedNextState": {
        "dialogAction": {
            "slotToElicit": string,
            "type": "Close | ConfirmIntent | Delegate | ElicitIntent | ElicitSlot"
        },
        "intent": {
            // see 意图 for details about the structure
        },
        "prompt": {
            "attempt": string 
        }
    },
    "requestAttributes": {
        string: string,
        ...
    },
    "sessionState": {
        // see 会话状态 for details about the structure
    },
    "transcriptions": [
        {
            "transcription": string,
            "transcriptionConfidence": number,
            "resolvedContext": {
                "intent": string
            },
            "resolvedSlots": {
                slot name: {
                    // see 槽值 for details about the structure
                },
                ...
            }
        },
        ...
    ]
}
```

输入事件中的每个字段如下所述：

## messageVersion
<a name="lambda-input-messageversion"></a>

消息的版本，用于标识进入 Lambda 函数的事件数据的格式以及来自 Lambda 函数的响应的预期格式。

**注意**  
您在定义意图时可以配置此值。在当前实现中，Amazon Lex V2 仅支持消息版本 1.0。因此，控制台假定 1.0 的默认值，并且不显示消息版本。

## invocationSource
<a name="lambda-input-invocationsource"></a>

调用 Lambda 函数的代码挂钩。以下是可能的值：

`DialogCodeHook`：Amazon Lex V2 在用户输入内容后调用了 Lambda 函数。

`FulfillmentCodeHook`：Amazon Lex V2 在填充了所有必需的槽位后调用了 Lambda 函数并且意图已准备好履行。

## inputMode
<a name="lambda-input-inputmode"></a>

用户言语模式。可能的值如下所示：

`DTMF`：用户通过按键键盘（双音多频）输入言语。

`Speech`：用户说出了言语。

`Text`：用户键入了言语。

## responseContentType
<a name="lambda-input-responsecontenttype"></a>

机器人对用户的响应模式。`text/plain; charset=utf-8` 表示写入了最后一个言语，而以 `audio` 开头的值表示说出了最后一个言语。

## sessionId
<a name="lambda-input-sessionid"></a>

用于对话的字母数字会话标识符。

## inputTranscript
<a name="lambda-input-inputtranscript"></a>

用户输入的转录。
+ 对于文本输入，表示用户键入的文本。对于 DTMF 输入，表示用户输入的密钥。
+ 对于语音输入，这是 Amazon Lex V2 将用户言语转换成的文本，以便调用意图或填补槽位。

## invocationLabel
<a name="lambda-input-invocationlabel"></a>

该取值表示调用 Lambda 函数的响应。您可以为初始响应、槽位和确认响应设置调用标签。

## 自动程序
<a name="lambda-input-bot"></a>

有关处理请求的机器人的信息，包括以下字段：
+ **id**：创建机器人时分配给机器人的标识符。您可以在 Amazon Lex V2 控制台的机器人**设置**页面上看到该机器人 ID。
+ **name**：创建机器人时赋予机器人的名称。
+ **localeId**：用于机器人的区域设置的标识符。有关区域设置列表，请参阅 [Amazon Lex V2 支持的语言和区域设置](how-languages.md)。
+ **version**：用于处理请求的机器人的版本。
+ **aliasId**：创建机器人别名时，为其分配的标识符。您可以在 Amazon Lex V2 控制台的**别名**页面上看到该机器人别名 ID。如果列表中未显示别名 ID，请选择右上角的齿轮图标并打开**别名 ID**。
+ **aliasName**：为机器人提供的别名。

## interpretations
<a name="lambda-input-interpretations"></a>

Amazon Lex V2 认为可能与用户言语相匹配的意图相关信息的列表。每一项都是一个结构，提供有关言语与意图的匹配的信息，格式如下：

```
{
    "intent": {
        // see 意图 for details about the structure
    },
    "interpretationSource": "Bedrock | Lex",
    "nluConfidence": number,
    "sentimentResponse": {
        "sentiment": "MIXED | NEGATIVE | NEUTRAL | POSITIVE",
        "sentimentScore": {
            "mixed": number,
            "negative": number,
            "neutral": number,
            "positive": number
        }
    }
}
```

结构中的字段如下：
+ **intent**：包含意图相关信息的结构。有关结构的详细信息，请参阅[意图](lambda-common-structures.md#lambda-intent)。
+ **nluConfidence**：该分数表明 Amazon Lex V2 对意图与用户意图相匹配的信心程度。
+ **sentimentResponse**：对响应情绪的分析，包含以下字段：
  + **sentiment**：表示言语的情绪，取值为 `POSITIVE`、`NEGATIVE`、`NEUTRAL` 或 `MIXED`。
  + **sentimentScore**：将每种情绪映射到一个数字的结构，该数字表明 Amazon Lex V2 对言语传达这种情绪的信心程度。
+ **InterpretationSourc** e — 表示插槽是由 Amazon Lex V2 还是由 Amazon Bedrock 解析的。

## proposedNextState
<a name="lambda-input-proposednextstate"></a>

如果 Lambda 函数将 `sessionState` 的 `dialogAction` 设置为 `Delegate`，则会出现此字段，并显示 Amazon Lex V2 对下一步对话的建议。否则，下一个状态取决于您在 Lambda 函数响应中返回的设置。只有满足以下两个语句时，此结构才存在：

1. `invocationSource` 的值为 `DialogCodeHook`。

1. `dialogAction` 的预测 `type` 是 `ElicitSlot`。

您可以通过此信息在对话的正确位置添加 `runtimeHints`。有关更多信息，请参阅 [在对话中使用运行时提示改善对槽位值的识别](using-hints.md)。`proposedNextState` 是包含以下字段的结构：

`proposedNextState` 的结构如下所示：

```
"proposedNextState": {
    "dialogAction": {
        "slotToElicit": string,
        "type": "Close | ConfirmIntent | Delegate | ElicitIntent | ElicitSlot"
    },
    "intent": {
        // see 意图 for details about the structure
    },
    "prompt": {
        "attempt": string 
    }
}
```
+ **dialogAction**：包含有关 Amazon Lex V2 提出的下一步行动的信息。结构中的字段如下：
  + **slotToElicit**— 按照 Amazon Lex V2 的提议，接下来要引出的时段。仅当 `type` 的值为 `ElicitSlot` 时才显示此字段。
  + **type**：Amazon Lex V2 提出的下一步对话。以下是可能的值：

    `Delegate`：Amazon Lex V2 决定下一步操作。

    `ElicitIntent`：下一步操作是引发用户的意图。

    `ElicitSlot`：下一步操作是从用户引发槽位值。

    `Close`：结束意图履行流程并表示用户不会做出回应。

    `ConfirmIntent`：下一步操作是询问用户槽位是否正确以及意图是否已准备履行。
+ **intent**：机器人已确定用户正在尝试履行的意图。有关结构的详细信息，请参阅[意图](lambda-common-structures.md#lambda-intent)。
+ **prompt**：包含字段 `attempt` 的结构，该字段映射到一个值，该值指定 Amazon Lex V2 提示用户进入下一槽位的次数。可能的值为第一次尝试的 `Initial`，以及后续尝试的 `Retry1`、`Retry2`、`Retry3`、`Retry4` 和 `Retry5`。

## requestAttributes
<a name="lambda-input-requestattributes"></a>

一种结构，其中包含客户端在请求中发送的请求特定的属性。可以使用请求属性传递不需要在整个会话中保留的信息。如果没有请求属性，该值将为空。有关更多信息，请参阅 [设置 Lex V2 机器人的请求属性](context-mgmt-request-attribs.md)。

## sessionState
<a name="lambda-input-sessionstate"></a>

用户与您的 Amazon Lex V2 机器人之间对话的当前状态。有关结构的详细信息，请参阅[会话状态](lambda-common-structures.md#lambda-session-state)。

## transcriptions
<a name="lambda-input-transcriptions"></a>

Amazon Lex V2 认为可能与用户言语匹配的转录的列表。有关更多信息，请参阅 [使用语音转录置信度分数来改善与 Lex V2 机器人的对话](using-transcript-confidence-scores.md)。每一项都是一个对象，具有以下格式，包含有关一个可能转录的信息：

```
{
    "transcription": string,
    "transcriptionConfidence": number,
    "resolvedContext": {
        "intent": string
    },
    "resolvedSlots": {
        slot name: {
            // see 槽值 for details about the structure
        },
        ...
    }
}
```

这些字段如下所述：
+ **transcription**：Amazon Lex V2 认为可能与用户的音频话言语匹配的转录。
+ **transcriptionConfidence**：该分数表明 Amazon Lex V2 对意图与用户意图相匹配的信心程度。
+ **resolvedContext**：一种包含字段 `intent` 的结构，该字段映射到言语相关意图。
+ **resolvedSlots**：一种结构，其键是通过言语解析的每个槽位的名称。每个槽位名称都映射到一个包含该槽位相关信息的结构。有关结构的详细信息，请参阅[槽值](lambda-common-structures.md#lambda-slot)。