我正在尝试创建一个lex机器人,让用户了解不同的选项。例如,它可以告诉用户三种可用的产品。我似乎找不到不使用lambda函数如何执行此操作的文档,也无法弄清楚如何将机器人自身的用户输入传递给lambda函数,以便使用简单的“ if / then ”,然后返回相应的消息。不得不使用lambda函数仅根据输入给出响应似乎过多,但是我陷入了困境。谢谢。
答案 0 :(得分:2)
为了进一步了解Lex机器人的工作方式,该服务使您能够定义语音(“ if”条件),以便智能地尝试执行模糊匹配以确定用户是否说了合适的话您定义的话语之一。您可以在AWS控制台examples of this are available here.
中声明和修改这些话语。此条件应用程序的“然后”部分是您定义用户输入与定义的话语(状态)匹配后发生的情况的地方,其中处理了一些基本计算,最容易以Lambda函数的形式进行。
对于简单的事情,例如在满足条件后返回静态文本/信息:
从lambda_handler
函数返回具有正确结果的字符串。准系统的伪代码Python实现如下所示:
# Here's an implementation where you have an intent of a user asking
# for more info about a particular object (info_type as defined in Lex console).
def lambda_handler(event, context):
info_name = event['info_type']
if info_name = 'business_hours':
return "Our business hours are 9am-5pm"
elif info_name = 'slogan':
return "We're customer obsessed"
else:
return "Sorry, we don't have info about this!"
根据您要如何设置应用程序,您可以决定如何在不同的话语之间以及在传递了prop数据的情况下将逻辑分开的方式。如果您有更复杂的查询,问题类型和计算将涉及所有这些因素,这些因素将决定构建Lex聊天机器人的最佳方式。
答案 1 :(得分:0)
Lex bot本身仅足以满足某些输入具有预定义响应的简单FAQ对话。但是您不能基于Lex捕获的插槽值设置响应。您可能会有一个非常有限的动态响应,其中这些插槽值只是放置在响应中(想象一个疯狂的Libs游戏),但仅此而已。
一旦您要基于用户输入创建真正的动态响应,则需要使用Lambda函数比较Lex请求并根据用户输入或广告位值构建适当的响应。
Amazon Lex - Using Lambda Functions
Create a Lambda Function (example Order Flowers)
Set the Lambda Function as the Lex Intent's Code Hook
一旦您设置了Lambda函数,并且Lex准备将经过处理的用户输入作为Lex Request(也称为“事件”)传递,那么您将必须密切注意此文档:
Lex Request and Response Format
传入的请求具有一致的格式,用于传递currentIntent
,sessionAttributes
,slots
,inputTranscript
(完整的用户输入)等等。它看起来可能很多,但是一旦将其解析为其主要组成部分,便可以非常轻松地使用它来构建动态响应。只要确保您完全遵循Lex请求和响应格式即可。
以下是启动Lambda函数(Node.js)的示例:
// The JSON body of the request is provided in 'event'.
// 'respondToLex' is the callback function used to return the Lex formatted JSON response
exports.handler = (event, context, respondToLex) => {
console.log( "REQUEST= "+JSON.stringify(event) ); //view logs in CloudWatch
// INCOMING VARIABLES FROM REQUEST EVENT
// -------------------------------------
var intentName = event.currentIntent.name;
var slots = event.currentIntent.slots
var sessionAttributes = event.sessionAttributes
var userInput = event.inputTranscript
// OUTGOING VARIABLES FOR RESPONSE
// -------------------------------
var responseMsg = "";
var responseType = "";
var slotToElicit = "";
var error = null;
var fulfillmentState = null;
// DETERMINE RESPONSE BASED ON INTENTS AND SLOT VALUES
// ---------------------------------------------------
if (intentName == "intentA") {
responseType = "Close";
responseMsg = "I am responding to intentA";
fulfillmentState = "fulfilled';
}
elseif (intentName == "intentB") {
if (slots["productChoice"] == null) {
responseMsg = "I can tell that productChoice slot is empty, so I should elicit for it here. Which product would you like? Hat or Shoes?";
responseType = "ElicitSlot";
slotToElicit = "productChoice";
}
else {
if (slots["productChoice"]=="hat") {
responseMsg = "I can tell you selected a hat, here is my dynamic response based on that slot selection.";
}
elseif (slots["productChoice"]=="shoes") {
responseMsg = "I can tell you selected shoes, here is my dynamic response based on that slot selection.";
}
}
}
else {
error = "Throw Error: Unknown Intent";
}
// CREATE RESPONSE BUILDER for each responseType (could turn into functions)
// -------------------------------------------------------------------------
if (responseType=="Close") {
var response = [
"sessionAttributes" = sessionAttributes,
"dialogAction" = [
"type" = responseType,
"fulfillmentState" = fulfillmentState,
"message" = [
"contentType" = "PlainText";
"content" = responseMsg,
]
]
];
}
elseif (responseType == "ElicitSlot) {
var response = [
"sessionAttributes" = sessionAttributes,
"dialogAction" = [
"type" = responseType,
"intentName" = intentName,
"slots" = slots
"slotToElicit" = slotToElicit,
"message" = [
"contentType" = "PlainText";
"content" = responseMsg,
]
]
];
}
responseJSON = JSON.stringify(response);
console.log("RESPONSE= "+ responseJSON); // view logs in CloudWatch
respondToLex(error, responseJSON);
}