Are you a chatbot?
Create a Chatbot using Rasa
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 chat bot via Facebook Messenger or Slack. It is also possible to publish the assistant as Alexa Skill. Rasa offers both the possibility to implement an assistant 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 Rasa makes it possible to easily create assistants. For this purpose, we create a chatbot, which can answer simple questions about its creators. The project is available at https://github.com/Steadforce/rasa-basic-tutorial. An exemplary dialogue that we can conduct is shown here:
Building a chatbot – Installation and set-up
Before we can start creating the assistant, we need to install Rasa. This requires Python 3.6 or 3.7. 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 this is not desired, the parameter can be removed.
pip install rasa rasa init --no-prompt
After successful initialization a data structure is created as shown in the figure. In the following we will discuss which data has to be created to create an assistant in Rasa.
First of all, we start by creating so-called stories. Stories define how the assistant reacts to user questions. This is done by Dialog Management and the created stories serve as training data for this.
Stories are created in data/stories.md. 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 assistant by an indented hyphen.
The exemplary dialogue, as shown above, can be presented as a story as follows:
## Bot-Challenge and ask for creator * is_bot - utter_i_am_a_bot * how_created - utter_created_by_steadforce
The story is called "Bot challenge and ask for creator". The two # signs in front of it mark that this is the name of a story and therefore the beginning of such.
In the next line the dialog between the assistant 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
We will go into the various forms of this question in the description of Natural Language Understanding (NLU).
The assistant reacts by replying with the action
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 assistant. Rasa also offers the option of creating stories in an interactive mode. These can also be saved in Markdown format and used for training.
Creating Natural Language Understanding (NLU) data
After creating sample dialogs based on the stories, we generate possible versions of the intents. When the user interacts with the assistant, 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.md.
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
## intent:is_bot - Are you a bot? - Are you a human or a bot? - Am I talking to a bot? - You're not a bot, are you?
For the intent
how_created we generate training data according to the same pattern as well.
Creating the domain
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 assistant operates. Among other things, intents and responses are defined there. The configuration of the domain is done in domain.yml. In this example, we configure the domain as follows:
intents: - is_bot - how_created responses: utter_i_am_a_bot: - text: “Yes I am a bot and developed with Rasa” utter_created_by_steadforce: - text: “My developers were staff of Steadforce” session_config: session_expiration_time: 60 carry_over_slots_to_new_session: true
In this file the individual intents are listed and the texts for the responses, the answers of the assistant, are defined. Furthermore, the so-called conversation session is configured here. The conversation is a dialog between the user and the assistant. 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.
Setting the language
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 assistant to enable a correct recognition of the users' intentions. This setting can be made in the config.yml file.
In our example we want to set the language to German. By default, English is preconfigured as language. So, 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 assistant. We do not adapt these configurations in our example and use the default values given by Rasa.
Create a chatbot – Training and a first attempt
Once we have made all the necessary configurations, we can run the training and test the assistant via the console. Both is possible using the Rasa CLI.
First of all, the training of the wizard has to be done.
After a successful training the assistant can be used directly via the console.
Here we can test the conversation defined at the beginning and see if our assistant gives the right answers.
In this example we have shown how to create a simple assistant 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 that fill out forms in the dialog. For this purpose, forms are used in Rasa. 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.