

支援終止通知：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 會將 `currentIntent`和 `alternativeIntents` 結構取代為 `interpretations`結構。每個解釋都包含意圖、意圖的 NLU 可信度分數，以及選用的情緒分析。
+ Amazon Lex V2 會將 Amazon Lex V1 `activeContexts``sessionAttributes`中的 移至統一`sessionState`結構。此結構提供對話目前狀態的相關資訊，包括原始請求 ID。
+ Amazon Lex V2 不會傳回 `recentIntentSummaryView`。請改用 `sessionState`結構中的資訊。
+ Amazon Lex V2 輸入在 `bot` 屬性`localeId`中提供 `botId`和 。
+ 輸入結構包含 `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 | 
| activeContexts【\*】.timeToLive | sessionState.activeContexts[\*].timeToLive | 
| activeContexts【\*】.timeToLive.timeToLiveInSeconds | sessionState.activeContexts[\*].timeToLive.timeToLiveInSeconds | 
| activeContexts【\*】.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：\*】 | 
| recentIntentSummaryView | N/A | 

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

在 Amazon Lex V2 中，機器人和別名具有識別符。機器人 ID 是 Codehook 輸入的一部分。包含別名 ID，但不包含別名名稱。Amazon Lex V2 支援相同機器人的多個地區設定，因此包含地區設定 ID。


| V1 結構 | V2 結構 | 
| --- | --- | 
| 機器人 | 機器人 | 
| bot.name | bot.name | 
| N/A | bot.id | 
| bot.alias | N/A | 
| N/A | bot.aliasId | 
| bot.version | bot.version | 
| N/A | bot.localeId | 

#### 目前意圖
<a name="lambda-migrating-intent"></a>

`sessionState.intent` 結構包含作用中意圖的詳細資訊。Amazon Lex V2 也會傳回`interpretations`結構中所有意圖的清單，包括替代意圖。解譯清單中的第一個元素一律與 相同`sessionState.intent`。


| V1 結構 | V2 結構 | 
| --- | --- | 
| currentIntent | sessionState.intent OR 解釋【0】.intent | 
| currentIntent.name | sessionState.intent.name OR 解釋 【0】.intent.name | 
| currentIntent.nluConfidenceScore | interpretations【0】.nluConfidence.score | 

#### 對話方塊動作
<a name="lambda-migrating-dialog"></a>

`confirmationStatus` 欄位現在是 `sessionState` 結構的一部分。


| V1 結構 | V2 結構 | 
| --- | --- | 
| currentIntent.confirmationStatus | sessionState.intent.confirmationState OR interpretations【0】.intent.confirmationState | 
| N/A | sessionState.intent.state OR 解釋 【\*】.intent.state | 

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

`kendraResponse` 欄位現在是 `sessionState`和 `interpretations`結構的一部分。


| V1 結構 | V2 結構 | 
| --- | --- | 
| kendraResponse | sessionState.intent.kendraResponse OR 解釋 【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 OR interpretations【0】.intent.slots | 
| currentIntent.slots[\*].value | sessionState.intent.slots[\*].value.interpretedValue OR interpretations【0】.intent.slots【\*】.value.interpretedValue | 
| N/A | sessionState.intent.slots[\*].value.shape OR interpretations【0】.intent.slots【\*】.shape | 
| N/A | sessionState.intent.slots[\*].values OR interpretations【0】.intent.slots【\*】.values | 
| currentIntent.slotDetails | sessionState.intent.slots OR interpretations【0】.intent.slots | 
| currentIntent.slotDetails[\*].resolutions | sessionState.intent.slots[\*].resolvedValues OR interpretations【0】.intent.slots【\*】.resolvedValues | 
| currentIntent.slotDetails[\*].originalValue | sessionState.intent.slots[\*].originalValue OR 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 | 
| N/A | inputMode | 
| N/A | originatingRequestId | 

### 回應
<a name="migrating-lambda-response"></a>

下列欄位已在 Lambda 函數回應訊息格式中變更。

#### 作用中內容
<a name="lambda-migrating-resonse-context"></a>

`activeContexts` 結構已移至 `sessionState`結構。


| V1 結構 | V2 結構 | 
| --- | --- | 
| activeContexts | sessionState.activeContexts | 
| activeContexts【\*】.timeToLive | sessionState.activeContexts[\*].timeToLive | 
| activeContexts【\*】.timeToLive.timeToLiveInSeconds | sessionState.activeContexts[\*].timeToLive.timeToLiveInSeconds | 
| activeContexts【\*】.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.dialogAction.slotToElicit | 
| dialogAction.type.fulfillmentState | sessionState.intent.state | 
| dialogAction.message | messages | 
| dialogAction.message.contentType | messages【\*】.contentType | 
| dialogAction.message.content | messages【\*】.content | 
| dialogAction.responseCard | messages【\*】.imageResponseCard | 
| dialogAction.responseCard.version | N/A | 
| dialogAction.responseCard.contentType | messages【\*】.contentType | 
| dialogAction.responseCard.genericAttachments | N/A | 
| dialogAction.responseCard.genericAttachments[\*].title | messages【\*】.imageResponseCard.title | 
| dialogAction.responseCard.genericAttachments[\*].subTitle | messages【\*】.imageResponseCard.subtitle | 
| dialogAction.responseCard.genericAttachments[\*].imageUrl | messages【\*】.imageResponseCard.imageUrl | 
| dialogAction.responseCard.genericAttachments[\*].buttons | messages【\*】.imageResponseCard.buttons | 
| dialogAction.responseCard.genericAttachments[\*].buttons[\*].value | messages【\*】.imageResponseCard.buttons[\*].value | 
| dialogAction.responseCard.genericAttachments[\*].buttons[\*].text | messages【\*】.imageResponseCard.buttons[\*].text | 
| dialogAction.kendraQueryRequestPayload | dialogAction.kendraQueryRequestPayload | 
| dialogAction.kendraQueryFilterString | dialogAction.kendraQueryFilterString | 

#### 意圖和槽
<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 | 
| N/A | sessionState.intent.slots[\*].value.shape | 
| N/A | sessionState.intent.slots[\*].values | 

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

`sessionAttributes` 結構現在是`sessionState`結構的一部分。結構`recentIntentSummaryReview`已移除。


| V1 結構 | V2 結構 | 
| --- | --- | 
| sessionAttributes | sessionState.sessionAttributes | 
| recentIntentSummaryView | N/A | 