During our university masters seminar we build a chat bot prototype to enhance the user experience on the MOOC platform Open SAP. In the following we will give a short overview over our experiment including how to train models and use the conversational UI prototype.
Rasa is an open source platform for building chat bots. It consists of two major parts: the Rasa Core and the Rasa NLU. On top of that we additionally build our own Chat UI frontend in ReactJS.
The Rasa NLU is responsible for the Natural Language Understanding of the chatbot. It receives an input (e.g. a sentence) from the user through the UI and predicts an intent of that sentence. The intent defines how the chatbot will react to a certain input.
Rasa Core takes the output of the NLU (a structured input e.g. an intent) and decides on an action to perform on it. Actions can be several things: an API Call, a response in form of an utterance or just an input validation (read more Actions). These actions are send back to the frontend as the bots response.
The chat UI is a simple reactJS frontend. It consits of a simple input mask where the user can enter question or actions to the bot and communicate with it.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. You will train a model for both the dialogue engine and the NLU and send your first message to the bot.
- Docker with Docker Compose
- Clone the repository.
git clone [email protected]:schul-cloud/cui.git
- Build the images by going to the root directory and execute the following command.
This may take a while...
docker-compose build
- Train the models, so the chatbot knows how to behave.
docker-compose run rasa-core python -m rasa_core.train -d data/opensap_faq/domain.yml -s data/opensap_faq/stories.md -o model/opensap_faq --epochs 200
docker-compose run rasa-nlu python -m rasa_nlu.train -c config.yml -d data/opensap_faq -o projects --project opensap_faq
- Start the containers.
docker-compose up
- Open
http://localhost:3000
to access the chat interface. The first request to the bot may take a few moments, since the NLU has to be initialized.
- Define examples of what the user might say to express the intent. This is done in the training data of the NLU in one or multiple Markdown files. These can be located in
rasa-nlu/data/<project-name>
.## intent:sound_of_cat - what does the cat say - what does a cat sound like
- Now the dialogue engine might receive the
sound_of_cat
intent, which has to be added to its domain. Since the bot should only output a simple text response, its template can also be specified inrasa-core/data/<project-name>/domain.yml
.Since there are multiple templates defined, Rasa Core will choose one randomly.intents: - sound_of_cat actions: - utter_sound_of_cat templates: utter_sound_of_cat: - text: Meow! - text: Meow meow!
- Add training data for the conversational flow, so that the bot triggers the correct action
utter_sound_of_cat
after the intentsound_of_cat
. This training data is located inrasa-core/data/<project-name>/stories.md
.## Make meow sound <this line is just a comment> * sound_of_cat - utter_sound_of_cat
- Train the models as described in Training Models.
Service | Description | Port | URL |
---|---|---|---|
Chat UI | Demo to show the interface on the openSAP landing page. | 3000 | http://localhost:3000 |
Rasa Core | Rasa Core instance for a predefined project. | 5005 | http://localhost:5005/conversations/default/respond?q=hello |
Rasa NLU | Rasa NLU instance, which can be used for multiple projects. | 5000 | http://localhost:5000/parse?q=hello&project=opensap_faq |
Rasa NLU Training | Runs a cron job, which starts the NLU training daily at midnight. | ||
Duckling | Entitiy extraction service for predefined entities. For more information see the Rasa and duckling documentation. | 8000 |
To train the respective service run one of the following commands from the root directory.
docker-compose run rasa-core python -m rasa_core.train \
-d data/opensap_faq/domain.yml \
-s data/opensap_faq/stories.md \
-o model/opensap_faq \
--epochs 200
docker-compose run rasa-nlu python -m rasa_nlu.train \
-c config.yml \
-d data/opensap_faq \
-o projects \
--project opensap_faq
Actions are the things the bot can do. As described in the Getting Started section one action type are simple utterances, that can be defined in the domain of your project. Furthermore variables can be used in the templates to fill them with slot entries. But you can also define your own custom actions with some Python code to call an API or do whatever you can think of.
Rasa provides a basic Action
class to inherit from and a more specific FormAction
. The latter one is a simplification for use cases, where you need a specific set of data values from the user to perform an action. You can find a small example in rasa-core/actions/opensap_faq/account.py
. The class ActionEmailForm
gets an email address from the user and prompts him to confirm it. Only if these two steps are done, the submit
function is called.
To use your custom actions in your stories they have to be added to your projects domain.
actions:
- opensap_faq.account.ActionEmailForm
What if the answer of the bot was not what the user was expecting or did not help with the users problem?
If the NLU was not able to clearly classify the intent, which means the confidence is below a specific threshold, the user is presented a selection of alternatives. To do this we extended the Core server with a mapping from an intent to a question. For example the intent sound_of_cat
might be mapped to the question Did you want to know what a cat sounds like?.
To offer this to the user, you need to define these questions for each intent in a intent_questions.json
file in your projects data folder, e.g., rasa-core/data/opensap_faq/intent_questions.json
.
{
"sound_of_cat": "Did you want to know what a cat sounds like?"
}
When the user chooses one of the alternatives, the endpoint http://localhost:5005/conversations/<sender_id>/tracker/reset_intent?intent=<alt_intent>
is called with the respective intent name. This resets the tracker for the current conversation to the last user input and executes the actions for the new intent.
To improve the NLU the input of the user is saved as an example for the alternative intent in rasa_nlu/data/<project_name>/user_input
. These markdown files are used in the next training for the model of the given project.
As already mentioned before this is just a prototype and there are some things that can be implemented in the future to introduce it as a beta feature. Two things we want to point out a bit more detailed:
- Session Management
- Authorization for Actions
Session Management and Authorization for Actions come hand in hand. Session Management is an important feature for every online service especially chat bots and therefore every session should be time-boxed. At the end of each session the user needs to authorize once again. This is on the one hand an important aspect for the actions which the bot wants to perform. For example, in order to change the email-address of the user, the bots needs to have the permission to access the users settings. On the other hand, it is a necessary security feature to limit the bots access to certain commands and prevent unwanted actions.