2. Build a Conversational Chatbot with Rasa Stack and Python — Rasa Core
I believe you have gone through my previous article Build a Conversational Chatbot with Rasa Stack and Python — Rasa NLU prior reaching here. If not then read that first before proceeding further.
We trained the bot in our previous article and now it’s time to build a dialogues management for bot to respond to the messages.
Rasa Core — Dialog Management
If you remember (from the previous article) we talked about the stories which are nothing but the sample interaction between the user and the chatbot. The user ’s inputs are expressed as intents with corresponding entities, and chatbot responses are expressed as actions. For dialog training, Rasa has 4 main components —
1. Domain(domain.yml)
The domain consists of five key parts consisting of intents, entities, slots, actions, and templates. We already discussed the first two in the previous part, let’s understand the rest.
- slots: slots are basically bot’s memory. They act as a key-value store which can be used to store information the user provided (e.g their home city) as well as information gathered about the outside world (e.g. the result of a database query).
- actions: are nothing but bots response to user input. There are 3 kinds of actions in Rasa Core: default actions, utter actions & custom actions
- templates: templates are messages the bot will send back to the user.
Here is an example of a domain for our bot:
If you see we have defined a slot which would help bot to have some memory about the conversation. The same slot is used in line no 42 & 43 to give the user some specific responses. All entities and intents from the previous part are mentioned. We have defined some actions and templates also for the bot to respond to user messages. In actions — there are default actions (action_restart), utter actions (utter_greet, utter_reply ….) and custom actions (action_get_news). In addition to it, there is one more action ‘utter_default’ which is nothing but a fallback in case bot is not able to understand the user message. We will discuss custom actions and fallback in just a short while.
2. Stories (stories.md)
Once we have defined our domain now let’s create a sample user-bot interaction. I have jotted down a few conversational paths
Lets put it down in a file. I named this file as stories.md and kept it in the data directory where we kept the nlu.md earlier.
$ cd wall-e
$ cd data
$ touch stories.md
So if you see above, We have defined various paths for the user and bot conversation. Always good to have more stories to train the dialog better.
3. Policies (policy.yml)
The rasa core policies decide which action to take at every step in the conversation. There are different policies to choose from, and one can include multiple policies in a single rasa core Agent but at every turn, the policy which predicts the next action with the highest confidence will be used. We have configured a basic policy(policy.yml) for our bot as shown below which has FallbackPolicy as well. The fallback policy comes in to picture when ‘nlu_threshold’ & ‘core_threshold’ meets the levels defined in the policy which means that bot is not able to understand the user message and it responds with ‘utter_default’.
4. Custom Actions (actions.py)
We have already defined the utter action by adding an utterance template to the domain file but if we need to run any code we want then we would need custom actions. Custom actions can do anything that you can imagine like turning on the lights, adding an event to a calendar, check a user’s bank balance, etc.
Rasa Core calls an endpoint specified by us when a custom action is predicted. This endpoint should be a web server that reacts to this call, runs the code and optionally returns information to modify the dialogue state.
To specify, our action server we use the endpoints.yml
and pass it to the scripts using --endpoints endpoints.yml
Though we can create an action server in node.js, .NET, Java, or any other language and define our actions there — but Rasa has provided a small python sdk to make the things easier for us.
The only thing our action server needs to install is rasa_core_sdk
:
$ pip install rasa_core_sdk
Let’s define actions.py, and add our custom action ‘action_get_news’.
FYI, We have used third-party APIs (NY Times API) to display the news in specific categories. The developer portal of the NY Times is very descriptive and well defined for one to consume messages. Follow the steps to get the api-key which would be required to make Rest Calls to the public APIs. (replace with your api-key)
Now run below command to run the action. Keep it running as when we will run the core it would need action to be up and running (run in separate terminal)
$ conda activate botenv
$ cd wall-e
$ python3 -m rasa_core_sdk.endpoint --actions actions
Now we have created all the required files/components needed for dialog management. Its time to train the dialog and run the Rasa Core to start the bot for the conversation.
First, we will train the dialogue (Make sure to activate the virtual environment (botenv) we created in the previous part)
$ conda activate botenv
$ cd wall-e
$ python3 -m rasa_core.train -d domain.yml -s data/stories.md -o models/dialogue -c policy.yml
then run the core
$ python3 -m rasa_core.run -d models/dialogue -u models/current/nlu --endpoints endpoints.yml
If using python then we can do both in one python file. We have created a python file dialogue_model.py and one can simply execute this file to train and run the dialogue.
$ conda activate botenv
$ cd wall-e
$ python3 dialogue_model.py
When python file gets executed, you will see the message that “Your bot is ready to talk! Type your message or send ‘stop’”. Some actual conversation with the bot looks like as below. Make sure to check the logs (nlu_model.log & dialogue_model.log) to get more idea on how bot classifies the intents and extracts the entities and then make a call to custom actions which eventually call a 3rd party APIs.
Finally, the bot is ready to chat :) and now needs some nice UI to interface with that we will cover in the next and final part of this series Deploy your chatbot on Slack.
I would highly recommend to go through each section step by step, check the previous part about Rasa NLU before moving further and read Rasa Documentation to understand it better. In case you need the code here is the git repository.
Please share your thoughts, feedback, comments, and clarifications. It will help me to improvise.
If you liked this post, you can help me share it by recommending it below ❤