A previous version of this article was published on Medium.
Though conversational AI has been around since the 1960s, it’s experiencing a renewed focus in recent years.While we’re still in the early days of the design and development of intelligent conversational AI, Google quite rightly announced that we were moving from a mobile-first to an AI-first world, where we expect technology to be naturally conversational, thoughtfully contextual, and evolutionarily competent. In other words, we expect technology to learn and evolve.
Most chatbots today can handle simple questions and respond with prebuilt responses based on rule-based conversation processing. For instance, if user says X, respond with Y; if user says Z, call a REST API, and so forth. However, at this juncture, we expect more from conversation. We want contextual assistants that transcend answering simple questions or sending push notifications. In this series, I’ll walk you through the design, development and deployment of a contextual AI assistant that designs curated travel experiences.
First, let’s talk about the maturity levels of contextual assistants as explained by their capabilities:
Conversational AI Maturity Levels
Level 1 Maturity: At this level, the chatbot is essentially a traditional notification assistant; it can answer a question with a pre-built response. It can send you notifications about certain events or reminders about things in which you’ve explicitly expressed interest. For instance, a level 1 travel bot can provide a link for you to book travel.
Level 2 Maturity: At this level, the chatbot can answer FAQs but is also capable of handling a simple follow up.
Level 3 Maturity: At this level, the contextual assistant can engage in a flexible back-and-forth with you and offer more than prebuilt answers because it knows how to respond to unexpected user utterances. The assistant also begins to understand context at this point. For instance, the travel bot will be able to walk you through a few popular destinations and make the necessary travel arrangements.
Level 4 Maturity: At this level, the contextual assistant has gotten to know you better. It remembers your preferences, and can offer personalized, contextualized recommendations or “nudges” to be more proactive in its care. For instance, the assistant would proactively reach out to order you a ride after you’ve landed.
Level 5 and beyond: At this level, contextual assistants are able to monitor and manage a host of other assistants in order to run certain aspects of enterprise operations. They’d be able to run promotions on certain travel experiences, target certain customer segments more effectively based on historical trends, increase conversion rates and adoption, and so forth.
Conversational AI has its roots in NLP
Natural language processing (NLP) is an application of artificial intelligence that enables computers to process and understand human language. Recent advances in machine learning, and more specifically its subset, deep learning, have made it possible for computers to better understand natural language. These deep learning models can analyze large volumes of text and provide things like text summarization, language translation, context modeling, and sentiment analysis.
Natural language understanding (NLU) is a subset of NLP that turns natural language into structured data. NLU is able to do two things : intent classification and entity extraction.
When we read a sentence, we immediately understand the meaning or intent behind that sentence. An intent is something that a user is trying to convey or accomplish. Intent classification is a two-step process. First, we feed an NLU model with labeled data that provides the list of known intents and example sentences that correspond to those intents. Once trained, the model is able to classify a new sentence that it sees into one of the predefined intents. Entity extraction is the process of recognizing key pieces of information in a given text. Things like time, place and name of a person all provide additional context and information related to an intent. Intent classification and entity extraction are the primary drivers of conversational AI.
For the purposes of this article, we will use the Rasa, an open-source stack that provides tools to build contextual AI assistants. There are two main components in the Rasa stack that will help us build a travel assistant , Rasa NLU and Rasa Core.
Rasa NLU provides intent classification and entity extraction services. Rasa Core is the main framework of the stack that provides conversation or dialogue management backed by machine learning. Assuming for a second that the NLU and Core components have been trained, let’s see how the Rasa stack works.
Let’s use this sample dialogue:
The NLU component identifies that the user intends to engage in vacation-based travel (intent classification) and that he or she is the only one going on this trip (entity extraction).
The Core component is responsible for controlling the conversation flow. Based on the input from NLU, the current state of the conversation and its trained model, the Core component decides on the next best course of action, which could be sending a reply to the user or taking an action. Rasa’s ML-based dialogue management is context-aware and doesn’t rely on hard-coded rules to process conversation.
Installation and Setup
Now, let’s install Rasa and start creating the initial set of training data for our travel assistant.
Rasa can be set up in two ways. You can either install the Rasa stack using Python/pip on your local machine, or you can use Docker to setup the Rasa stack using preconfigured Docker images. We’re going to install the Rasa stack using Python and pip.
If you don’t have Python installed on your machine, you can use Anaconda to set it up. Note that you need Python 3.6.x version to run the Rasa stack. The latest version of Python (3.7.x at the time of this post) is not fully compatible.
Run the following command to install Rasa core:
Install Rasa NLU by running this command:
Now let’s scaffold our application by cloning the starter pack provided by Rasa:
Once cloned, run these commands to install the required packages and the spaCy English language model for entity extraction.
At this point, we have everything we need to begin developing our travel assistant. Let’s take a look at the folder structure and the files that were created during the scaffolding process.
The “domain.yml” file describes the travel assistant’s domain. It specifies the list of intents, entities, slots, and response templates that the assistant understands and operates with. Let’s update the file to add an initial set of intents corresponding to our travel domain. Here’s a snippet:
The “data/nlu_data.md” file describes each intent with a set of examples that are then fed to Rasa NLU for training. Here’s a snippet:
The “data/stories.md” file provides Rasa with sample conversations with users that the travel assistant can use to train its dialog management model. Here’s a snippet:
Rasa provides a lot of flexibility in terms of configuring the NLU and Core components. For now, we’ll use the default “nlu_config.yml” for NLU and “policies.yml” for the Core model.
Run the following command to train Rasa NLU:
Run the following command to train Rasa Core:
We can now run the server to test Rasa through the command line:
The Rasa stack provides hooks to connect our assistant to various front-end channels like Slack and Facebook. Let’s configure and deploy our travel assistant to Slack.
Let’s start by creating a new app in Slack:
- Under features, go to “OAuth & Permissions” then add permission scopes like “chat:write:bot” and save your changes.
- Go to “Bot Users” then add a bot user and save your changes.
- Go back to “OAuth & Permissions” and install the app to your workspace.
- Copy the “Bot User OAuth Access Token” under tokens for your workspace.
- Back to the “travel-bot” folder on your local machine, create a new “credentials.yml” file. Paste the token into the file so it looks like this:
We need to pass these credentials to Rasa. To make our lives easier, let’s update the “Makefile” under the “travel-bot” folder to add a new command called “start.”
Caution: Based on your requirements, update ‘cors’ settings before deploying your bot to production.
Let’s start the server by calling:
Our travel assistant is now running on the local port 5005 and it’s configured to talk to Slack via REST APIs. We need to make these endpoints reachable to the outside world. Let’s use ngrok to make our server available to Slack. ngrok helps in setting up a secure tunnel to our local server for quick development and testing. Once you have ngrok installed, run the following command in a new command line terminal:
Make a note of the “https” URL provided by ngrok. Back in the Slack app management UI:
- Enable “Event Subscriptions.” In the request URL textbox, paste your ngrok URL and add “/webhooks/slack/webhook” to the end of that URL. Complete the event subscription by subscribing to bot events like “message.im” and save your changes.
- Optionally, enable “Interactive Components” and pass the same request URL that you used in the previous step. Be sure to save your changes.
At this point we have fully configured our bot assistant to interact with Slack. To test the travel assistant, slack your newly created travel bot.
In the next part of the series, we’ll deep dive into our NLU pipeline, custom components like Google’s BERT and Recurrent Embedding Dialogue Policy (REDP), and approach concepts like context, attention, and non-linear conversation.