A practical guide to Rasa chatbot — Part 01

Rakesh Panigrahy
Chatbots Life
Published in
6 min readSep 22, 2020

--

Are you new into machine learning or a tech freak, who aspires to build a chatbot? but don’t know how and where to start. Also wish to skip heavy documents of Rasa framework. Then don’t worry, you have landed on a right place. Here, you gonna learn how to build a conversational chatbot using Rasa in a simplest possible way. We will cover up the most necessary things. So grab a cup of coffee and sit back.

What is Rasa?

Rasa is an open source framework for building chatbot. The top features of Rasa are intent and entity recognition. There are basically 5 types of chatbots.

  1. Notification assistants
  2. FAQ assistants
  3. Contextual assistants
  4. Personalized assistant
  5. Autonomous organization of assistants

Here, we are at level 3, as we are gonna build a chatbot on a specific context. So without wasting time, let’s start with the installation of Rasa.

Installation of Rasa

  1. Create and activate a virtual environment (recommended) or you can install Rasa on base environment.
python -m venv myvirtualenvnameactivate myvirtualenvname

2. Upgrade pip

pip install -U pip

3. Install rasa

pip install rasa[full]

If you want to install Spacy to use in your Rasa, run the following command.

Note:- Sometimes it gives issue to create a sym link. So you should open your cmd as administrator

pip install rasa[spacy]
python -m spacy download en_core_web_md
python -m spacy link en_core_web_md en

Build chatbot

Navigate to your directory, where you want to create your bot and run,

rasa init — no-prompt

Your project will be ready within a minute with some files. Now let’s run our bot and see how it responds.

rasa shell

Hurray… We just created our first hello world chatbot.

Now let’s understand the files,

  1. data

a. nlu.md

It contains your training data. The all possible combination of sentences mapped with the intents. We can specify our entities as well.

Intent:- What exactly you want to tell your chatbot. The intent of your conversation.

Trending Bot Articles:

1. 8 Proven Ways to Use Chatbots for Marketing (with Real Examples)

2. How to Use Texthero to Prepare a Text-based Dataset for Your NLP Project

3. 5 Top Tips For Human-Centred Chatbot Design

4. Chatbot Conference Online

Entity:- A named entity is a “real-world object” that’s assigned a name — for example, a person, a country, a product or a book title.(According spacy definition).

Here the intent is ticket_booking and covered by [] belongs to different types of entities.

eg:- bus, train and flight comes under vehicle entity.

b. stories.md

It contains the real stories, where you have to map the action for each intent.

eg:- Here below * greet is the intent and for that we mapped utter_greet, and the text for utter_greet is defined inside domain.yml

2. models

In this folder, all the trained models are stored.

3. tests

It contains conversation_tests.md file, in which you can write your test sentences to check whether it is predicting right intent or not.

4. Config.yml

It contains the whole configuration part like pipeline and policies. Pipeline contains all the preprocessing and other methods to be performed on the text. For more info can visit rasa document.

Let’s understand the pipeline

a. WhitespaceTokenizer will split the sentence based on whitespace and then tokenizes each word.

b. RegexFeaturizer used to support the intent classification and entity extraction.

c. LexicalSyntacticFeaturizer moves with a sliding window over every token in the user message and creates features according to the configuration (see below). As a default configuration is present, you don’t need to specify a configuration.

d. CountVectorsFeaturizer creates bag-of-words representation of user messages, intents, and responses.

e. DIETClassifier dual Intent Entity Transformer (DIET) used for intent classification and entity extraction.

f. EntitySynonymMapper maps synonymous entity values to the same value.

g. ResponseSelector selects appropriate response.

Now let’s talk on policies,

a. Memoization Policy remembers the stories from your training data. It checks if the current conversation matches a story in the training data. If so, it will predict the next action from the matching story of your training data with a confidence of 1.0. If no matching conversation is found, the policy predicts None with confidence 0.0.

b. TED Policy policy has a pre-defined architecture, which comprises the following steps:

  • concatenate user input (user intent and entities), previous system actions, slots and active forms for each time step into an input vector to pre-transformer embedding layer.
  • feed it to transformer.
  • apply a dense layer to the output of the transformer to get embeddings of a dialogue for each time step.
  • apply a dense layer to create embeddings for system actions for each time step.
  • calculate the similarity between the dialogue embedding and embedded system actions. This step is based on the StarSpace idea.

5. Credentials.yml

It contains the configuration for webooks and integration with social media apps like slack and facebook.

6. domain.yml

It contains the all the response and actions to be returned.

7. endpoints.yml

This file contains the different endpoints your bot can use.

8. actions.py

Here you can write your own custom action for any specific intent.

Chatbot Workflow

Now let’s understand the workflow of our chatbot.

Let’s assume we are building a bot for a travel agency.

  1. So we define all possible combination of user queries in our nlu.md file as above picture.
  2. Then it checks that qeury fits to which intent.
  3. That intent is checked in stories.md and finds out the action name for that intent.
  4. That action name is searched in domain.yml file and if the action name is under response and the respone text is defined then it is returned.

Below is the pictorial representaion of rasa conversation.

Congratulation, you are done with the rasa basics. On our next part, we will uncover the advanced techniques and will try to build a contextual chatbot of our own choice.

Don’t forget to give us your 👏 !

--

--