Building a bot using RASA open-source machine learning framework

Apr 11, 2022by, Melvin Jacob Joseph

Uncategorized

RASA is a production-ready framework for building chatbots, which uses deep learning frameworks internally and has support for research and experimentation.  RASA approaches the conversational problem in a different way than the traditional method of using state machines with a bunch of rules which has the big problem of scaling. It is built on a reinforcement learning approach which learns the best solutions from experience.

Rasa NLU is an open-source natural language processing tool used for intent classification. That is, it helps us to extract structured information from users. 

How about building your own assistant which will reply to the conversations it is trained for. Interesting? 

Okay, let’s learn to build.

So in this blog post, We’ll build a conversational bot on Rasa stack which will interact with you and it can provide random facts if you ask.

Requirements

RASA open-source machine learning framework: Essentially, it is a set of open-source machine learning tools for developers that enable them to create contextual AI assistants and chatbots, and it is the leading open-source machine learning toolkit that allows developers to expand bots beyond answering simple questions with minimal training data.

Installation and building of a basic chatbot

Installation of  Rasa open-source framework is given below..

You will be able to install RASA open source utilizing python package manager pip. 

sudo add-apt-repository ppa:deadsnakes/ppa 

sudo apt-get update 

sudo apt-get install python3.7

sudo apt-get install python3.7-pip python-dev

python3.7 -m pip install virtualenv

python3.7 -m virtualenv venv

source venv/bin/activate

python3.7 -m pip install rasa

mkdir rasa

cd rasa

To start build a default chatbot type : rasa init

You can initialise it in the current directory and can give yes to train with the existing model.

Fig 1.

Congratulations you have successfully created a basic chat bot!!!

Lets see what happened. On initialising it will create some files. 


Fig.2 

I’ll walk you through some of the files.

  • nlu.yml :  This file contains the intents and the example words to which the intent is mapped. As shown in the image the words hey, hello, good morning etc will be mapped to the intent “greet”. Behind the screen the classification algorithm makes this work.


Fig.3

  • stories.yml :  This file holds a record of which action to be performed on for each intent. From the below image we can understand that the intent “greet” is in the story “happy path” and  is mapped to action utter_greet. And what is the action string associated with the action utter_greet that is stored in the domain.yml.

    Fig.4
  • rules.yml : As the name suggests we can specify rules to handle specific cases in the conversations. With the combined usage of rules and stories we can efficiently handle different conversation patterns. In order to enable rules we need to add the rule policy to our config.yml as given below.

Policies:

– … # Other policies

– name: RulePolicy


 Fig.5

  • Domain.yml – This file holds the response string mapped to each action. We can change any of these responses and can retrain the deep learning model to reflect the same in action.Domain includes what user inputs it should expect to get, what actions it should predict, how to respond and what things to store. The domain consists of intents, slots, entities, actions and templates.


Fig. 6

These files represent few of the concepts of the RASA framework. For deeper knowledge on concepts go through https://rasa.com/docs/rasa/glossary#!.

We should also note that the trained model is stored in the data folder. If we make some changes to the above file we need to execute the following commands to train and rerun.

  1. rasa train 
  2. rasa shell

Creating a new intend to call external api through an Action

Now we are going to create an action which is triggered on the intent “fact”.For example, in our bot, the sentence :”amuse me” has a fact  intent. We will create an action_fact which will call an external api to retrieve some random facts on cats. 

  1. A portion of the training data is shown below. We can mention the intents and its possible examples for the same. We can also add some spelling mistakes/slangs since that will make chatbot speak like humans. We will save this under data/nlu.yml

version: “2.0”

– intent: fact

  examples: |

    – fact

    – amuse

    – fact me

    – give me a fact

    – anything

 

– intent: greet

  examples: |

    – hey

    – hello

    – hi

    – hello there

    – good morning

    – good evening

2. Here is some portion of training data, that I have prepared for adding the intent fact and associating the action action_fact to it, also a story happy path can be seen for greeting and we will store this under data/stories.yml

– story: fact

  steps:

  – intent: fact

  – action: action_fact


– story: happy path

  steps:

  – intent: greet

  – action: utter_greet

  – intent: mood_great

  – action: utter_happy

3. Next, we’ll define the domain domain.yml.In domain we have added all our intents, action and responses for which the model has to be trained.Here is an example domain for our bot:

version: “2.0”

intents:

  – greet

  – fact

actions:

  – action_fact

responses:

  utter_greet:

  – text: “Hey! How are you?”

  utter_cheer_up:

  – text: “Here is something to cheer you up:”

    image: “https://i.imgur.com/nGF1K8f.jpg”

  utter_did_that_help:

  – text: “Did that help you?”

session_config:

  session_expiration_time: 90

  carry_over_slots_to_new_session: true

4. Next you can see a python script where We are going to make our Bot to make an API call to retrieve random facts on cats. We need to create a custom action for this purpose. Add the below code fragment  in actions.py                                                                          

from __future__ import absolute_import

from __future__ import division

from __future__ import print_function

from __future__ import unicode_literals

import logging

import requests

import random

import json

from rasa_sdk import Action

logger = logging.getLogger(__name__)

class ActionFact(Action):

 def name(self):

   return “action_fact”

 def run(self, dispatcher, tracker, domain):

   n = random.randint(0,4)

   request = json.loads(requests.get(‘https://cat-fact.herokuapp.com/facts’).text)

   fact = request[n][‘text’]

   dispatcher.utter_message(fact)

   print(fact)

   return []

5. Now we need to run actions.py file in debug mode using the   command

 rasa run actions –actions actions -vv 

6. In the endpoints.yml add the following lines to connect to our action endpoint

action_endpoint:

 url: “http://localhost:5055/webhook”

After preparing the training data and running the action, it is time to train rasa-core. To initiate the training we can feed the following command.

Rasa train

Fig 7. Training-rasa

The trained model will be saved at: models/

So it is time to test the action . I will be checking one basic conversational story that we have prepared to reply to. To start a discussion within the command line, type the following command.

Rasa shell

Fig 8. Rasa-shell

Conclusion

In this blog, We developed a chat bot which is capable of listening to the user’s input and responding contextually with minimal training with the help of RASA open source framework . We also created a simple action to return a random joke using an API call. Similarly it can be customized to a great extent depending on our requirements. RASA brings a vast opportunity to developers in developing chat-bots with ease. So now it’s your turn!!