Rasa Tutorial 1: How to Build a Custom Chatbot Agent with Rasa

This tutorial will walk you through the steps to create your first custom conversational chatbot using the community edition of the Rasa framework.

Before we dive into the details of this development process, I would like to mention that this tutorial will be semi-technical. This means that it will include steps that will require installing some software but not necessarily writing any code.

Why Develop a Chatbot?

Chatbots are very useful tools that can help you optimize your business by providing instant and accurate responses to your customers at all times. Chatbots are useful in many business processes such as tier-1 customer support, internal knowledge base question and answer, employee training and many other use cases.

What is Rasa?

Rasa is a framework that allows you to develop custom chatbots trained on your data and instructions. It allows you to build your own custom chatbot the way you want. This is a great tool that you can use to develop chatbots for your own business or for your clients as a service.

Install Rasa and Dependencies

I chose Rasa for this tutorial because it is a powerful framework for building chatbots. Let's start by installing Rasa on your machine so you can start the journey of building your first custom chatbot.

Step 1: Install Docker

I found out that the easiest way to run Rasa is by using its Docker image and deploying it into a container on my machine. Let's just do that by first installing Docker.

To install docker on your machine visit the link below to download it for your platform.

https://docs.docker.com/get-docker/

Step 2: Download and Setup Rasa

In this step you will download the Rasa image to your machine and then initialize Rasa inside a docker container. To do that first create a new directory for your project and call it whatever you want. From the command line run the following command to go to your project directory.

cd {your_directory_name}

Next, download the image and setup Rasa in the container by running the following command.

docker run -v $(pwd):/app rasa/rasa:latest init --no-prompt

Once the above command finishes. Run the following command to list all files in the current directory.

For Mac or Linux

ls -l

For Windows

dir

You should see the following files and directories:

Project Structure

Let's go over the project structure to better understand it.

actions

This directory is where you define your custom actions as classes. We will get into that in another tutorial about custom actions.

models

Rasa will save versions of your trained models in this directory.

data

The data directory includes chatbot training data in configuration files. In this directory you will find the following files:

  • nlu.yml: This file includes definitions for user intents. An intent definition declares certain phrases as an example of a user's intention. We will go over some examples later in this tutorial.

  • rules.yml: Rules are defined in this file. They link intents to system actions such as a response. Rasa rules are for simple single-turn conversational flows. For example, when a user greets the chatbot, it will greet the user back.

  • stories.yml: Stories are used for more complex multi-turn conversations. For example, the chatbot asks the user a yes or no question. Depending on the answer the chatbot can take one of two paths to figure out the response.

tests

Directory for test files.

config.yml

Main project config file. This file defines some system wide configs such as pipelines and other settings.

domain.yml

This file contains configs related to your business domain. Some of the important sections defined in this file are intents, responses, actions, entities, slots and other settings.

credentials.yml

File for credentials needed by the system to integrate to other services.

endpoints.yml

This file contains configurations for system endpoints such as the actions server URL and other endpoints.

This is a quick overview of the project directories and files to give you an idea about the project structure. By default, when you initialize your first project, you will have a basic chatbot with a few default responses defined. Before we customize the new chatbot, you can already chat with it.

Chat with the Bot

Now, you have a basic chatbot setup and ready to chat. Run the following command in your terminal to chat with the bot:

docker run -it -v $(pwd):/app rasa/rasa:latest shell

Steps to Build your Custom Chatbot

To build a chatbot in general with any framework there are a set of steps that you will need to follow. I will list the steps and then we will implement each step using Rasa in the following sections of this tutorial.

Step 1: Define Your Objectives and Use Cases

Before diving into the technical aspects of building a chatbot, it's crucial to clearly define your objectives and use cases. Ask yourself: What purpose will the chatbot serve? Who is the target audience? What problems will it solve? By identifying your goals and use cases upfront, you can ensure that your chatbot is designed to meet the specific needs of your users and your business.

Let's answer these questions for our case.

What purpose will the chatbot serve?

Our chatbot will serve as a tier-1 customer service agent for our web hosting company. It will inform our customers about general questions about the company such as operating hours and hosting plans.

Who is the target audience?

The target audience in our scenario will be our website visitors and existing clients.

What problems will it solve?

Our solution will solve two main problems. They are as follows:

Customers are not Getting Answers Quickly

Customers are looking for quick answers but they have to browse the website or contact customer service to get them.

Customer Support is not Available 24/7

Customers might have to wait when customer support is available to get answers.

As we can see these two problems can affect a business's customer satisfaction level and reputation. So let's help our customers by building a 24/7 chatbot available for them to ask away :)

Step 2: Design Conversational Flows

In this step we will be designing our conversational flows for the chatbot. Based on our goals for the chatbot we want to add some information about four things. And they are as follows:

  • How the chatbots responds regarding its identity
  • Information about the company in general and what it offers
  • Information about hosting plans
  • Information about customer service and how to contact them

Step 2.1: Add User Intents

You need to define user intents for your new conversation flows. This should cover three extra pieces of information we will add to the user intents defined in the data/nlu.yml file in the project.

When you open this file it should look like the image below:

Add the following lines at the end of the file

- intent: identity
    examples: |
        - who are you?
        - what are you?
        - who developed you?
        - who created you?
        - what can you do?

- intent: company_info
    examples: |
    	- tell me about your company
        - what does your company offer?
        - why should I use your service?
        - what does your company do?
        - where is the company located?

- intent: hosting_plans_info
    examples: |
        - list your plans
        - list your hosting plans
        - what are your hosting plans?
        - what hosting plans do you offer?
        - which hosting plans do you offer?
        - how much are your plans?
        - how much do your hosting plans cost?

- intent: customer_service_info
    examples: |
        - I want to contact customer service
        - what is your customer support number?
        - what is the customer service email?
        - what time is your customer service available?
        - what is the link for your customer support live chat?
        - How can I contact customer service?
Explanation

The lines we added above define four new intents namely identity, company_info, hosting_plans_info and customer_service_info. Each definition in this file tells the system to match user input against the given examples. We will see how to tie each intent to an action in the next section.

Step 2.2: Add Responses

In order to activate our new intents and define responses we will need to edit two sections in the domain.yml file in the project root. Open the file and find the intents section of the file and add the following entries to it:

- identity
- company_info
- hosting_plans_info
- customer_service_info 

Next find the responses section and add the following to it:

utter_company_info:
- text: "We are Hosting Company, Inc. We are pioneers in our industry and we put customer satisfaction, and the quality of our 	service as top priorities.
      We guarantee top quality service and customer service to our clients. Our infrastructure is state-of-the-art and is ready to sastisfy you hosting needs.
      We offer premium hosting plans and other addons such as Virtual Private Servers and Deidicated servers to our customers."

utter_customer_service_info:
- text: "You can contact our customer service agents by phone on 888-986-9905 or by email at cs@hostingcompany.com.
      You can also chat live with one of our agents at http://hostingcompany.com/livechat"

utter_hosting_plans_info:
- text: "We offer the following hosting plans:
    * Bronze - 20 GB Storage, 1 domain, SSL certificate - $15 / month
    * Silver - 50 GB Storage, 2 doamins, SSL certificates - $25 / month
    * Gold - 150 GB Storage, 5 domains, SSL certificates - $40 / month"

Step 2.3: Add Rules

To link each of our new user intents to responses we have to define rules. Open the file data/rules.yml and add the following rules to it:

- rule: Respond with info about chatbot identity
    steps:
        - intent: identity
        - action: utter_iamabot # this response action is already defined in `domain.yml`

- rule: Share customer support information
    steps:
     	- intent: customer_service_info
     	- action: utter_customer_service_info


- rule: Share hosting plans information
    steps:
    	- intent: hosting_plans_info
    	- action: utter_hosting_plans_info


- rule: Share company information
    steps:
        - intent: company_info
        - action: utter_company_info
Explanation

Each of these rules links an intent definition to an action. For our simple project all of our actions are just responses. However, Rasa enables you to write custom actions which can execute code to process the request. For example, you can add a custom action to notify customer service agents about a livechat request. You can also implement any custom business logic you need. This is out of the scope of this tutorial, but will be discussed in another tutorial.

Step 3: Train your Chatbot

Now we added all the configurations needed to make our chatbot respond how we want it to according to our plan. It is time to train our new chatbot on the new configurations and data we added. To do that run the command below from the project directory:

docker run -v $(pwd):/app rasa/rasa:latest train

This command will take a few minutes, wait for it to finish.

Step 4: Test the Chatbot

Congratulations, you have your custom chatbot ready for testing. To chat with your newly trained chatbot use the commmand below:

docker run -it -v $(pwd):/app rasa/rasa:latest shell

You can start testing by asking questions related to the company, hosting plans or customer service information. The chatbot should reply with our new responses we added. Here is a screenshot of my tests after training the chatbot.

Step 5: Deploy your Custom Bot

Deploying your custom chatbot is not an easy feat and depends on a lot of factors. For example, do you want to integrate it to your website or maybe your social media channels, does your host support Python? It all depends on your situation. The easier case to consider here is deploying to your website given that you have a host that supports Python or supports Docker container hosting. But that is still a big task which I will discuss in another future tutorial.

Conculsion

This tutorial went over the process of developing a simple conversational chatbot using the Rasa framework. Rasa is a powerful tool that allows you to develop custom chatbots based on your own rules and data. We went over how to install all needed software and how to configure our Rasa chatbot to respond with our pre-defined responses. There is a lot more that could be done with Rasa such as custom actions and handling more complex multi-turn conversations, but I will cover this in other future tutorials.