An Introduction to Chatbot Development using RASA

Basil Saji Last Updated : 17 Nov, 2021
5 min read

This article was published as a part of the Data Science Blogathon

Introduction

Do you ever wonder how Google assistant, Siri, chatbots in different websites work? The advancement in the field of Natural Language Processing paves the way to different very accurate AI assistants in various fields like medical, defense, management, etc. There are several AI frameworks and tools like RASA, Dialogflow, Microsoft Bot Framework, etc.. are available for making both large-scale and small-scale chatbots.

In this article, we are discussing briefly the chatbot development using the RASA framework.

What is RASA?

RASA is an open-source chatbot framework based on machine learning. With the help of it, we can easily create highly accurate chatbots and can easily integrate these chatbots with our website, telegram, Facebook, Whatsapp, etc.

Before we get into it, let’s look into some simple concepts that we should know while creating a chatbot.

Query

A query is the user message for the chatbot in order to get details of something.

Response/Action

Action is the response from a chatbot based on the query.

Intents

Intents can be described as the aim or intention of the user input.

For example, hii, hello, good morning, good evening, etc.. can be treated as greeting intent.

Entities

Entities can be described as useful information that can be extracted from the user input ( The nouns in the dialogue).

For example, “‘I want to book a ticket from DELHI to COCHIN”, here both Delhi and Cochin are entities(location)

RASA Components

It has main 2 components, RASA NLU and RASA CORE

RASA NLU

RASA NLU is the interpreter which processes the user input and identifies the intents and extracts the entities from it.

RASA CORE

RASA CORE receives the output from the RASA NLU (which a dictionary specifies the intents entities and other information) and based on these details, the RASA Core part selects the appropriate reply and sends it back to the user as the bot reply.

Architecture of RASA

Architecture of RASA

In the architecture, we can mainly discuss 4 things Interpreter, Tracker, Policy, Action.

Whenever a person types and send a message to the RASA chatbot it will be received and passed to the Interpreter. It is this interpreter which identifies the intent of the message and extracts the entities out of it.
There is a Tracker present, which always tracks the state of the conversation between the user and the bot. Policy in the architecture tracks the current conversation state and decides which is the appropriate bot action. The selected action is also then tracked by the tracker and then sent to the user as the reply.

Installing RASA on Windows

Creating Virtual Environment

Lets install it in a virtual environment. So we are creating a virtual environment in our windows using virtualenv

installing virtualenv

pip install virtualenv

creating a virtual environment named my_env

virtualenv my_env

Next, we need to activate our environment

my_envScriptsactivate.bat

Installing RASA

Our virtual environment is successfully created and activated. Next, we need to install the RASA on our my_env.

pip install rasa

Next is to initialize our project

rasa init

We can train the initial model while initializing.

Installing RASA

Several new files will be created after the initialization of the project. The file structure will be

Installing RASA 2

Let’s go through the file structure in order to get an idea about the different files. These files are filled with default text by RASA.

The important files are

nlu.yml

This file contains the possible messages from the user and the corresponding intent. This file is used for creating the intent classification model. Whenever the user inputs a message, the classification model automatically classifies the intent of the message.

version: "2.0"
nlu:
- intent: greeting
examples: |
- hey
- hello there
- good morning
- good evening
- intent: goodbye
examples: |
- good afternoon
- cu
- goodbye
- see you later
- intent: deny
examples: |
- no
- never
- I don't think so

stories.yml

It contains different possible sample interactions between the user and the chatbot. With this sample, the bot will get an idea about what would be the possible reply for user input.

version: "2.0"
stories:
- story: happy 
  steps:
  - intent: greeting
  - action: utter_greeting
  - intent: mood_great
  - action: utter_happy
- story: sad path
  steps:
  - intent: greeting
  - action: utter_greeting
  - intent: mood_unhappy
  - action: utter_cheer_up
  - intent: goodbye
  - action: utter_goodbye

domain.yml

This file contains different bot responses, lists all the intents and entities used while creating the nlu.yml file.

version: "2.0"
intents:
  - greetings
  - goodbye
responses:
 utter_greeting:
  - text: "Hii How are you?"
  utter_goodbye:
  - text: "Bye"

actions.py

This is the python file to run the custom actions. This file can be used for an API call or database querying

from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
class ActionHelloWorld(Action):
    def name(self) -> Text:
        return "action_hello"
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        dispatcher.utter_message(text="Hello")
        return []

When the action is action_hello the above code will execute and dispatches the text “Hello” as a reply.

Training

We can train our model based on the data we provided in the above files.

rasa train

When we run the above code in the terminal, rasa will start to train both nlu and core model and then stores the trained model in the models folder.

Now let’s check the working of the bot. We can interact with the bot in a command shell

rasa shell

With this command, the rasa will load the trained model and we can interact with the bot in the shell

rasa shell

We can stop this rasa shell either by cntrl+c or by typing “/stop”.

RASA Interactive

We can set up our rasa bot in an interactive mode.

rasa interactive

While starting the interactive mode, it will first train a model followed by opening an interactive session. You can chat with your bot in this interactive session and can correct the prediction done by the bot. After the interactive session, you may generate some more training data and then train a model for better prediction.

model for better prediction

Conclusion

That was all about creating a simple basic chatbot. I hope you might have got an idea of the working of the RASA chatbot. We can discuss the development of an end-to-end chatbot in the coming article.

Thank You..!

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion

Python Developer, ML Enthusiast, Blogger and an Electronics and Communication Engineering aspirant determined and motivated to finish tasks with atmost sincerity and dedication.I'am a good learner who ready to accept challenges to bring up my best even in the worst. Wish for a world with enough advancements and opportunities for the people.

Responses From Readers

Clear

Prableen
Prableen

Thank you basil saji for explaining it in such simple manner. glad I found your article.

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details