It seems like a lot of websites these days are equipped with a Chatbot. It’s used for customer service, simple Q/A, and more.
As chatbots have grown in popularity, so have the number of different frameworks that can be used to implement them.
In this blog, we will explore one of these frameworks, Rasa. First, I’ll cover some of the basics of chatbots. Then, I’ll explain how to write a simple Chatbot using Rasa, and we’ll highlight some of its additional functionality as well.
Let’s get started!
What is a Chatbot?
A Chatbot is an artificial intelligence software capable of carrying out basic conversation with humans via text or text-to-speech. Chatbots are usually developed in the hope that end users will not know they are talking to a robot.
Why use a Chatbot?
There are several benefits to implementing a Chatbot.
Rather than having the user search in other places, chatbots can serve as a centralized location for the information available immediately accessible by the user.
Chatbots are also always available, whereas some live chats are only open during business hours.
Additionally, as Chatbots become more advanced, they are capable of fulfilling the needs of customers, which can cut spending on customer service-related costs.
An Overview of Rasa
Rasa is an open-source machine learning framework capable of automated text or voice-based conversations.
It is great for building scalable contextual assistants that meet many different needs. It can easily be used even without prior machine learning or chatbot development experience.
Rasa chatbots are generally set up in three main parts: the Natural Language Understanding (NLU) data, responses, and stories.
Natural Language Understanding (NLU)
The NLU is responsible for how the bot interprets user input. By defining intent and providing some examples, the chatbot will be able to recognize phrases regardless of how the user enters their message.
Responses are phrases that the chatbot uses to respond to the user. We are able to add multiple text options per response. One will be selected at random when that response is used.
Stories are example conversations that train the chatbot how to respond when a specific intent is encountered.
Demo: A Simple Chatbot with Rasa
With the basics out of the way, let’s dive into our demo. I’ll walk you through installation, and then we’ll move into setup.
Requirements: Python 3.6, 3.7 or 3.8
We will use pip to install Rasa. First, make sure it is up to date:
pip3 install -U pip
As long as everything checks out, we can now install rasa:
pip3 install rasa
Once Rasa has successfully been installed, you’re all set to move on to the next phase!
First, let’s create a new project with some base code preset for us:
For our purposes, we will only be using the base project provided by the initialization. This project is set for a basic greeting, asking how the user is, and responding based on how the user is doing.
Take a moment to look around at the project structure on your own. The user’s intents are defined in
data/nlu.md, bot’s responses are in the
domain.yml, and stories are in
Once the NLU data, responses and stories are defined. We can train the bot using the rasa CLI:
We’ll run the bot from the command line as well with:
Once our bot is running, we can begin talking to it:
bradens-mbp:rasa bniswonger$ rasa shell 2021-05-19 23:25:44 INFO root - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument. 2021-05-19 23:25:44 INFO root - Starting Rasa server on http://localhost:5005 Bot loaded. Type a message and press enter (use '/stop' to exit): Your input -> Hello Hey! How are you? Your input -> Not good Here is something to cheer you up: Image: https://i.imgur.com/nGF1K8f.jpg Did that help you? Your input -> yes Great, carry on! Your input -> Goodbye Bye
Now that we have a generic bot running, let’s add another path for the user to go. First, we’ll add a new intent:
## intent:best_nfl_team - which nfl team is the best? - what is the best team in the nfl? - who is better than all the rest? - are the chiefs the best team?
Next, we’ll add the response:
utter_cheifs: - text: "Obviously, the Kansas City Chiefs." - text: "The Chiefs are the best team in the NFL."
Note: We will also have to add
best_nfl_team to the list of intents in the
Now that the intent and response are defined, we can create a story:
## best nfl team * best_nfl_team - utter_chiefs
After retraining, the chatbot should now be trained on who is the best team in the NFL.
bradens-mbp:rasa bniswonger$ rasa shell 2021-05-19 23:58:03 INFO root - Connecting to channel 'cmdline' which was specified by the '--connector' argument. Any other channels will be ignored. To connect to all given channels, omit the '--connector' argument. 2021-05-19 23:58:03 INFO root - Starting Rasa server on http://localhost:5005 Bot loaded. Type a message and press enter (use '/stop' to exit): Your input -> Who is the best nfl team? The Chiefs are the best team in the NFL.
More Features of Rasa
Our demo is finished, and we’ve successfully created a simple chatbot using Rasa. We’ve covered a few of Rasa’s features in the demo, but I wanted to share a few others I find helpful!
Custom Actions: When an intent is encountered, a custom action can be created to run specified code, such as make an API call or query a database.
Forms: Forms are especially useful when the Chatbot is required to obtain a set of information from the user and can execute a custom action when they do so.
Rasa X: Rasa X is a conversation-driven development tool that can be implemented into your existing Rasa Chatbot. It will gather data and use those insights to improve itself.
In this blog, we discussed the basics of Chatbots and how they can be utilized, as well as how to get started with the Rasa framework.
As artificial intelligence (AI) software becomes more popular, it will be important for developers to begin exploring these topics. Something as simple as a Chatbot can be a good way to dip your toes before getting into more advanced AI concepts! With thorough documentation and an active community, Rasa provides a good opportunity to do so.
If you have any feedback or questions, please let me know in the comments below. Thanks for reading!