

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

# 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)。
+ **版本** – 處理請求的機器人版本。
+ **aliasId** – 建立機器人別名時指派給機器人別名的識別符。您可以在別名頁面的 Amazon Lex V2 ****主控台中查看機器人別名 ID。如果您在清單中看不到別名 ID，請選擇右上角的齒輪圖示，然後開啟**別名 ID**。
+ **aliasName** – 您給機器人別名的名稱。

## 解釋
<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
        }
    }
}
```

結構中的欄位如下所示：
+ **意圖** – 包含意圖相關資訊的結構。[意圖](lambda-common-structures.md#lambda-intent) 如需結構的詳細資訊，請參閱 。
+ **nluConfidence** – 表示 Amazon Lex V2 對意圖符合使用者意圖的可信度的分數。
+ **sentimentResponse** – 回應情緒的分析，其中包含下列欄位：
  + **情緒** – 指出表達用語的情緒是 `POSITIVE`、`NEUTRAL`、 `NEGATIVE`或 `MIXED`。
  + **sentimentScore** – 將每個情緒映射至數字的結構，指出 Amazon Lex V2 對表達該情緒的信心有多高。
+ **interpretationSource** – 指出插槽是由 Amazon Lex V2 或 Amazon Bedrock 解析。

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

如果 Lambda 函數將 `dialogAction`的 設定為 `sessionState` `Delegate`，則會顯示此欄位，並顯示 Amazon Lex V2 在對話中下一個步驟的提案。否則，下一個狀態取決於您在 Lambda 函數的回應中傳回的設定。只有在下列兩個陳述式都為 true 時，才會出現此結構：

1. `invocationSource` 值為 `DialogCodeHook`

1. `type` 的預測`dialogAction`為 `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` – 下一個動作是詢問使用者槽是否正確，以及意圖是否已準備好履行。
+ **意圖** – 機器人判斷使用者正在嘗試履行的意圖。[意圖](lambda-common-structures.md#lambda-intent) 如需結構的詳細資訊，請參閱 。
+ prompt**** – 包含欄位 的結構`attempt`，會映射至指定 Amazon Lex V2 提示使用者輸入下一個槽的次數的值。可能的值`Initial`適用於第一次嘗試`Retry1`，以及後續嘗試`Retry5`的 `Retry2``Retry3`、`Retry4`、、 和 。

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

包含用戶端在請求中傳送之請求特定屬性的結構。使用請求屬性來傳遞不需要在整個工作階段內保留的資訊。若無請求屬性，則數值將為 null。如需詳細資訊，請參閱[設定 Lex V2 機器人的請求屬性](context-mgmt-request-attribs.md)。

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

使用者與 Amazon Lex V2 機器人之間對話的目前狀態。[工作階段狀態](lambda-common-structures.md#lambda-session-state) 如需結構的詳細資訊，請參閱 。

## 轉錄
<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
        },
        ...
    }
}
```

這些欄位如下所述：
+ **轉錄** – Amazon Lex V2 認為可能符合使用者音訊表達用語的轉錄。
+ **transcriptionConfidence** – 表示 Amazon Lex V2 對意圖符合使用者意圖的可信度的分數。
+ **resolvedContext** – 包含 欄位 的結構`intent`，對應到表達用語相關的意圖。
+ **resolvedSlots** – 一種結構，其索引鍵是表達式解析的每個槽的名稱。每個槽名稱都會映射到包含該槽相關資訊的結構。[槽](lambda-common-structures.md#lambda-slot) 如需結構的詳細資訊，請參閱 。