Build a chatbot with Rasa stack — The Beginner’s Guide
My friend who had learnt about conversational AI told me about RASA stack. Initially I was sceptical about its performance, developer friendliness, simplicity etc however it has proven me wrong at each step whenever I tried to question it. I could create my first bot in no time and since then I have been in love with RASA. I have done couple of large projects with it. This essentially means that I have seen this thing growing over the past one year. I must say if you want to make a quick chatbot then RASA is what will be my first recommendation.
I had been a RASA HERO for quite sometime now and had arranged the first RASA meetup in India, with the first one in Mumbai and then in Bangalore. While I was doing the first RASA meetup in Bangalore, one of the participants asked me to share my experience and guide the newbees, how to use the stack and create a chatbots super quick. Also I have seen a lot of RASA blogs, they are really very technical in nature and goes overhead of a new chatbot developer plus the blogs are not updated with the latest updates which makes it difficult for any newbie to try this stack. What I have shown here is quiet simplified. I will also keep this blog updated over a longer run with the latest updates so that it doesn’t become stale. I hope this will be encouraged by someone who is trying to get started with RASA and by folks who are already working on this stack. Lets make this simple bot in just 6 easy steps … 🚀
Step 1: Setting up the environment
To start working with RASA I have created a VM with the rasa setup. The same can be downloaded from http://bit.ly/rasa-vm . The same needs to be imported in VirtualBox like below:
If you want to create your own setup you can install python and use the command “$ pip install rasa”. This will install all the packages that are required to install rasa.
Thing to remember — if the above command gives you an error you can install the individual packages and then rerun the pip command again.
Step 2: Login to your VM
Start the VM and login into the same by putting the password as “admin”
Step 3: Check RASA Version
Check the version of RASA by opening up the terminal. This is the latest version of rasa on the VM. We are committed to update the blog post with all the latest additions as an when they are available
Create your first RASA Bot — It’s super quick !!
Earlier most of the developers pull the basic project structure of a rasa weather bot from git and then use to modify it however with the latest update of RASA 1.0.1, boilerplate with the basic project structure is now available. Also all the commands that are now being used are quite shortened.
Step 4: Know your Project Structure
Create a new directory as “rasa-medium-bot”. Once the directory is made, go to the directory and create the boilerplate with “$ rasa init”
Press Enter. When you press enter rasa will create the project structure, train the model for both NLU and CORE and create zip file (model folder) for the same. In the earlier versions of RASA the chatbot developers generally use to pull the default RASA project from Git and train individual nlu and core. With the latest version of RASA this is now simplified :)
Once the model is created the next prompt is to talk to the trained bot, however we can do this later. You can see below the set of files that are created with the rasa init command.
Now let us see what each file mean in the project structure by opening in our editor Visual Studio Code.
1. __init__.py : This is an empty file that helps python finds the actions which you have written.
2. domain.yml : This is the most important file for RASA. They call it as an assistant domain. Your bot has 2 components — NLU and CORE. The NLU is what the user will / may ask the bot. The CORE is what the bot will answer (We will talk about the RASA internals in a seperate blog). When you train your bot for both on the NLU / CORE side, it take reference from this file and create separate models. In RASA 1.0 this is a single zip file that is being created, however you can train these two seperately as well. Important features in this file are :
intents : All the intents that you have defined in your NLU file (nlu.md). For example when the user asks “Hi How are you?” or “Hello” or “Hi” likewise. Rasa will perform intent classification and map this sentence to “greet”. The same question needs to be defined in the NLU.md file under intent #greet.
actions / templates : Actions are nothing but what bot will reply to the user. In the below example you see utter_greet (in action) and utter_greet (in template) which means the bot will reply Hey! How are you ? when a user ask a greet message like “hey / hello / good morning” (corresponds an action).
Other than this you will have slots / entities (We will see this in another post).
3. actions.py : This is a file in which you can write your custom actions. For example: If a user asks “how is the weather in Mumbai” once the intent classification and entity extraction is done, the action action can be called to hit a weather api. Additionally you can also define a fallback_custom_action which can be called if the NLU component cannot understand the incoming conversation. You can then store those questions, retrain your model.
4. config.yml : This is an important file, which tells rasa how to train your bot. Having said that it will decide how to train both the NLU or the CORE side of your bot. For this guide we will use spacy_sklearn as our NLU training algorithm. The reason we will use spacy is that spacy provides inbuilt tokeniser, word embeddings for greetings like hi, bye, how are you ? and all
5. credentials.yml : To connect other services like slack, facebook messenger (voice and chat platforms). We will see later in the blog series how to connect with facebook messenger, google voice assistant etc
6. data/nlu.md : This defines the probable questions that the user may ask. As a bot developer you need to define all the set of questions here. If you look all the questions that you define are part of some intent, be it Greet, GoodBye, Affirm, Deny etc. I have added one more in greet “how are you doing today ?”
7. data/stories.md: This defines the flow of the conversation or you can say possible scenarios in which the conversation can lead into.
For every conversation that the user does with the bot, RASA NLU does intent classification and entity extraction and RASA Core is responsible for what Bot will “utter” to the user.
If I see above what I understand is that if the user asks “hello” what RASA NLU will do is, it do intent classification and mark this message as a “greet” intent and then RASA Core (which is responsible for utterance) picks up a message from the “utter_greet” template “Hey! How are you?”. Now if the user says “perfect” the RASA will map this to the intent “mood_great” and utter “Great carry on!”. If you look at it you are following a story/scenario as per the “stories.md” file.
8. endpoints.yml: If you want to interact with your bot over rest api or can store the tracker information (soring conversations) directly in redis or you want to run your custom action server etc you can define all these in this file.
Step 5: Train your RASA Bot
Now lets see how we can run this “rasa-medium-bot”. Since we have added a new question (training data) in the nlu.md and space_sklearn pipeline in the config.yml, we need to retrain the bot. For which you need to open the terminal, go to the directory and type in “rasa train”, this will train your both and create both NLU and CORE models
Step 6: Run Your RASA Bot
Now that the training is done, you can see that there are two .gz file inside the models folder. RASA will pick up the latest one each time you will run you bot. To run the bot type “rasa shell”. You will see the question that we have added is understood by the bot and replied “based on the stories
Whats Next !!
What I want you to do is to add more NLU data, stories and keep training and playing around with this bot. In the subsequent blogs I will talk about custom actions, how to talk to your bot through rest (postman), working with fall back actions, customised templates, hit the 3rd party apis and send custom responses to the users, about RASA X, connect it with google assistant and various other topics.
If you like the post please follow me on Twitter : @qasoumya or on Linked-In : mukherjeesoumya
Thanks swapnil patil for introducing me to RASA.