Search code examples
amazon-web-servicesamazon-lex

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


How can I keep the context of a conversation in amazon lex, I read on using session attributes but I did not find any samples.

Here is an example of how I want the conversation to go:

Temperature Intent:

Human: What is the temperature in New York?

Bot: The temperature in New York is 3 degrees Celsius

HumidityIntent:

Human: What about the Humidity?

Bot: The Humidity in New York is 61%

How can I make the bot know that I'm talking about New York when I ask for Humidity?


Solution

  • Since you didn't specify which language you're using, I'm gonna respond using C# (which is what I used).

    Conversation context is achieved via the use of Session Attributes. In the case of C#, the easiest way to achieve this is by using AWS Lambda functions. Here's the example provided by Amazon itself (using the BookTrip blueprint for Amazon Lex):

            // 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}?"
                                    }
                                  );
            }
    

    The best way to understand how this works is to go through the relevant Blueprint for your language of choice. Hopefully this helped you!

    Edit: Additional reading on the Blueprints that are available that may be useful for you. More details on the information flow when using Session Attributes in Lex.