RASA 3.0 – Creating Intent-based Chatbots with RASA

RASA 3.0 – Creating Intent-based Chatbots with RASA

11 mins read3.4K Views Comment
clickHere
Updated on Sep 21, 2022 12:49 IST

This step-by-step guide will help you understand how to create your intent-based chatbot using RASA 3.0.

2022_02_RASA-3.0-AI-Based-Chatbot.jpg

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:-

  1. What is Rasa and how to install it
  2. Defining a problem domain
  3. Understanding file structure of Rasa
  4. 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.

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:-

2022_02_File-Structure-in-Rasa.jpg

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:-

2022_02_Problem-Definition.jpg

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:-

2022_02_Problem-Definition-example.jpg

Looks cool? This is how our chatbot will work.

Let’s Understand File structure

  • nlu.yml
2022_02_NLU-in-Rasa.jpg

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

  1. 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
2022_02_Domain-Rasa.jpg

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:-

  1. Declare all the intents we have made in our nlu.yml file
  2. Define responses for the intents in our nlu.yml file
  3. 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

  1. Good paths:  These are the steps a user follows when they come to a chatbot and they have got answers for their queries Example:-
2022_02_Story-Happy-Path.jpg

This shows a user came to a chatbot

  1. Greeted a chatbot ( intent: greet )
  2. Chatbot greeted back ( action : utter_greet )
  3.  The user asked a question about breaking news in Germany ( intent: news_checker )
  4. 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

2022_02_Sad-Paths.jpg

 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.

2022_02_Action-Files.jpg

In this custom action, we are using https://newsapi.org/ for fetching the latest news

  1. The name of your custom action goes in the return statement of the name function of the custom action class you made.
  2. The run function is responsible for making actual responses to a user query.

Let’s now decode our run function.

<
  • At first we defined the header variables to avoid any SSL error that occurs when you ping an API
  • Url → The URL we are going to use to get our results regarding crypto
  • 2022_02_URL.jpg

    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.

    2022_02_Rules-File.jpg

    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: –

    1. 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:-

    1. Add data to nlu.yml
    2. Define response for the intent either in domain.yml or using a custom action
    3. Declare the name of the intent and its response in domain.yml.
    4. 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.
    2022_02_Cryptocurrency.jpg
    • 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.
    2022_02_Crypto-Custom-Action.jpg

    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.
    2022_02_Intent.jpg

    Story for the above intent.

    2022_02_Intent-Story.jpg

    ●     Get News headlines for countries like Germany and the United States.

    1. To get news for these countries we will also make an intent.
    2. Define a custom action for this.
    3. Declare the intent and its response in domain.yml
    4. Define a story for the intent and its response
    • The intent for getting news about a country ( ex: Germany )
    2022_02_Intent-for-News.jpg
    • Custom action to get news about the country ( ex Germany )
    2022_02_Custom-Action-in-Rasa.jpg

    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.
    2022_02_Declare-Intent.jpg
    • Story for the same.
    2022_02_Story-Happy-Path.jpg

    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
    Copy code

    In your terminal and whoop your rasa model is training it will look something like this.

    2022_02_Rasa-Model.jpg

    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.

    2022_02_Chatbot.jpg

    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.

    1. What is Rasa?
    2. How to install Rasa.
    3. The file structure of Rasa
    4. How to make intents in Rasa
    5. How to make custom actions in Rasa.
    6. Train your first chatbot in Rasa.
    7. 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.

    About the Author

    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

    Comments