Chatbots and how we built one using Rasa
Tags: chatbot, machinelearning, rasa, ai
Published: 2020-08-18 04:19:48

If you are reading this, chances are high you have spoken to a bot.

Yup, you who is reading this. You. Have. Spoken. To. A. Bot.

Before you go all Terminator on us and close this window and scroll away to another chatbot story that you found interesting out there on Google, bear with us.

![](https://cdn-images-1.medium.com/max/960/0*VDv6tDI2j-Zh1ZEQ)

<figcaption><a href="https://giphy.com/gifs/ill-be-back-rOsebqhlfCRby">Image source</a></figcaption>

Chatbots are those interesting little guys who occupy a corner of your screen, usually the bottom right (don’t look, we don’t have a chatbot, yet) and help you with a question that you need an answer to.

Questions like:

-   What are your pricing options?
-   Where can I find more details about your product?
-   Do you have any client testimonials to back up your claims?
-   Where can I stash a body if I don’t own a ranch and live ina two-room apartment with three other guys?

The chatbot may have some trouble with the last question. Moving on.

Chatbots represent a form of marketing called Conversational marketing, which has been around for as long as we can all remember.

When you used to walk into a store maybe a decade ago and ask a salesperson about the right kind of vacuum cleaner for your house, that was Conversational marketing.

Ever picked up a phone and spoken to a company rep who explained to you why the “Toastmaster 5000” will solve all your breakfast problems? Yup, that’s conversational marketing too.

Chatbots are just taking those conversations to a whole another level. And they seem to be working too. [This](https://www.gartner.com/en/newsroom/press-releases/2018-02-19-gartner-says-25-percent-of-customer-service-operations-will-use-virtual-customer-assistants-by-2020) Gartner report says that 85% of all customer interactions will be managed without humans as soon as 2021.

![chatbot market size](https://cdn-images-1.medium.com/max/960/0*3uMbH5M3cGXJNdbY)

<figcaption><a href="https://www.tidio.com/blog/chatbot-statistics/">Image credit</a></figcaption>

Add to this other interesting Chatbot statistics such as:

-   40% of all online shoppers, irrespective of age, prefer to use a chatbot, according to this Tidio [report.](https://www.tidio.com/blog/chatbot-statistics/)
-   According to this Forbes [report](https://www.forbes.com/sites/andrewarnold/2018/01/27/how-chatbots-feed-into-millennials-need-for-instant-gratification/#56b5e4673675), three out of five millennials have used Chatbots at least once in their lives,
-   63% of customers feel that businesses of the future [should](https://chatbotslife.com/messenger-chatbots-the-stats-dont-lie-f8cd640152f1#:~:text=63%25%20of%20consumers%20believe%20businesses%20should%20be%20on%20Messenger&text=Messenger%20is%20fast%20becoming%20the,should%20keep%20it%27s%20communication%20relevant.&text=By%20building%20a%20chatbot%20for,business%20and%20get%20immediate%20answers.) be conducted on Messenger.
-   By 2021, 50% of all companies will spend more on chatbots than on traditional mobile apps.

We now know chatbots are cool, and that they can lead us to some serious moolah, but we need to brush up on a few basics before we get down and dirty building one.

## **Getting the basics right**

Chatbots are cool. You get it. But should you actually invest your time and money building a chatbot, and, if yes, what framework should you choose?

To answer these questions, we turn to Master Shifu from Kung Fu Panda, who said something along the lines of

![Master Shifu from Kung Fu Panda](https://cdn-images-1.medium.com/max/960/0*mDe6fHNFl2aJEIiD)

<figcaption><a href="https://lifenlesson.com/fictional-teacher-inspirational-quotes-by//">Image source</a></figcaption>

Figure out what problem you and your website are trying to solve, and if a chatbot will help the customer in his user journey.

Start by asking yourself these simple questions:

-   Should my business by available 24*7?
-   Does a chatbot make sense when most of my business is B2B, and customers prefer to speak to a sales rep?
-   Do my customers want faster customer service?
-   Do I need to lower my labour costs?
-   Can customers find everything easily on my website, or do they need more personalized service?

If your answer is YES to most of the questions above, a chatbot is a must for your business.

We will now look at the structure of a chatbot, telling you important definitions that you need to learn if you wish to move past the “Chatbot- Noob” stage.

A chatbot usually consists of 3 components: A messaging platform, a Natural Language Processing Engine, and a Database.

We won’t get into the nitty-gritty details here, since we can’t wait to show you how we built a Chatbot using Rasa, but here is a graphic we found online that will give you a better idea.

![basic process of one type of chatbot](https://cdn-images-1.medium.com/max/960/0*exEpyFnOl3sPnVw4)


<figcaption><a href="https://www.enterprisebotmanager.com/chatbot-architecture-and-how-they-work/#:~:text=In%20a%20simple%20summary%2C%20chatbots,NLP%29%20engine%20and%20a%20database.">Image source</a></figcaption>

Now you need to remember three key chatbot terms before we move further

-   **Intent:** The intent of a visiting user answers the simple question — What does a user want? Let us say the user says something like:

![](https://cdn-images-1.medium.com/max/960/0*eMpW9CE-7ha2_5Aw)

The intent of the user would be  `book_flight`_._

-   **Entity:** Entities are the buckets of data that is used in the conversation between you and the chatbot. These are the blocks that you would like to send to the server to process later on, and consists of information you want to capture from the user.

![](https://cdn-images-1.medium.com/max/960/0*GZNFEKHh3E7-Qh3Q)

Hre the entities would be: `from_city`**_:_** _Los Angeles_

`to_city`**_:_** _New York_

-   **Stories:**

Think of stories as to how you would like the conversation to flow. Different responses you give to the chatbot will have different replies. You are telling the chatbot the various combinations that are possible in the conversation.

Think of it as teaching sentence formation to a seven-year-old You need to train the chatbot on all the possible responses a user can give.

Once you have mastered these basics, you are now ready to build a chatbot on your own. Let us see how that pans out:

## **How we built a simple conversational chatbot**

**Step 1:** First, we’ll install python 3.

**Step 2:** Create and activate the virtual environment using the below commands.

-   `python3 -m venv ./venv`
-   `source ./venv/bin/activate`

**Step 3:** Install Rasa open-source with the below command. If you are facing any issues, you can explore the [documentation](https://rasa.com/docs/rasa/user-guide/installation/).

-   `pip install rasa`

Once the packages installation is done, let’s create the project directory.

`mkdir v-assistant`

`cd v-assistant`

Simple so far? Now things get a bit more interesting.

Create the new project with `rasa init-no-prompt` command.

Once the project is created, you will see below logs in the terminal.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*5ji8CQ7Sz7IxO4MV)

The `rasa init`_command_ creates all the files that a Rasa project needs and trains a simple bot on some sample data.

You can play around with this data to get the hang of things. Remember training a seven-year-old that we mentioned earlier? Follow the same principle.

This creates the following files:

![](https://cdn-images-1.medium.com/max/960/1*c23Gvm5qMM1811passklww.png)

Let’s interact with the sample bot to see if the execute `rasa shell` command from the terminal is working.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*0R-PiUEljvdiAJgw)

As you can see, we can interact with the sample data.

Now let’s see how we can make the bot more interactive, for which we need… drumroll…**stories!**

We have told you what stories are before, but we are going a bit technical here for people with some coding background who like big technical words.

A story is a representation of a conversation between a user and an AI assistant, converted into a specific format where **user inputs are expressed as corresponding intents** and entities ( where necessary ), while **the responses of an assistant are expressed as corresponding action** names.

We’ll first add intent, then it’s the corresponding action, and finally, we’ll train our bot to do what we asked it to do. Hell Yeah!

Let’s say we want a chatbot to speak a name when the user asks the name of our chatbot. So we’ll add intent for that in the **intent.md** file.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*RUZxQDeP7jpHF79c)

Here ‘##intent’ defines the intent _e.g.- ##intent:welcome, ##intent:name_. Also, we have to register the intent in our **domain.yml** file in the intents section, as shown below.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*A8vIzjL5buz8cASr)

After the intent, we’ll define the action for that intent in the same file under the responses section.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*jiK1v-HKNZzyp_oN)

Now that you have defined the action, we have to register that action in the same domain file under the actions section.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*uDgCLYZO74MMEg6S)

After adding the appropriate action and intent, we’ll define the story in **stories.md** file.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*Ck5-djN7QzhGRlaD)

Now, as our NLU data and stories are ready, it is time to train the bot.

We can do this with `rasa train`command in the terminal.

After execution run the command  `rasa shell`for interaction.

![Chatbots and how we built one using Rasa](https://cdn-images-1.medium.com/max/960/0*-FeaWH3KJ-ry_nTV)

As you can see, the bot will utter its name when the user asks for it. The action which we have added is utterance action, which starts with **utter_** and sends a specific message to the user.

Rasa has provided more information about the action [here](https://rasa.com/docs/rasa/core/actions/).

This way, you can add N number of intents, actions and make more user stories.

We still need to build the dialogues and stories with some custom actions, entities, and slots so that bot can respond to the messages.

As a bonus point, we can integrate it with different channels and web applications.

I would recommend going through each section step by step and reading the Rasa Documentation to understand it better.

## **Parting words**

This is just grazing the surface of building Chatbots, and if you want to go deep, we would recommend you to compare frameworks like Rasa in [this](https://chatbotsjournal.com/25-chatbot-platforms-a-comparative-table-aeefc932eaff) post and choose one that you like.

Once you pick a framework, the world is your chatbot’s oyster, and you can program it to help out any customer that comes to your site, except maybe the shoddy character with a dead body that we mentioned in the beginning.

You can also take your chatbots to the next level by asking your chatbot to send meaningful data to a third party API and then perform an action in that system.

For instance, you can connect your chatbot to Salesforce and ask your visitor for his basic information like name and email. If the person has interacted with your business before, you can provide him a personalized user journey by doing away with the initial questions. The possibilities are limitless!!

Now that you know why chatbots are important and how to build a chatbot using Rasa, we feel it is time to pick up that lightsaber, young Skywalker. If, at any point, you feel stuck or want to build a bot for your business, send us a shoutout here at [SoluteLabs.](https://www.solutelabs.com/)