How to build a contextual assistant using RASA Forms

A super simple guide to creating an information retrieval ChatBot with forms integration.

Amal Menzli
Towards Data Science

--

We know very well that the ChatBots are the future of this generation and I believe that everyone must know the basics of how Chatbots work. This is exactly the aim of my article and I hope that after reading it you can build your own Bot.

Introduction

You can find many tutorials on RASA that are using RASA APIs to build a Chatbot. But I haven’t recently found anything that talks in detail and simplifies my research like installation problems which for me was the most crucial part.

In this article, I will be happy to share with you my journey building a Chatbot with Rasa framework in which we will collect some information from the user. We will start by the installation part until speaking with the Bot.

Table of contents

  1. Information Retrieval ChatBots
  2. Overview of the RASA framework
  3. Installing Rasa and its Dependencies
  4. Build ChatBot that receives user information using forms

Information Retrieval Chatbots

A chatbot is a computer program or an artificial intelligence which conducts a conversation via auditory or textual methods. Such programs are often designed to convincingly simulate how a human would behave as a conversational partner. Chatbots are typically used in dialog systems for various practical purposes including customer service or information acquisition. Some chatbots use sophisticated natural language processing systems -but many simpler systems scan for keywords within the input- then pull a reply with the most matching keywords, or the most similar wording pattern, from a database. For more details about the Chatbot definition and for more basic understanding like What are intents? and Entities? You can check my previous article about chatbots in this link.

Nowadays, most of the chatbots are used for one specific application which is collecting a few pieces of information from a user in order to offer something (book a restaurant, search a database,etc.). This is also called slot filling. This class of bots is designed to provide human-like answers without human intervention. Here, the system tries to understand what question you are trying to ask. And answer you by giving such Form to fill to save your specific information.

Example of Registration Form in website: image source

In this article, we will build a chatbot to fill such a simple form for learning purposes. That’s why you need to follow me to the end.

Overview of the RASA framework

RASA is an open-source Conversational AI framework. What I like the most about this framework, is data privacy, where you don’t need to worry about putting your data in someone else’s cloud-like Microsoft Luis or Amazon Lex. For now, there are 3 types of AI assistants:

  • Notifications Assistants: Capable of sending simple notifications, like a text message, push notification.
  • FAQ Assistants: The most common type of assistant today. It can answer simple questions, like FAQs.
  • Contextual Assistants: can handle any user goal gracefully and help accomplish it as best as possible. Which means that it is capable of understanding and responding to different and unexpected inputs.

RASA framework has three major components that work together to create contextual assistants:

  • RASA NLU: is Natural Language Understanding. Suppose the user says “I want to order a book”. NLU’s job is to take this input, understand the intent of the user, and find the entities in the input. For example, in the above sentence, the intent is ordering and the entity is a book. Rasa NLU internally uses Bag-of-Word (BoW) algorithm to find intent and Conditional Random Field (CRF) to find entities. Although you can use other algorithms for finding intent and entities using Rasa.
  • RASA CORE: Core is Rasa’s dialogue management component. It decides how an assistant should respond based on “the state of the conversation” and “the context”. Rasa Core learns by observing patterns in conversational data between users and an assistant. Rather than a bunch of if/else statements, it uses a machine learning model trained on example conversations to decide what to do next.
  • RASA X: is a toolset for developers to build, improve, and deploy contextual assistants with the Rasa framework. The good thing about Rasa X is that you can share your assistant with real users and collect the conversations they have with the bot, allowing you to improve it without interrupting the bot running in production.

Usage: After installing Rasa, you will find 3 files, namely:

domain.yml, stories.md, nlu.md

They define respectively the universe your bot lives in, the conversational backbone the bot has to follow, and the data on which it is trained to classify intents and entities.

Domain: defines the universe in which your bot operates. It specifies exactly:
- which intents you are expecting to respond to
- which slots you wish to track
- which actions your bot can take
What are Slots?
Slots are the things you want to keep track of during a conversation.

Actions: are the things your bot can do. For example, an action can:
- respond to a user.
- make an external API call.
- query a database.

Stories: A training data sample for the dialogue system is called a story. This shows the bot how to act and react to the inputs given by the user. It starts with a name preceded by two hashes ##

Our story example for this article for admission inquiry

NLU Data: The data to train the NLU. So, this is what it looks like:

Screenshot for our data/nlu.md file

The model is trained on these examples. The golden rule is, the more the examples (for each intent), the better the prediction.

Hence, depending on the definition and content of the above 3 files, your bot will be trained and will perform accordingly.

Installing Rasa and its Dependencies

I have installed RASA in both windows 10 and Ubuntu 19.04. And to be honest, I always believe that the fastest way to begin building an AI assistant with RASA is on the command line, with very simple steps.
You can install both Rasa (NLU and Core) and Rasa X with a single command, but you need first to upgrade your pip, here are the steps of installation in Ubuntu 19.04.

$python3 -m pip install --user --upgrade pip
$python3 -m pip install --user rasa --default-timeout=100

Dependencies for spaCy: For more information on spaCy, check out the spaCy docs. You can install it with the following commands:

$python3 -m pip install --user -U spacy
$python3 --user -m spacy download en_core_web_sm

Congratulations! You have successfully installed Rasa Open Source! 🥳

You can now create your new project. To do this, you need just to run:

$mkdir myfirst_Bot
$cd myfirst_Bot
$rasa init --no-prompt

The rasa init creates all the files that a Rasa project needs and trains a simple bot on some sample data. If you leave out the --no-prompt flag you will be asked some questions about how you want your project to be set up.

Now, it’s time to learn about some more advanced RASA features.

Build ChatBot that receives user information using forms

We know now that a contextual assistant goes beyond simple FAQ bot. It needs to have collected important details needed to answer user questions in the right context. In this article, I will show you how to build a simple demo Form Bot, about the admission process, it will ask you about your name, your SSN number, and the subject you want to learn.

So, how do you implement this ChatBot? 🤔 Glad you asked. 👇

To get started, we need to create a new folder you can name it whatever you want. Then run rasa init to create all the files that a RASA project needs.

The best part about the forms is that the assistant learns to handle the admission path from one single training story. To simplify the things and avoid a boring long article, the process of creating this Chatbot has been summarized in the gif with details. Check out the gif below for more illustration: 💡 👇👇

We can now, try Rasa X for depth understanding and verify if our validation function works fine or not. 👇

And it’s working!! 🎉🎉 👏 👏

Now, you can also create a simple but good conversational chatbot that behaves like a human. 😉😉

Conclusion

“One good conversation can shift the direction of change forever.” — Linda Lambert.

This was a tutorial to create a conversational ChatBot that uses Forms to receive information. Now, I think that you had the basics and you can add as much interactivity as you want to create your own ChatBot for a restaurant booking, doctor appointments, etc.

Through this article, I want to show you that creating Chatbots is neither easy nor tough. We used the capabilities of Rasa to create a bot with minimum training data. Rasa is a pretty useful library and you can experiment and tinker with it to create some truly useful chatbots.
Don’t stop yourself here — go on and experiment more. 🤖👩‍💻

Happy reading, happy learning, and happy coding.

Before You Go:

Let me know if you have any questions or if anything is unclear, you can contact me through my LinkedIn or send me an email if you want the source code for this project.

You can also check my previous article on building Chatbot:

References:

  1. RASA documentation (Rasa basics).
  2. A tutorial from Justina on how to build contextual assistants with Rasa.

--

--