RASA 3.0 – Creating Intent-based Chatbots with RASA
This step-by-step guide will help you understand how to create your intent-based chatbot using RASA 3.0.
By Sahib Singh
It is getting really necessary for companies and organizations to provide an immersive experience to their customers when it comes to either customer support or delivering any service. But the difficult bit is these organizations have to employ people for the same but nowadays CHATBOTS are taking place of these people to help companies in their task and the best part of these chatbots is that you have to just employ one chatbot that can easily take over the task of many people seamlessly.
After reading you might have concluded that chatbot making is easy but wait the only difficult piece in making chatbots relevant in today’s world is that they should be contextual which means a client should never feel that they are talking to a computer who has been taught on Hundreds of If-else statements to answer queries.
They ( Chatbots ) should be able to understand the context. So now comes the boss in this domain Rasa.
Rasa is a contextual chatbot software that helps developers to make chatbots relevant in today’s world and in today’s blog we will be deep diving:-
- What is Rasa and how to install it
- Defining a problem domain
- Understanding file structure of Rasa
- The solution to the problem
So Let’s dive in.
What is Rasa?
Rasa is a tool built for making contextual chatbots using the power of transformers. Chtabots made using rasa can be deployed on websites like slack,Microsoft Teams and yes our own website.
Best-suited Python courses for you
Learn Python with these high-rated online courses
Installation
Windows :-
Check if your Python environment is set
python3 --version pip3 --version
If there is no error above you are good to go.
And if you see errors in the Python command install python from https://www.python.org/.
After successfully running the above 2 commands you can now start the installation of Rasa in your machine.
1. Create a new virtual environment by choosing a Python interpreter and making a .\venv directory to hold it:
C:> python3 -m venv ./venv
2. Activate the virtual environment
C:> .venvScriptsactivate
3. To install Rasa 3.0 (The latest one) you will require Python 3.7 or 3.8 with pip as package manager.
pip3 install -U --user pip && pip3 install rasa
Or you can try if the above command gives some error
pip install rasa
And guess what you have successfully installed Rasa 3. Just one last step is to make your initial rasa project.
rasa init
That’s it your Rasa is installed on your machine.
Mac:-
Install the Homebrew (https://brew.sh/) package manager if you haven’t already.
Once you’re done, you can install Python3.
brew update brew install python
1. Create a new virtual environment by choosing a Python interpreter and making a .\venv directory to hold it:
python3 -m venv ./venv
2. Activate the virtual environment:
source ./venv/bin/activate
3. Install Rasa
pip3 install rasa
And finally
rasa init
So till now, we have covered the installation of rasa in your systems. Once installation is done you will see a file structure like this:-
So these are a lot of files but believe me while going through this blog I will explain the purpose of every file and this will become more interesting when we will solve our problem statement.
Problem Definition
For today’s tutorial, we will make a newsbot. Newsbot will be a chat-oriented news portal where you can type in what news you are looking for and the chatbot will assist you.
Example:-
Looks cool while going through the blog. I will include more interesting ideas so that you can explore more after the end of the blog.
What will you be able to do from this chatbot?
Examples:-
Looks cool? This is how our chatbot will work.
Let’s Understand File structure
- nlu.yml
NLU files in the Rasa project hold all the intents that are required in a chatbot.
But what are intents?
Intents are basically different topics on which we are going to train our chatbot so that it can answer user queries. If you see the example image above I have different 2 intents
- news_checker intent and it has nearly 4-5 examples written under it. By these examples, we are trying to tell our chatbot that whenever a user asks any question you have to recognize it as news_checker intent and then give its response accordingly
So whenever you are going to define any new topic for your chatbot you will define your examples for that intent in NLU files.
It is always a recommended practice to define at least 5 examples per intent before training your chatbot.
- domain.yml
Domain file —> domain.yml file This is the most important file for our rasa chatbot. It is also recognized as the universe of a rasa chatbot. In this file we will:-
- Declare all the intents we have made in our nlu.yml file
- Define responses for the intents in our nlu.yml file
- Declare the name of all the custom actions made in the actions.py file.
- Stories.yml
Stories.yml —> Before explaining the usage of this file. Let me tell you one interesting thing that I notice as a chatbot developer that is whenever a user comes to a chatbot to solve the queries they put their questions in some particular pattern and being a chatbot developer you should recognize some of these patterns before making your bot public and you should use these patterns in your chatbot training.
These patterns are basically divided into 2 sections
- Good paths: These are the steps a user follows when they come to a chatbot and they have got answers for their queries Example:-
This shows a user came to a chatbot
- Greeted a chatbot ( intent: greet )
- Chatbot greeted back ( action : utter_greet )
- The user asked a question about breaking news in Germany ( intent: news_checker )
- Chatbot revert us back with breaking news in
Germany (intent : action_hello_germany )
2. Sad paths: These are the steps a user follows when they come to a chatbot and they
have not got answers for their queries
I would request you to read about intents mentioned in sad paths ( image above) from nlu.yml and see if you can relate to it. So basically stories.yml is a file whereas a chatbot developer you define some happy and sad paths so that your chatbot can learn from these paths.
- Actions file
Actions file → This python file contains code for all the custom actions we are going to define for intents.
For the intents defined in nlu file we either define their pre-made responses in domain.yml or if we are looking for custom responses we define their custom actions and custom actions are python functions that are used to deliver responses to chatbot queries made by a user.
For example, to fetch the latest news regarding crypto we have defined a custom action named
action_crypto-section.
In this custom action, we are using https://newsapi.org/ for fetching the latest news
- The name of your custom action goes in the return statement of the name function of the custom action class you made.
- The run function is responsible for making actual responses to a user query.
Let’s now decode our run function.
This URL is using q argument to get us results regarding crypto you can replace it with any keyword. So whatever idea fascinates you can diverge the newsbot in that direction and get news regarding that keyword or domain.
- Obj variable stores the actual dictionary we get as a response from which we want to fetch different portions related to news like URL, title, description, content, etc
- The index variable is used to randomly select a number from the total number of news we got for crypto and then we will use this number to show random top headlines from around the globe regarding crypto.
- The output contains the actual we are going to show to the user.
- And at last, the dispatcher is responsible to dispatch news back to the user in actuality.
- Rules file
This file contains the set of rules that we want our chatbot to strictly follow. Like once our chatbot has recognized an intent and now we want our chatbot should answer with a specific response for that intent. We will define a rule for it.
This rule defines once our chatbot has recognized goodbye intent it will revert back with utter_goodbye (defined in domain.yml ).
- config.yml: This file defines the configuration of the NLU and core model. If you are using any model outside the NLU model, you have to define the pipeline here.
- credentials.yml: This file is used to store credentials for connecting to external services such as Facebook Messenger, Slack, etc
- endpoints.yml: This defines the details for connecting channels like Slack, FB messenger, etc. for storing chats data in the online databases like Redis, etc.
- All the models we will train in Rasa will get stored in the models folder
Now let’s add some data to make our chatbot interesting.
I will now break down what things our Newsbot will be able to do: –
- Get top-news related to Cryptocurrencies from around the globe
- Get news headlines from different countries, for example, Germany and the United States, etc.
To explore more you can go to the same website mentioned above and see what different news you can fetch and use in your chatbot.
● Top-news related to cryptocurrencies.
To make our chatbot able to talk to the end-user and give the user the latest news about crypto, we will follow some steps:-
- Add data to nlu.yml
- Define response for the intent either in domain.yml or using a custom action
- Declare the name of the intent and its response in domain.yml.
- Write a minimum of 1 story for it so that the chatbot could know which intent has been attached to which response.
- As we are talking about cryptocurrency news it will be a section of our chatbot that our chatbot will be able to answer so first of all we will add an intent in nlu.yml containing some examples of how users can ask for breaking news in this domain.
- Now as you can see we have defined our nlu data. Now we will have to define a custom action that can fetch the latest news regarding crypto from https://newsapi.org.
I have already explained this custom action for cryptocurrencies while explaining the usage of the actions.py file. It is just a python function using requests library from python to fetch news from an API.
- Now the last step is to put the name of the intent and its corresponding response in the domain file and write a minimum of 1 story for it.
Story for the above intent.
● Get News headlines for countries like Germany and the United States.
- To get news for these countries we will also make an intent.
- Define a custom action for this.
- Declare the intent and its response in domain.yml
- Define a story for the intent and its response
- The intent for getting news about a country ( ex: Germany )
- Custom action to get news about the country ( ex Germany )
If you carefully look at the function it is nearly the same as the above function but with just the difference in URL variable.
- Declare the intent and its response in domain.yml.
- Story for the same.
So that’s it we have successfully made our first chatbot in Rasa. Now it’s time for training our chatbot and it is very simple. Just type
rasa train
In your terminal and whoop your rasa model is training it will look something like this.
Now the final frontier let’s talk to our Newsbot
To talk to Newsbot just type
rasa shell
And your chatbot will look like this.
So this is it. In this blog, we have made our Newsbot which can help you get the latest news regarding different topics using Rasa.
What you learned in RASA.
- What is Rasa?
- How to install Rasa.
- The file structure of Rasa
- How to make intents in Rasa
- How to make custom actions in Rasa.
- Train your first chatbot in Rasa.
- Talking to it ????
Below is the link to the Github repo that contains all the files for this chatbot.
Github: https://github.com/sahibpreetsingh12/Blog-newsbot/tree/main
I hope this blog helped you get started with Rasa 3.
Top Trending Tech Articles:Career Opportunities after BTech Online Python Compiler What is Coding Queue Data Structure Top Programming Language Trending DevOps Tools Highest Paid IT Jobs Most In Demand IT Skills Networking Interview Questions Features of Java Basic Linux Commands Amazon Interview Questions
Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.
Click here to submit its review with Shiksha Online.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio