Lambda 函數輸入事件和回應格式 - Amazon Lex V1

如果您使用的是 Amazon Lex V2,請參閱 Amazon Lex V2 指南

 

如果您使用的是 Amazon Lex V1,我們建議您將機器人升級到 Amazon Lex V2。我們不再向 V1 添加新功能,強烈建議所有新機器人使用 V2。

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

Lambda 函數輸入事件和回應格式

本節說明了 Amazon Lex 提供給 Lambda 函數的事件資料結構。請使用此資訊來剖析 Lambda 程式碼內的輸入內容。文中亦說明了 Amazon Lex 預期您的 Lambda 函數傳回的回應格式。

輸入事件格式

以下顯示傳遞給 Lambda 函數的 Amazon Lex 事件的一般格式。請在寫入 Lambda 函數時使用此資訊。

注意

即使 messageVersion 中沒有對應的變更,輸入格式也可能變更。如果出現新欄位,您的程式碼不應擲出錯誤。

{ "currentIntent": { "name": "intent-name", "nluIntentConfidenceScore": score, "slots": { "slot name": "value", "slot name": "value" }, "slotDetails": { "slot name": { "resolutions" : [ { "value": "resolved value" }, { "value": "resolved value" } ], "originalValue": "original text" }, "slot name": { "resolutions" : [ { "value": "resolved value" }, { "value": "resolved value" } ], "originalValue": "original text" } }, "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)" }, "alternativeIntents": [ { "name": "intent-name", "nluIntentConfidenceScore": score, "slots": { "slot name": "value", "slot name": "value" }, "slotDetails": { "slot name": { "resolutions" : [ { "value": "resolved value" }, { "value": "resolved value" } ], "originalValue": "original text" }, "slot name": { "resolutions" : [ { "value": "resolved value" }, { "value": "resolved value" } ], "originalValue": "original text" } }, "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)" } ], "bot": { "name": "bot name", "alias": "bot alias", "version": "bot version" }, "userId": "User ID specified in the POST request to Amazon Lex.", "inputTranscript": "Text used to process the request", "invocationSource": "FulfillmentCodeHook or DialogCodeHook", "outputDialogMode": "Text or Voice, based on ContentType request header in runtime API request", "messageVersion": "1.0", "sessionAttributes": { "key": "value", "key": "value" }, "requestAttributes": { "key": "value", "key": "value" }, "recentIntentSummaryView": [ { "intentName": "Name", "checkpointLabel": Label, "slots": { "slot name": "value", "slot name": "value" }, "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)", "dialogActionType": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close", "fulfillmentState": "Fulfilled or Failed", "slotToElicit": "Next slot to elicit" } ], "sentimentResponse": { "sentimentLabel": "sentiment", "sentimentScore": "score" }, "kendraResponse": { Complete query response from Amazon Kendra }, "activeContexts": [ { "timeToLive": { "timeToLiveInSeconds": seconds, "turnsToLive": turns }, "name": "name", "parameters": { "key name": "value" } } ] }

請注意以下有關事件欄位的更多資訊:

  • currentIntent – 提供意圖 nameslots slotDetailsconfirmationStatus 欄位。

     

    nluIntentConfidenceScore是 Amazon Lex 對當前意圖最符合用户當前意圖的信心。

     

    slots是槽名稱 (為意圖所設定) 與 Amazon Lex 在使用者對話中識別出的槽值的對應。槽值會保持 null,直到使用者提供值為止。

     

    輸入事件中的槽值可能不符合為槽所設定的其中一個值。例如,如果使用者對提示「您想要什麼顏色的車?」 和「比薩」,」Amazon Lex 將傳回「比薩」做為槽值。您的函數應該驗證值,以確保內容符合邏輯。

     

    slotDetails 會提供有關槽值的更多資訊。resolutions 陣列包含為槽所識別的其他值清單。每個槽最多可有 5 個值。

     

    originalValue 欄位包含使用者為槽輸入的值。當槽類型設定為傳回最常用的解析值來做為槽值時,originalValue 可能與 slots 欄位中的值不同。

     

    confirmationStatus 提供對確認提示 (如果有的話) 的使用者回應。例如,如果 Amazon Lex 詢問「您是否要訂購大型起士比薩? 」,根據使用者的回應,此欄位的值可以是Confirmed或者Denied。否則,此欄位的值會式 None

     

    如果使用者確認意圖、Amazon Lex 會將此欄位設定為Confirmed。如果使用者拒絕意圖、Amazon Lex 會將此值設定為Denied

     

    在確認回應中,使用者表達用語可能會提供槽更新。例如,使用者可能說「是,大小改為中型。」 在這種情況下,後續 Lambda 事件會有已更新的槽值PizzaSize設定為medium。Amazon Lex 設定confirmationStatusNone,因為使用者修改了一些槽資料,而需要使用 Lambda 函數執行使用者資料驗證。

     

  • 替代用途— 如果啟用置信度分數,Amazon Lex 將返回最多四個備選方法。每個意圖都包含一個分數,表明 Amazon Lex 對意圖是基於用户言論的正確意圖的信心程度。

     

    備選意圖的內容與currentIntent欄位。如需詳細資訊,請參閱 使用可信度分數

     

  • bot – 處理請求之機器人的相關資訊。

    • name – 處理請求的機器人名稱。

    • alias – 處理請求的機器人版本別名。

    • version – 處理請求的機器人版本。

     

  • userId— 此值是由用户端應用程式提供。Amazon Lex 傳遞給 Lambda 函數。

     

  • inputTranscript – 用來處理請求的文字。

    如果輸入是文字,inputTranscript 欄位會包含使用者輸入的文字。

     

    如果輸入是音訊串流,inputTranscript 欄位會包含從音訊串流擷取的文字。這是實際處理以識別意圖和槽值的文字。

     

  • invocationSource— 指明為什 Amazon Lex 叫 Lambda 函數,它會將此設定為下列其中一個值:

    • DialogCodeHook— Amazon Lex 會設定此值以引導 Lambda 函數來初始化函數和驗證使用者的資料輸入。

       

      當將意圖設定為呼叫 Lambda 函數做為初始化和驗證程式碼掛勾時,在了解意圖後,Amazon Lex 會呼叫每個使用者輸入 (表達用語) 上指定的 Lambda 函數。

      注意

      如果意圖不明確、Amazon Lex 便無法呼叫 Lambda 函數。

       

    • FulfillmentCodeHook— Amazon Lex 設定此值以引導 Lambda 函數來滿足意圖。

       

      如果將意圖設定為呼叫 Lambda 函數做為履行程式碼掛勾,Amazon Lex 會設定invocationSource只有在具有所有槽資料以滿足意圖後才會傳回此值。

       

    在您的意圖組態中,可以有兩個單獨的 Lambda 函數來初始化和驗證使用者資料,以及滿足意圖。您也可以使用一個 Lambda 函數來執行兩者。在這種情況下,Lambda 函數可以使用invocationSource值以遵循正確的程式碼路徑。

     

  • outputDialogMode— 對於每個使用者輸入,用户端會使用 Amazon Lex 中一個執行時間 API 操作,PostContent或者PostText。Amazon Lex 使用請求參數來判斷回應用户端的是文字還是語音,並相應設定此欄位。

     

    Lambda 函數可以使用此資訊來產生適當的訊息。例如,如果用户端預期語音回應,Lambda 函數可以傳回語音合成標記語言 (SSML),而非文字。

     

  • messageVersion— 事件的訊息,可識別進入 Lambda 函數的格式以及從 Lambda 函數的回應預期的格式。

    注意

    您在定義意圖時設定此值。在目前實作中,僅支援訊息版本 1.0。因此,主控台假設 1.0 的預設值,而且不會顯示訊息的版本。

  • sessionAttributes— 用户端在請求中傳送的應用程式特定的工作階段屬性。如果您希望 Amazon Lex 將其包含在對用户端的回應中,您的 Lambda 函數應該在回應中把這些傳回 Amazon Lex。如需詳細資訊,請參閱「」設定工作階段屬性

     

  • requestAttributes— 用户端在請求中傳送的請求特定的工作階段屬性。使用請求屬性來傳遞不需要在整個工作階段內保留的資訊。若無請求屬性,則數值將為 null。如需詳細資訊,請參閱「」設定請求屬性

     

  • recentIntentSummaryView— 關於意圖狀態的資訊。您可以查看最近使用之三個意圖的相關資訊。您可以使用此資訊來設定意圖中的值,或返回先前的意圖。如需詳細資訊,請參閱 使用 Amazon Lex API 管理工作階段

     

  • sentimentResponse— Amazon Comprehend 情緒分析最後表達用語的結果。您可以根據使用者表達的情緒,使用此資訊來管理機器人的對話流程。如需詳細資訊,請參閱 情緒分析

     

  • kendraResponse— 查詢 Amazon Kendra 索引的結果。只會出現在履行程式碼掛勾的輸入中,以及只有在意圖延伸 AMAZON.KendraSearchIntent 內建意圖時才會出現。欄位包含來自 Amazon Kendra 搜尋的整個回應。如需詳細資訊,請參閱 AMAZON.KendraSearchIntent

     

  • 活動文本— 一個或多個在與用户進行對話期間處於活動狀態的上下文。

    • TimeToLive— 上下文保持活動狀態的用户對話中的時間長度或轉彎次數。

    • 名稱— 上下文的名稱。

    • 參數鍵/值對的列表包含激活上下文的意圖中插槽的名稱和值。

    如需詳細資訊,請參閱 設定意圖上下文

回應格式

Amazon Lex 預期 Lambda 函數使用以下列格式回應:

{ "sessionAttributes": { "key1": "value1", "key2": "value2" ... }, "recentIntentSummaryView": [ { "intentName": "Name", "checkpointLabel": "Label", "slots": { "slot name": "value", "slot name": "value" }, "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)", "dialogActionType": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close", "fulfillmentState": "Fulfilled or Failed", "slotToElicit": "Next slot to elicit" } ], "activeContexts": [ { "timeToLive": { "timeToLiveInSeconds": seconds, "turnsToLive": turns }, "name": "name", "parameters": { "key name": "value" } } ], "dialogAction": { "type": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close", Full structure based on the type field. See below for details. } }

回應包含四個欄位。所以此sessionAttributesrecentIntentSummaryView,和activeContexts欄位是選用的,dialogAction欄位是必要的。dialogAction 欄位的內容取決於 type 欄位的值。如需詳細資訊,請參閱 dialogAction

sessionAttributes

選用。如果您包含 sessionAttributes 欄位,它可以留空。如果 Lambda 函數沒有傳回工作階段屬性,則最後已知sessionAttributes通過 API 或 Lambda 函數傳遞。如需詳細資訊,請參閱 PostContentPostText 操作。

"sessionAttributes": { "key1": "value1", "key2": "value2" }

recentIntentSummaryView

選用。如果包含,則設定一個或更多最近的意圖的值。您最多可以包含三個意圖的資訊。例如,您可以根據目前意圖所蒐集的資訊設定先前意圖的值。摘要中的資訊必須適用於該意圖。例如,意圖名稱必須是機器人中的意圖。如果要在摘要檢視中包含槽值,該槽必須存在於意圖中。如果沒有在回應中包含 recentIntentSummaryView,最近意圖的所有值仍會保持不變。如需詳細資訊,請參閱 PutSession 操作或 IntentSummary 資料類型。

"recentIntentSummaryView": [ { "intentName": "Name", "checkpointLabel": "Label", "slots": { "slot name": "value", "slot name": "value" }, "confirmationStatus": "None, Confirmed, or Denied (intent confirmation, if configured)", "dialogActionType": "ElicitIntent, ElicitSlot, ConfirmIntent, Delegate, or Close", "fulfillmentState": "Fulfilled or Failed", "slotToElicit": "Next slot to elicit" } ]

活動文本

選用。如果包含,則會設定一個或多個上下文的值。例如,您可以包含一個上下文,以使一個或多個意圖具有該上下文作為輸入符合對話的下一個回閤中的識別條件。

響應中未包含的任何活動上下文都會減少其生存時間值,並且在下一個請求中仍然處於活動狀態。

如果您為輸入事件中包含的上下文指定的生存時間為 0,則該上下一個請求將處於非活動狀態。

如需詳細資訊,請參閱 設定意圖上下文

dialogAction

必要。所以此dialogAction欄位會引導 Amazon Lex 執行後續動作,並說明在 Amazon Lex 傳回回回回應給用户端之後對使用者的期望。

type 欄位會指出後續動作,它也會判斷 Lambda 函數需要在dialogAction值。

  • Close— 通知 Amazon Lex 不預期使用者回應。例如,「您訂購的比薩已下單」不需要回應。

     

    fulfillmentState 欄位是必要的。Amazon Lex 使用此值來設置dialogState欄位PostContent或者PostText回應用程式。messageresponseCard 欄位是選用的。如果您不指定訊息,Amazon Lex 會使用針對意圖設定的再見訊息或後續追蹤訊息。

    "dialogAction": { "type": "Close", "fulfillmentState": "Fulfilled or Failed", "message": { "contentType": "PlainText or SSML or CustomPayload", "content": "Message to convey to the user. For example, Thanks, your pizza has been ordered." }, "responseCard": { "version": integer-value, "contentType": "application/vnd.amazonaws.card.generic", "genericAttachments": [ { "title":"card-title", "subTitle":"card-sub-title", "imageUrl":"URL of the image to be shown", "attachmentLinkUrl":"URL of the attachment to be associated with the card", "buttons":[ { "text":"button-text", "value":"Value sent to server on button click" } ] } ] } }
  • ConfirmIntent— 通知 Amazon Lex 預期使用者給予是或否的回答,以確認或拒絕目前的意圖。

     

    您必須包含 intentNameslots 欄位。針對指定的意圖所填入的每個槽,slots 欄位都必須包含項目。您不需要為未填入的槽 slots 欄位中包含項目。​ 如果意圖的 message 欄位為空值,您必須為納入 confirmationPrompt 欄位。的內容message欄 Lambda 會優先於confirmationPrompt在意圖中指定。此 responseCard 欄位為選用。

    "dialogAction": { "type": "ConfirmIntent", "message": { "contentType": "PlainText or SSML or CustomPayload", "content": "Message to convey to the user. For example, Are you sure you want a large pizza?" }, "intentName": "intent-name", "slots": { "slot-name": "value", "slot-name": "value", "slot-name": "value" }, "responseCard": { "version": integer-value, "contentType": "application/vnd.amazonaws.card.generic", "genericAttachments": [ { "title":"card-title", "subTitle":"card-sub-title", "imageUrl":"URL of the image to be shown", "attachmentLinkUrl":"URL of the attachment to be associated with the card", "buttons":[ { "text":"button-text", "value":"Value sent to server on button click" } ] } ] } }
  • Delegate— 引導 Amazon Lex 根據機器人組態選擇後續動作。如果回應不包含任何工作階段屬性,Amazon Lex 會保留現有的屬性。如果您希望槽值為空,您就不需在請求中包含槽欄位。如果您的履行函數沒有移除任何槽就傳回 DependencyFailedException 對話方塊動作,您將收到 Delegate 例外狀況。

    kendraQueryRequestPayloadkendraQueryFilterString 欄位是選用的,只有當意圖是從 AMAZON.KendraSearchIntent 內建意圖衍生而來時才會使用。如需詳細資訊,請參閱AMAZON.KendraSearchIntent

    "dialogAction": { "type": "Delegate", "slots": { "slot-name": "value", "slot-name": "value", "slot-name": "value" }, "kendraQueryRequestPayload": "Amazon Kendra query", "kendraQueryFilterString": "Amazon Kendra attribute filters" }
  • ElicitIntent— 通知 Amazon Lex 預期使用者以包含意圖的表達用語回應。例如,「我想要大型比薩。」,亦即指出 OrderPizzaIntent。「大型」的表達用語在另一方面,並不足以讓 Amazon Lex 推斷使用者的意圖。

     

    messageresponseCard 欄位是選用的。如果您未提供訊息,Amazon Lex 會使用其中一個機器人的澄清提示。如果未定義釐清提示,Amazon Lex 會傳回 400 錯誤請求例外狀況。

    { "dialogAction": { "type": "ElicitIntent", "message": { "contentType": "PlainText or SSML or CustomPayload", "content": "Message to convey to the user. For example, What can I help you with?" }, "responseCard": { "version": integer-value, "contentType": "application/vnd.amazonaws.card.generic", "genericAttachments": [ { "title":"card-title", "subTitle":"card-sub-title", "imageUrl":"URL of the image to be shown", "attachmentLinkUrl":"URL of the attachment to be associated with the card", "buttons":[ { "text":"button-text", "value":"Value sent to server on button click" } ] } ] } }
  • ElicitSlot— 通知 Amazon Lex 預期使用者在回應中提供槽值。

     

    intentNameslotToElicitslots 欄位是必要的。messageresponseCard 欄位是選用的。如果您不指定訊息,Amazon Lex 會使用為槽設定的其中一個槽引出提示。

    "dialogAction": { "type": "ElicitSlot", "message": { "contentType": "PlainText or SSML or CustomPayload", "content": "Message to convey to the user. For example, What size pizza would you like?" }, "intentName": "intent-name", "slots": { "slot-name": "value", "slot-name": "value", "slot-name": "value" }, "slotToElicit" : "slot-name", "responseCard": { "version": integer-value, "contentType": "application/vnd.amazonaws.card.generic", "genericAttachments": [ { "title":"card-title", "subTitle":"card-sub-title", "imageUrl":"URL of the image to be shown", "attachmentLinkUrl":"URL of the attachment to be associated with the card", "buttons":[ { "text":"button-text", "value":"Value sent to server on button click" } ] } ] } }