With more than 2 million downloads, Rasa is an increasingly relevant open source framework for the creation of conversation assistants. A Rasa conversation assistant can be provided to users as a chatbot via Facebook Messenger or Slack. It is also possible to publish the assistant as Alexa Skill. Rasa offers both the possibility to implement a chatbot without any programming knowledge and to adapt the functional range of the framework to the respective needs via implementing extensions in Python. This article gives an overview of how to easily build a chatbot using Rasa. For this purpose, we create a chatbot in Rasa, which can answer simple questions about its creators. The project is available here. You can find an exemplary dialogue that we can conduct here:
Before we can start creating the chatbot, we need to install Rasa. This requires Python 3.7 or 3.8. After the installation we can directly initialize a new project with the Rasa-CLI. The parameter --no-prompt during initialization sets up the project in the current directory and trains an initial model as a test. If you do not want this, you can remove the parameter.
After successful initialization a data structure is created as shown in the figure. In the following we will discuss which data we need to build a chatbot in Rasa.
First of all, we start by creating so-called stories. Stories define how the chatbot reacts to user questions. Dialog Management takes care of this and the created stories serve as training data for this.
We can create stories in data/stories.yml. The start of a dialog is indicated by their names in the form of a markdown heading of type H2. User intentions are marked by an asterisk and actions of the chatbot by an indented hyphen.
We present the exemplary dialogue, as shown above, as a story as follows:
The story is called "Bot challenge and ask for creator", as marked from the "-story"-key. With this the name of the story is defined and and therefore the beginning of such.
In the next line the dialog between the chatbot and the user starts. It begins with a so-called intent, which is indicated by the * character followed by its identifier. An intent is an intention of the user, for example, the question whether the user’s conversation partner is a bot. This intent also has a name, in this example is_bot.
We will go into the various forms of this question in the description of Natural Language Understanding (NLU).
The chatbot reacts by replying with the action utter_i_am_a_bot.
This is also just the name of this action. We see the definition of the response text right away in creating the domain.
Next follows another intent of the user, to which we answer with utter_created_by_steadforce. In this pattern it is possible to create different stories, which serve as a basis for the Dialog Management of the chatbot. Rasa also offers the option of creating stories in an interactive mode. We can also save these in Markdown format and use them for training.
After creating sample dialogs based on the stories, we generate possible versions of the intents. When the user interacts with the chatbot, the intent is recognized based on the input using so-called Natural Language Understanding. Again, we provide sample data, i.e. possible versions of the intent. We do this in data/nlu.yml.
The individual intents must be specified in the format
- intent: <intent-name>
The training data need to be provided as a list. For a meaningful training we need at least four example data per intent. The following example shows this using the intent is_bot.
For the intent how_created we generate training data according to the same pattern as well.
Now that we have set up sample dialogs and training data for the NLU, all we have to do is define the domain. The domain is the environment in which the chatbot operates. Among other things, we define intents and responses there. We configure the domain in domain.yml, this is how it looks for our example:
In this file we list the individual intents and define the texts for the responses, i.e. the answers of the chatbot. Furthermore, we configure the so-called conversation session here. The conversation is a dialog between the user and the chatbot. In this configuration, the session is terminated after 60 minutes without interaction by the user and slots are taken over into new sessions. Slots are key-value stores that the user can fill during the conversation. In this article we do not want to go into slots any further.
Before we can start training the bot, we have to do some configuration. Besides creating the training data for the NLU, we must specify the language of the chatbot to enable a correct recognition of the users' intentions. We can make this setting in the config.yml file.
By default, English is the preconfigured language. In our example, however, we want to set the language to German. Therefore, we have to replace the value en with de.
In this file you not only specify the language but also the NLU pipeline and configure the policies that implement the Dialog Management for the chatbot. We do not adapt these configurations in our example and use the default values given by Rasa.
Once we have made all the necessary configurations, we can run the training and test the chatbot via the console. Both is possible using the Rasa CLI.
First of all, we have to train the wizard.
After a successful training we can use the chatbot directly via the console.
Here we can test the conversation defined at the beginning and see if our chatbot gives the right answers.
In this example we have shown how to create a simple chatbot in Rasa that answers simple questions with fixed answers. Rasa also offers the possibility to execute Python code with so-called actions, for example to create dynamic answers. In addition, you can create assistants using Rasa that fill out forms in the dialog. An application scenario for this are for example assistants that simplify reservation systems. If training data already exists in a certain data format, it is possible to create your own importers to avoid having to convert this data into the format described in the article.
Rasa offers an easy way to create conversation assistants. If required, it also offers a high degree of flexibility to make extensions and configurations to meet the needs of individual target groups.