Table of Contents

Building and Training a Custom AI Chatbot in Rasa

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on email

AI chatbot

Table of Contents

Do you remember having come across this infamous image of a PayPal virtual assistant’s conversation gone wrong, that quickly became an internet meme, not a long time ago:

Paypal Chatbot

Here, the PayPal chatbot clearly missed the user’s intention and message when they had clearly affirmed that they had been scammed! We will blame it on the poor chatbot’s naivety and age: after all, those were chatbots’ formative years.

The above scenario is most likely to happen if a chatbot is wholly scripted, from head to toe. Meaning, when encountered with a task that has not been written in its code, the chatbot will not be able to handle it properly, just like PayPal’s chatbot.

But now, the times are changing. With the advent and the inevitable proliferation of chatbots in web and mobile applications, we are seeing them utilize Artificial Intelligence (AI), especially machine learning (ML), to accomplish unseen tasks in real-time, that too at scale, cannot be matched by a team of interns or even veterans. Even better, companies are now able to gain insights by analyzing chatbot conversations with their end-users.

Let’s not forget, the unnatural chatbots of yesteryear were, by and large, rule-based chatbots. These chatbots don’t tend to remember the previous interactions with the users because they are powered by a relatively simpler machine learning technique called pattern matching.

To make a case in point, let’s take this question: “What is the cost of your subscription?”

Now, this same question can be asked in a number of different ways by customers in the future. The rule-based chatbot is taught how to respond to these questions—but the wording must be an exact match. This means manually programming all the different ways to ask how much a subscription costs, for every possible question a customer may ask, which is temporally costly. That’s not to say this type of chatbot can’t be useful: if your organization tends to get only a certain number of questions that are usually asked in just a few ways, then a simple rule-based chatbot is probably the way to go. But for many companies these days, this technology is not scalable enough to keep up with their customer queries and interactions.

ai chatbot

 

Welcome, the Chatbots of Tomorrow

The new generation of chatbots are Natural Language Processing (NLP) powered chatbots aka AI chatbots that have the ability to get smarter each day. They carry information forward to the next conversation and keep evolving as they go. To ensure your chatbot doesn’t go awry, it’s necessary to systematically train and send feedback to improve its understanding of customer intents using real-world conversation data being generated across different channels. To get the most out of your chatbot, you’ll want it to be set up as early and as simply as possible, with all the basic functionality that you need.

One of our enterprise-grade solutions includes an intranet portal called BOSSS where we have developed an AI-based customer service support using a Google Dialogflow chatbot. Moreover, Technology Rivers has built an in-house NLP-powered SaaS application that uses AI and NLP to analyze human interactions both in video and audio calls in real-time to empower businesses to make insightful decisions.

In one of Technology Rivers’ previous blogs, we have already walked you through the process of building a chatbot using Google Dialogflow. But since we are talking about chatbots of tomorrow here, one cannot miss out on one of the key players in this new age of conversational AI design—Rasa. Rasa provides an advanced NLP framework that lets you build and train an AI, task-oriented dialog system that can be put into production while it still continues to learn from real-world human interaction data.

In this article, we will be demonstrating how you can build and train a Rasa chatbot by setting up Rasa locally on your machine. But first, let’s try to dive deeper into what Rasa does and why businesses should choose it for meeting their customer-facing conversational interface needs.

 

What Is Rasa?

Rasa is a highly customizable, open-source, AI framework that makes it easy to make virtual assistants and custom chatbots using a combination of AI and machine learning approaches and heuristics for rules. It can support chatbot development for hobby projects to complex enterprise systems alike.

The core of building a Rasa chatbot/assistant is providing training examples that your system can learn from. Such examples should tell the system:

  • What are the different ways people phrase words to say something?
  • How do dialogues flow in a conversation?
  • What conversational patterns do you want your system to be able to handle?

While exploring any AI/ML platform, one must consider its limitations as well. One caveat with using Rasa is the cost attached to setting up Rasa environment locally. But when you do put in the time to set up Rasa, you are good to go and ready to build some chatbots!

 

Why Use Rasa?

As explained through an example above, for a chatbot developer, it can sometimes be really hard to know when we have given our system enough training data or examples to be able to roll it out into the production phase.

Building and Training a Custom AI Chatbot in Rasa 1

The real problem that most chatbot developers face is when we come up with our own hypothesized interaction paths and stories that we think our users are going to have with our chatbot, but later on, while in production, find out that our users actually behave quite differently from that. Here, the real challenge is to be able to find that sweet spot (as shown in the figure above) where the two can substantially overlap.

Luckily, Rasa identifies this caveat in building conversational AI, and therefore enables developers to put their virtual assistants and chatbots into production as quickly as possible through Rasa X. This way, our chatbot can start interacting with real users briskly. Additionally, these real-world interactions, in turn, create more training examples and data for our system that it can learn from and cover more grounds for machine learning model improvement.

Building and Training a Custom AI Chatbot in Rasa 2

Source: Rasa

 

Rasa vs. Google Dialogflow for Building Chatbots

Now, let’s try to pit Rasa and Dialogflow against each other in a friendly (read: competitive) setting to understand how they are similar to and different from each other from a chatbot development standpoint.

Ease of Development

As Rasa is primarily an on-premise platform and the open-source meaning developer has full control of engineering. It requires a setup and understanding of the platform to get started. Dialogflow provides an easy-to-use platform and an easy integration process to several channels which reduces most of the development time when compared to Rasa.

Setup and Installations

Truth be told – Rasa requires locally installing it and setting up the development environment prior to building chatbots using it. But Google Dialogflow, on the other hand, does not have any installation procedure overhead, and therefore, no infrastructure setup requirements.

Rule-Based vs. Neural Net-Based Dialogue Management

Rasa’s dialogue is designed and trained using stories. It’s the first-ever ML-based dialogue management approach combined with a rule-based approach that involves using a custom action file for creating tighter control over the flow. In short, a Rasa assistant can use both, but the neural methods are the core of the framework.

Dialogflow uses more of a rule-based approach and manages Dialogue via context itself.

Slot-Filling for Entities

Both Rasa and Dialogflow use slots to manage contexts. A slot is nothing but a memory of the assistant. Rasa’s Transformer (neural methods) based core takes slots, current intent, and entity to predict the next action of the virtual assistant.

Production and Deployment

This is an important decision factor for a lot of clients as most of them do not want the data to move through an external entity that isn’t in their full control. This is where Rasa wins without a doubt as it can be run locally. It can be deployed on-prem and on the cloud. Rasa allows you to run your assistant’s operations on your own infrastructure, without sending the customer messages to a hosted, third-party service for processing.

As Dialogflow is a Google Cloud Platform product, it is a UI-based bot framework. The entire project and of course models will be stored and deployed in the Google cloud. We only get the flexibility to implement the backend business logic on-premise or on other cloud platforms.

These are some of the many stark differences between Rasa and Dialogflow AI platforms used for developing virtual assistants. However, in this article, we will take through building a custom chatbot using the Rasa framework that we then deploy into one of our in-house mobile applications.

 

How to Build an AI Chatbot in Rasa

In this tutorial, we will utilize Rasa Open Source, along with Visual Studio and Command Line Interface to develop an AI chatbot.

Our chatbot should be able to:

  • Greet a user and ask how they are feeling
  • Respond to a user based on their mood
  • Do a weather check for a city

But first, let’s start by setting up the Rasa environment and its SDKs locally.

Install Python on Your Local Machine

Rasa SDK is a Python SDK. So first things first – we will install the Python programming language locally. You can do this by downloading Python from its website, and installing it.

Once done with installing Python, now based on your OS, complete installing Python packages and dependencies on your machine, for example, a virtual environment.

Install Rasa Open Source Through CLI Commands

Next up, we will install Rasa Open Source. For this:

  • We will open the command line interface or terminal on our machine.
  • We will activate a virtual environment through CLI commands. After making a separate directory (you can name it anything) using the CLI command mkdir, we will go to that directory and make an instance of virtual environment venv by typing the following command on CLI and press the return key:
python3.7 -m venv venv
  • Now, let’s activate this venv instance through the following command:
source venv/bin/activate
  • Once the venv instance has been activated, we are now going to directly install rasa through this command:
python -m pip install rasa

This is going to install Rasa onto our local machine. Once the installation has finished, you will be provisioned with the command line prompt. If you haven’t used Rasa in a while, consider upgrading Rasa before you begin, using the command:

pip install --upgrade pip

Initialize a Project in Rasa

Now that we have our Rasa installed and virtual environment venv activated, we will go ahead and initialize a new project in Rasa. This will create a clean starter kit for us to play within Rasa. Additionally, with the help of python -m rasa –help command, you will be able to see a list of all the possible CLI commands for Rasa.

Alternatively, to initialize and run Rasa, type in the following CLI command and hit the return key:

python -m rasa init

In a few seconds if everything works out, you should see a welcome message from Rasa and a command line prompt that looks like this CLI snippet below:

Building and Training a Custom AI Chatbot in Rasa 3

We will press the return key and continue typing Y until Rasa has set up and prepared a fundamental file system needed to train an initial machine learning model for a chatbot in Rasa as shown below:

Building and Training a Custom AI Chatbot in Rasa 4

We will hit the return key and let Rasa train an actual Rasa model for us. On the CLI, you will see logs of different systems being trained.

Train and Interact With the Rasa Chatbot on Command Line

We have up till now trained our virtual assistant (chatbot) on the command line. At the culmination of the chatbot’s training, you will see a prompt asking you if you want to test your newly trained Rasa chatbot:
Building and Training a Custom AI Chatbot in Rasa 5Type Y and we can now start interacting with Rasa’s open-source chatbot:

Building and Training a Custom AI Chatbot in Rasa 6

This chatbot essentially tries to respond to the user based on their mood. It basically greets the user back by saying “Hey! How are you?”. The user tells the chatbot “I am sad”, to which the chatbot responds by sharing a link to a cute photo to cheer them up. The chatbot then asks the user “Did that help you?” and when the user replies with a no, the chatbot bids farewell to the user “Bye”.

We will then exit from the chatbot.

Build the Chatbot Using Visual Studio Code

In order to customize and understand this open source chatbot even more, we will open Visual Studio Code from the command line using the shortcut:

code .

Now we will continue the rest of our chatbot tutorial on Visual Studio Code. We will open the terminal tab as shown below:

Building and Training a Custom AI Chatbot in Rasa 7

We will activate our virtual environment venv, using the same command we used on the command line earlier. You can open the files data/nlu.md and data/stories.md in Visual Studio Code and customize them to add/edit intents to make this open-source chatbot more advanced. Don’t forget to type the command rasa train on the Visual Studio Code’s Terminal whenever you want to retrain and ultimately test the updated chatbot using the rasa shell command.

Rasa Configuration File System

Before we move on with our chatbot development on Visual Studio Code, let’s take a brief pause and look at some of the important files that we can see in the left side Editor, which are created as the initial project structure of Rasa:

  • actions.py contains your custom actions. In case you want Rasa to call an external server via REST API or API call, you can define those Custom Actions here, as we will do soon in this tutorial.
  • config.yml has the configuration of your NLU and core models. If you are dealing with Tensorflow or Spacy, you need to define such pipelines here.
  • data/nlu.md has your NLU training examples. Here you can define intents.
  • data/stories.md enlists all your conversation stories and patterns.
  • domain.yml contains your virtual assistant’s domain. This file combines different intents which the chatbot can detect and the list of responses that it can give.
  • endpoints.yml specifies the connecting channels for chatbot deployment. This is mainly used for production setup.
  • models/.tar.gz stores the chatbot model

Write a Program to Perform a Custom Action

An action can run any code you want for your chatbot. Custom Actions in Rasa can turn on the lights, add an event to a calendar, check a user’s bank balance, or anything else you can imagine. Whenever a custom action is probable, Rasa will call a specified endpoint for you. That endpoint would, in turn, run a code and could optinally modify the dialogue state by returning some piece of information back to the user.

For our chatbot, we will create a custom action that can give a weather check on demand using a weather API.

Write a Python Code for Calling the Weather API

Below is the script named weather.py that we will be using for calling a weather API. We will code and save this in a separate file in our chatbot directory:

import requests

def Weather(city): 
api_address='http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='

url = api_address + city 
json_data = requests.get(url).json() 
format_add = json_data['main'] 
print(format_add) 
return format_add

Define a Custom Class in Rasa’s actions.py file

Now, let’s write our own custom action in Rasa from scratch. In order to call the method Weather, we will define a class called ActionCheckWeather by copying and pasting the following code in the actions.py file:

from rasa_sdk.events import SlotSet
from weather import Weather

class ActionCheckWeather(Action):
def name(self) -> Text:
return "action_weather_api"

def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

city=tracker.latest_message['text']
temp=int(Weather(city)['temp']-273)
dispatcher.utter_template("utter_temp",
tracker,temp=temp)

return []

Configure the Custom Action With the Chatbot

Add action_weather_api action in the domain.yml file, as shown below:
Building and Training a Custom AI Chatbot in Rasa 8

Add the following intents and entities to the nlu.md file:

## intent: weather 
what’s the weather today
i want to know the weather today
tell me the weather forecast for today
how’s the weather today

## intent: city 
[tokyo] (city_name)
[new york] (city_name)
[berlin] (city_name)

## lookup: city_name
data/cities.txt

As you can notice in the code snippet above, we have added an intent here called city that uses the Rasa entities and slot-filling functionalities in it. Here, we can see that our chatbot has some examples for this intent such as:

  • [tokyo] (city_name)
  • [new york] (city_name)
  • [berlin] (city_name)

where in this syntax, [*] with the square brackets is symbolizing an entity, and (*) is symbolizing the entity type.

Here, the entity type city_name is mapped to a Rasa lookup table utility, as shown above. This lets the virtual assistant know that it needs to lookup in the file data/cities.txt. This saves us from giving all the examples with all the cities names to train our chatbot on how to identify them.

Add the following to the stories.md file:

## happy weather
* greet 
- utter_greet
* weather 
- utter_city
* city 
- action_weather_api

After this, you are good to go. Re-train the Rasa model and run the Rasa shell from Visual Studio Code using the CLI commands to test your chatbot.

 

How to Integrate Rasa Chatbot With a Custom Web or Mobile Application

Now that the Rasa chatbot is all configured, it’s time to integrate it into a custom web or mobile application.

For this, the Rasa Chat Widget can be utilized. After setting up the SocketIO channel, the following code given below can be pasted into a web application. The URL of the Rasa instance should be pasted into the data-websocket-url attribute, as shown below:

<div id="rasa-chat-widget" data-websocket-url="https://your-rasa-url-here/"></div>
<script src="https://unpkg.com/@rasahq/rasa-chat" type="application/javascript"></script>

Building and Training a Custom AI Chatbot in Rasa 9

This is just one of the ways the Rasa chatbot can be deployed in your web apps. The process for integrating the Rasa bots with mobile apps varies too, depending on your specific use case and organizational needs. Feel free to reach out to us if you have any specific questions or need help in integrating your chatbot with your web or mobile application.

 

Wrap Up

This culminates our tutorial on how you can build an open-source Rasa chatbot from scratch and deploy it into a custom mobile app for meaningfully transforming how customers communicate with your business.

In this tutorial, we learned how to build, train, test and integrate our custom-made Rasa chatbot to greet customers and give them a weather check on demand.

We used Rasa Open Source to build our chatbot from scratch, using both the command line interface and Visual Studio Code.

Additionally, we learned about some differences between Rasa’s conversational AI framework and Google Dialogflow from a development standpoint.

 

Improve your user’s app experience. Integrate a Custom AI Chatbot using Rasa in your mobile app and simulate human conversations. Feel free to reach out to us and let’s discuss your app ideas!

Do you have any additional thoughts that you want to share with us? Share them with us via our Facebook and LinkedIn pages.

 

 

References:
https://rasa.com/docs/rasa-x/
https://www.youtube.com/watch?v=fqzsE70Rvr0&list=PL75e0qA87dlEWUA5ToqLLR026wIkk2evk&index=2
https://www.youtube.com/watch?v=lWP1K-IcAN4&list=PL75e0qA87dlEjGAc9j9v3a5h1mxI2Z9fi&index=2
https://rasa.com/docs/rasa/connectors/custom-connectors
https://innovationyourself.com/calling-weather-api-in-rasa/
https://towardsdatascience.com/rasa-vs-dialogflow-faceoff-part-2-6a4692b25b71
https://www.youtube.com/watch?v=GlR60CvTh8A
https://www.youtube.com/watch?v=Ap62n_YAVZ8&list=PL75e0qA87dlEjGAc9j9v3a5h1mxI2Z9fi&index=2
https://rasa.com/blog/level-3-contextual-assistants-beyond-answering-simple-questions/
https://www.youtube.com/watch?v=sazsWmP2d3o
https://github.com/RasaHQ/NLU-training-data
https://rasa.com/docs/action-server
https://rasa.com/product/why-rasa/
https://rasa.com/docs/rasa/connectors/your-own-website
https://towardsdatascience.com/create-chatbot-using-rasa-part-1-67f68e89ddad

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on email
Email
Hiba Latifee

Hiba Latifee

Similar Blog Posts