Amazon Lex is an AWS service for building conversational interfaces for applications using voice and text. With Amazon Lex, the same conversational engine that powers Amazon Alexa is now available to any developer, enabling you to build sophisticated, natural language chatbots into your new and existing applications. Amazon Lex provides the deep functionality and flexibility of natural language understanding (NLU) and automatic speech recognition (ASR) so you can build highly engaging user experiences with lifelike, conversational interactions, and create new categories of products.

CSML Studio is a chatbot development powerhouse, leveraging the open-source CSML programming language and created to answer the needs of enterprise chatbot development factories. It merges the capabilities of the best-in-class Natural Language Processing services such as Amazon Lex with a simple yet extremely scalable low-code approach to designing, developing, deploying and maintaining rich conversational experiences with more powerful and more integrated chatbots. CSML Studio includes integrations with hundreds of business applications such as Service Now, Zapier, Google Sheets, Gorgias, SAP, Google Calendar, Airtable, Amplitude Analytics, Zendesk, Box, Clickup...

In this article, we are going to demonstrate how to add Natural Language Understanding with Lex to a simple CSML customer support chatbot to make it even more interactive and useful to its users!

Step 0. Requirements

To begin with, you will need an AWS account, and a IAM user with permissions allowing access to Lex, with the AmazonLexRunBotsOnly managed policy. Retrieve the ACCESS_KEY_ID and SECRET_ACCESS_KEY for this user and you are all set! Be aware that there is a small cost associated with using Amazon Lex, but for this demonstration it should be negligible as we are only going to send a few requests to the Lex service.

You will also need a free account on CSML Studio to create your CSML chatbot.

Step 1. Creating the conversation

Let's start with a simple customer support chatbot built with CSML, for users that are having issues with one of their devices (phone, computer...). When the user arrives on this chatbot, they are greeted with a few action choices, and guided in their decisions with buttons until the ticket is properly qualified and then created in a ticketing service. CSML Studio has native integrations with many ticketing service providers, such as Service Now, Gorgias, Zendesk... and we are adding more every month.

The code for this bot is available on github for you to import directly in CSML Studio and contains these 4 flows:

  • Welcome, to greet the user when they arrive on the chatbot,
  • MainMenu, where the user can select what they want to do next,
  • Default, a catch-all flow that will handle paths where the bot does not know what to do,
  • CreateSupportTicket, which handles the main support ticket creation logic.

A user journey would usually take them from the Welcome flow to the MainMenu, where they would select an option to create a support ticket to get their computer or smartphone fixed. This is already a complete user flow, which guides the user well, contains rich content (images, gifs, emojis, buttons, carousels...), is integrated with 3rd-party ticketing software, and can be deployed instantly on several channels both consumer-facing (Messenger, Whatsapp, website plugin...) and for internal use (Slack, MS Teams, Workplace...).

Let's see how we can improve this user flow by adding Lex's Natural Language Processing capabilities into the mix!

Step 2. Creating the Amazon Lex chatbot

Our goal is to make it even easier for our users to reach the point where they can actually create the ticket itself. Ideally, it would be great if they could just say something like "I have an issue with my computer" and the bot would not only immediately recognize that they want to create a ticket, but also already know that the device they are having a problem with is specifically a computer (and not a phone or a toaster).

To help you get started quickly with Amazon Lex, we put together a simple chatbot that you can simply import in your account. To import the bot, visit the Lex console, click on Actions > Import, and import this zip file. One of the great things about Lex chatbots is that you can simply export and import full bot definitions, complete with intents and slot types in a compact zip format!

The Editor view of the Lex chatbot that we are going to use

Once the bot is correctly imported, you need to build it and publish it to an alias, and you are done!

Step 3. Connecting Amazon Lex and CSML Studio

Back to CSML Studio, go to your bot's settings > NLP > Amazon Lex. Fill in the form with your own information:

And now you are done! Every request on your chatbot will be sent for analysis to Amazon Lex before it is processed by CSML Studio, greatly enhancing what you can do with your chatbot. Let's see how!

Step 4. Improving a simple rule-based chatbot with Lex NLP

Our goal is to help a user get to the create_ticket step of the CreateSupportTicket flow as quickly/easily as possible. One way to do that is by using the buttons as demonstrated in the video above, but what if the user were to simply say "I have an issue with my computer"?

Using NLP as a flow command

In Lex, such a sentence would be detected as the declare_issue intent, with device_type slot set to computer:

{
  "botVersion": "$LATEST",
  "dialogState": "ReadyForFulfillment",
  "intentName": "declare_issue",
  "message": null,
  "messageFormat": null,
  "nluIntentConfidence": {
    "score": 0.95
  },
  "responseCard": null,
  "sentimentResponse": null,
  "sessionAttributes": {},
  "sessionId": "2020-11-03T12:24:55.860Z-UNJLygFj",
  "slotToElicit": null,
  "slots": {
    "device_type": "computer"
  }
}

When running the same request in CSML Studio, we do get an event set to "declare_issue", overriding the text value that the user gave.

This allows us to configure our bot to use declare_issue as a command to trigger the CreateSupportTicket flow so that whenever a user says something like "I have an issue with my computer, they are directly sent to this part of the bot! This can be done very easily in the AI Rules section:

Now, it would be even more useful to also catch the type of device they are having an issue with, so they don't have to repeat themselves. As it happens, after it was handled by Lex and an intent was found, the event object now contains a lot of extra information, that you can access with the helper method event.get_content():

{
    "text": "I have an issue with my computer",
    "_nlp_provider": {
        "bot_name": "TicketingBot",
        "provider": "lex"
    },
    "payload": "declare_issue",
    "_nlp_result": {
        "message": null,
        "sessionId": "2020-11-03T12:31:11.490Z-XYDEeBVS",
        "slots": {
            "device_type": "computer"
        },
        "messageFormat": null,
        "dialogState": "ReadyForFulfillment",
        "slotToElicit": null,
        "intentName": "declare_issue"
    }
}

Sure enough,  event._nlp_result.slots.device_type is set to "computer"! Let's use this in our CSML script, by replacing the start step of the CreateSupportTicket flow with the following:

start:
  // the user's input was parsed with Lex and a device_type was found
  // in their sentence, so we can skip the selection step and directly
  // ask them for the next section of the ticket creation
  if (event._nlp_result.slots.device_type) {
    remember device_type = event._nlp_result.slots.device_type
    say "OK! I understand that you are having issues with your {{device_type}}"
    goto get_email
  }

  say "OK, let's see how we can help you with your issue!"
  goto select_device_type

Adding these few lines has a tremendous effect on the user experience:

  • if the user directly states that they have an issue, the bot understands they want to create a ticket,
  • and when doing so, if they mention what they are having an issue with, the bot can even skip a few steps!

Let's see how it affects the user experience:

Using NLP inside flows

Another way to use an intent is inside a flow. At the beginning of the user interaction with the bot, the user is asked for their name. They could just say "Jane" directly, or they could say "My name is Jane". By using NLP, we can isolate the name from the rest of the sentence and get the best of both worlds. The Lex bot is already setup with a ready-to-use intent for just this use case, my_name_is:

To use this in the Welcome flow, we can simply change the remember firstname = event line with the following:

// the NLP detected the `my_name_is` intent and a username value
if (event == "my_name_is" && event._nlp_result.slots.username) {
  remember firstname = event._nlp_result.slots.username
}
// otherwise, just use the user's input directly
else {
  remember firstname = event
}

Now, rebuild the bot, clear the memory and try again:

Conclusion

As you can see, using Lex with CSML can tremendously improve the user experience of your chatbots, without changing the experience of users that prefer clicking on buttons and selecting options. You can use as much or as little NLP as you want, and the conversation will adapt accordingly! CSML continue will handle the conversational experience, the integration with other systems, the long-term memory, and the deployment to any conversation channel, while Lex will be able to focus on adding a whole set of Natural Language Understanding capabilities to your chatbot, within minutes.