Lambda函数将Lex引向新意图

问题描述 投票:2回答:2

我正在尝试使用lambda函数根据传入槽的值将lex发送到新的intent:

像这样

 public override LexResponse Process(LexEvent lexEvent, ILambdaContext context)
    {
        var slots = lexEvent.CurrentIntent.Slots;
        var sessionAttributes = lexEvent.SessionAttributes ?? new Dictionary<string, string>();
        DesiredVehicleYesNo type;
        if (slots.ContainsKey("DesiredVehicleYesNo"))
        {
            Enum.TryParse(slots["DesiredVehicleYesNo"], out type);
        }
        else
        {
            type = DesiredVehicleYesNo.Null;
        }

        switch (type)
        {
            case DesiredVehicleYesNo.YES:
                Dictionary<string, string> s = new Dictionary<string, string>();
                s.Add("DesiredVehicle", null);
                //return ConfirmIntent(sessionAttributes, "DesiredVehicleYes", s, new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });
                //return ElicitSlot(sessionAttributes,"DesiredVehicleYes",null,"DesiredVehicle", new LexResponse.LexMessage() { Content = "That's great! Let's get started.", ContentType = MESSAGE_CONTENT_TYPE });

            case DesiredVehicleYesNo.NO:
                return ConfirmIntent(sessionAttributes, "DesiredVehicleNo", new Dictionary<string,string>(), new LexResponse.LexMessage() { Content = "Well, that's ok, I can help you choose", ContentType = MESSAGE_CONTENT_TYPE });

        }

我只是不确定我应该使用哪种返回类型? ConfirmIntent,ElicitSlot,ElicitIntent?此外,我确实需要传回插槽,我希望新的意图使用它自己的提示来填充与该意图相关的插槽。

谢谢

amazon-web-services amazon-lex
2个回答
2
投票

你应该使用ConfirmIntent并提供你要切换到的意图的intentNameslots

Lex Response Format Docs

ConfirmIntent - 通知Amazon Lex,用户应该给出是或否答案以确认或拒绝当前意图。 您必须包含intentName和slots字段。 slots字段必须包含为指定intent配置的每个插槽的条目。如果插槽的值未知,则必须将其设置为null。如果intent的confirmationPrompt字段为null,则必须包含消息字段。如果同时指定message字段和confirmationPrompt字段,则响应将包含confirmationPrompt字段的内容。 responseCard字段是可选的。

因此,您可以提供自己的消息,但请确保将其写为是/否问题。因为ConfirmIntent会期待用户的回答是/否。

此方法将始终触发您在intentName中提供的意图。所以你必须在那里处理用户的响应。 如果用户说“是”,那么confirmationStatus将保持值Confirmed。 如果用户说“不”,那么confirmationStatus将保持值Denied

确保您传回的插槽对于该新意图是正确的。你可以预先填写它们以使用用户已经给你的东西;或设置为null以允许新意图要求用户再次填充这些插槽。


0
投票

您可以像Jay建议的那样使用ConfirmIntent,但是如果您不想向用户提示任何内容,则有一种实现此目的的hacky方法:

  • 允许访问调用lambda函数到你的调用lambda函数,即intent-A的lambda函数
  • 获取intent-B的后端lambda函数的名称
  • 使用boto3调用所有输入的lambda函数
  • 响应将在响应对象的“Payload”键中
  • 使用read()方法获取响应
  • 在['dialogAction'] ['message'] ['content']中获取实际输出
  • 使用默认的close()方法返回

以下是相同的示例代码:

import boto3

client = boto3.client('lambda')
data = {'messageVersion': '1.0', 'invocationSource': 'FulfillmentCodeHook', 'userId': '###', 
        'sessionAttributes': {}, 'requestAttributes': None, 
        'bot': {'name': '###', 'alias': '$LATEST', 'version': '$LATEST'}, 
        'outputDialogMode': 'Text', 
        'currentIntent': {'name': '###', 'slots': {'###': '###'}, 
        'slotDetails': {'###': {'resolutions': [], 'originalValue': '###'}}, 
        'confirmationStatus': 'None'}, 
        'inputTranscript': '###'}
response = client.invoke(
    FunctionName='{intent-B lambda function}',
    InvocationType='RequestResponse',
    Payload=json.dumps(data)
)
output = json.loads(response['Payload'].read())['dialogAction']['message']['content']

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.