Introduction to Rasa Open Source

Eleonora Fontana
CodeX
Published in
5 min readMay 18, 2021
Photo by Volodymyr Hryshchenko on Unsplash

This article was written by the Betacom Analytics and Innovation team. Betacom is a company based in Italy and Germany that operates in the IT field using innovative technologies, digital solutions, and cutting-edge programming methodologies. You can find out more on our website.

Introduction

Rasa is an open source machine learning framework for automated text and voice-based conversations. Understand messages, hold conversations, and connect to messaging channels and APIs.

Source: https://rasa.com/docs/rasa/

In this article, we will explore Rasa's main concepts and learn how to build a simple chatbot using this framework.

Overview

Rasa Open Source is based on Python and can be installed using pip. Be aware that it requires Python 3.6, 3.7, or 3.8.

pip install -U pippip install rasa

More details on the installation process can be found here.

As you may already know, a chatbot needs both a backend and a frontend. The first one is what we are going to build today and consists of all the rules and actions the bot has to follow to interact with the user; whereas developing the second one means creating an interface, such as a widget, to allow the user and the bot to chat. It is beyond the scope of the article to build a frontend so we will use the command line interface provided by Rasa.

When building a chatbot, the first thing you need to individue is the User Goal, i.e. the overall goal that a user aims to achieve. To do so, the user will send messages and interact with the buttons proposed by the assistant.
In a user message, the intent is what the user is trying to accomplish. Some intent examples are greeting, specifying a location, asking for help. From a user message, it is also possible to extract keywords, called entities. They can be telephone numbers, locations, etc.

On the other hand, the assistant performs actions, such as sending a response back to the user or completing default and custom actions. Responses can be texts, buttons, images, and other content, whereas custom actions are actions written by developers to run arbitrary code, mainly to interact with external systems and APIs. Note that responses are just a list of answers from which the bot chooses one each time it has to. The server that runs custom action code is called the action server. Rasa maintains the Rasa SDK in Python for implementing custom actions, although it’s also possible to write custom actions in other languages.

A form is a type of custom action used to ask the user for multiple pieces of information. For example, if you need a city and a movie title to book a ticket to watch it, you can create a form to collect such information. You can describe business logic inside a form, like offering the customer a different seat option in the theatre. We will cover them in detail in a later article.

Everything we just discussed is listed in the domain, which defines the inputs and outputs of an assistant.

Lastly, a story is a training data format for the dialogue model, consisting of a conversation between a user and a bot. The user’s messages are represented as annotated intents and entities, and the bot’s responses are represented as a sequence of actions.

Building a chatbot

In this section, we will discuss how to build a simple chatbot. As stated before, the first thing to do is to define the user goal. We want our assistant to act as a search engine for movie titles. It will then be able to introduce itself and ask the user how they are feeling and if they want to search for a movie title. If so, it will then state that such a request will be available soon, since we will build this feature in a later article.

Let’s get in action! Open a command window, go to an empty directory where all the chatbot configuration files will be stored. Once there, execute the command rasa init and press n to answer the question “Do you want to train an initial model?”. We will indeed train the model in a second moment.

This will create an example chatbot configuration with the following folder structure:

We can now train the model by running the command rasa train. Let’s now take a look at what the example chatbot can do!

Open two command prompts and goes to the root folder of your chatbot. In the first cmd run the command “rasa run actions” to activate the action server, i.e. the server where the backend will run; whereas in the second one execute “rasa shell” to load the last trained model and start the command-line interface to chat with the assistant.

An example of a basic conversation is the following:

The example chatbot indeed is only able to ask how the user is feeling and react to both positive and negative answers. Try yourself what happens if you write that you are feeling sad.

Let’s now edit the configuration files to achieve the user goal. The first thing to do is to add two bot responses into the domain.yml file:

  • utter_movie to ask the user if they want to look for a movie
  • utter_not_yet to inform the user that a feature will be available soon.
utter_movie:
- text: "Do you want me to look for a movie?"
- text: "Do you need me to look for a movie?"
- text: "Would you like to look for a movie?"

utter_not_yet:
- text: "This feature is not available yet. Please come back!"
- text: "This feature will be available soon!"

We now have to edit the data\stories.yml file to add these responses in the conversation paths. The current stories are

  • “happy path” which covers the conversations in which the user states to be happy,
  • “sad path 1” which covers the conversations in which the user states to be happy and that the bot response was helpful,
  • “sad path 2” covers the conversations in which the user states to be sad and that the bot response was not helpful.

We then need 6 stories to cover all possible scenarios:

The complete file with all stories can be found here.

We can now train the new model, using the rasa train command as before.

Conclusion

We went through the basics of Rasa Open Source and learned how to build a simple chatbot. We recommend taking a look at the files generated by the rasa init command and try to add some examples both to intent and responses. In the next article, we will discuss how to add a custom action and use entities and slots to improve the chatbot skills. See you soon!

--

--