如何使用 amazon lex 保持转换的上下文?

How to keep the context of a conversion with amazon lex?

如何在 amazon lex 中保留对话的上下文,我阅读了有关使用会话属性的内容,但没有找到任何示例。

下面是我希望对话如何进行的示例:

温度意图:

Human: What is the temperature in New York?

Bot: The temperature in New York is 3 degrees Celsius

湿度意图:

Human: What about the Humidity?

Bot: The Humidity in New York is 61%

当我询问湿度时,如何让机器人知道我在谈论纽约?

因为你没有指定你使用的是哪种语言,我将使用 C# 进行响应(我使用的是 C#)。

对话上下文是通过使用会话属性实现的。对于 C#,实现此目的的最简单方法是使用 AWS Lambda 函数。这是 Amazon 自己提供的示例(使用 Amazon Lex 的 BookTrip 蓝图):

        // Extract slots from Lex Event
        var slots = lexEvent.CurrentIntent.Slots;

        // Extract Session Attributes if they exist, otherwise create new Dictionary
        var sessionAttributes = lexEvent.SessionAttributes ?? new Dictionary<string, string>();

        Reservation lastConfirmedReservation = null;

        // if previous Reservation from Session Attributes exists
        if (slots.ContainsKey(LAST_CONFIRMED_RESERVATION_SESSION_ATTRIBUTE))
        {
            lastConfirmedReservation = DeserializeReservation(slots[LAST_CONFIRMED_RESERVATION_SESSION_ATTRIBUTE]);
        }

        string confirmationContext = sessionAttributes.ContainsKey("confirmationContext") ? sessionAttributes["confirmationContext"] : null;

        if (lastConfirmedReservation != null &&
                        string.Equals(lastConfirmedReservation.ReservationType, "Hotel", StringComparison.Ordinal))
        {
            // If there was a previous reservation - auto-populate to match
            sessionAttributes["confirmationContext"] = "AutoPopulate";
            return ConfirmIntent(
                                sessionAttributes,
                                lexEvent.CurrentIntent.Name,
                                new Dictionary<string, string>
                                {
                                    {PICK_UP_CITY_SLOT, lastConfirmedReservation.PickUpCity },
                                    {PICK_UP_DATE_SLOT, lastConfirmedReservation.CheckInDate },
                                    {RETURN_DATE_SLOT, DateTime.Parse(lastConfirmedReservation.CheckInDate).AddDays(int.Parse(lastConfirmedReservation.Nights)).ToUniversalTime().ToString(CultureInfo.InvariantCulture) },
                                    {CAR_TYPE_SLOT, null },
                                    {DRIVER_AGE_SLOT, null },
                                },
                                new LexResponse.LexMessage
                                {
                                    ContentType = MESSAGE_CONTENT_TYPE,
                                    Content = $"Is this car rental for your {lastConfirmedReservation.Nights} night stay in {lastConfirmedReservation.Location} on {lastConfirmedReservation.CheckInDate}?"
                                }
                              );
        }

了解其工作原理的最佳方法是查看您选择的语言的相关蓝图。希望这对你有所帮助!

编辑:Additional reading on the Blueprints that are available that may be useful for you. More有关在 Lex 中使用会话属性时信息流的详细信息。