Building an Intelligent Chatbot Using Botkit and Rasa NLU
I don’t know if bots are just hype or the real deal, but I can say with certainty that building bots is fun and challenging. Here are some tools to help you build an intelligent chatbot.
Join the DZone community and get the full member experience.
Join For FreeEvery day, we hear about a new bot catering to domains like travel, social, legal, support, sales, etc. being launched. Facebook Messenger alone has more than 11,000 bots when I last checked and probably added thousands more since I wrote this article. The first generation of bots was dumb since they could understand only a limited set of queries based on keywords in the conversation. But the commoditization of NLP (Natural Language Processing) and machine learning by services like Wit.ai, API.ai, Luis.ai, Amazon Lex, IBM Watson, etc. has resulted in the growth of intelligent bots like donotpay and chatShopper.
I don’t know if bots are just hype or the real deal, but I can say with certainty that building a bot is fun and challenging at the same time. In this article, I would like to introduce you to some of the tools to build an intelligent chatbot.
The title of the blog clearly tells that we will use Botkit and Rasa (NLU) to build our bot. Before getting into the technicalities, I would like to share the reason for choosing these two platforms and explain how they fit our use case.
Bot Development Framework
Howdy, Botkit, and Microsoft (MS) Bot Framework were good contenders for this. Both these frameworks:
- Are open source.
- Have integrations with popular messaging platforms like Slack, Facebook Messenger, Twilio, etc.
- Have good documentation.
- Have an active developer community.
Due to compliance issues, we chose AWS to deploy all our services, and we wanted the same with the bot, as well.
NLU (Natural Language Understanding)
API.ai (acquired by Google) and Wit.ai (acquired by Facebook) are two popular NLU tools in the bot industry that we first considered for this task. Both the solutions:
- Are hosted as a cloud service.
- Have Nodejs, Python SDK, and a REST interface.
- Have good documentation.
- Support state or contextual intents, making it very easy to build a conversational platform on top.
As stated before, we couldn’t use any of these hosted solutions due to compliance issues, and that is where we came across an open-source NLU called Rasa, which was a perfect replacement for API.ai and Wit.ai — and we could host and manage it on AWS.
Now, you might be wondering why I used the term NLU for Api.ai and Wit.ai, and not NLP (Natural Language Processing).
NLP refers to all the systems that handle the interactions with humans in the way humans find it natural. It means that we can converse with a system just the way we talk to other human beings. Conversely, NLU is a subfield of NLP that handles a narrow but complex challenge of converting unstructured inputs into a structured form that a machine can understand and act upon. So you say date=20th April 2017, location=San Francisco and action=book hotel and the system can understand.
Rasa NLU
In this section, I would like to explain Rasa in detail and give you some terms used in NLP that you should be familiar with.
Intent: Tells us what the user would like to do.
Example: Raise a complaint, request for refund, etc.
Entities: The attributes that give details about the user’s task.
Example: Complaint regarding service disruptions, refund cost, etc.
Confidence score: A distance metric that indicates how closely the NLU could classify the result into the list of intents.
Here is an example to help you understand the above terms.
Input: "My internet hasn't been working since this morning."
Intent: service_interruption
Entities: “service=internet”, “duration=morning”
Confidence score: 0.84 (may vary based on your training)
NLU’s job (Rasa in our case) is to accept a sentence/statement and give us the intent, entities, and a confidence score that our bot can use. Rasa basically provides a high-level API over various NLP and ML libraries that do intent classification and entity extraction. These NLP and ML libraries are called the backend. They bring the intelligence to Rasa. These are some of the backends used with Rasa:
- MITIE: An all-inclusive library; in other words, it has an NLP library for entity extraction as well as an ML library for intent classification built into it.
- spaCy + sklearn: spaCy is an NLP library that only does entity extraction. sklearn is used with spaCy to add ML capabilities for intent classification.
- MITIE + sklearn: This uses best of both the worlds. This uses good entity recognition available in MITIE along with fast and good intent classification in sklearn.
I have used MITIE backend to train Rasa. For the demo, we have a “Live Support ChatBot” that is trained for messages like:
My phone isn’t working.
My phone isn’t turning on.
My phone crashed and isn’t working anymore.
My training data looks like this:
{
"rasa_nlu_data": {
"common_examples": [
{
"text": "hi",
"intent": "greet",
"entities": []
},
{
"text": "my phone isn't turning on.",
"intent": "device_failure",
"entities": [
{
"start": 3,
"end": 8,
"value": "phone",
"entity": "device"
}
]
},
{
"text": "my phone is not working.",
"intent": "device_failure",
"entities": [
{
"start": 3,
"end": 8,
"value": "phone",
"entity": "device"
}
]
},
{
"text": "My phone crashed and isn’t working anymore.",
"intent": "device_failure",
"entities": [
{
"start": 3,
"end": 8,
"value": "phone",
"entity": "device"
}
]
}
]
}
}
Note: We have observed that MITIE gives better accuracy than spaCy + sklearn for a small training set, but as you keep adding more intents, training on MITIE gets slower and slower. For a training set of 200+ examples with about 10-15 intents, MITIE takes about 35-45 minutes for us to train on a C4.4xlarge instance (16 cores, 30 GB RAM) on AWS.
This is a good tutorial on training Rasa with MITIE backend. If you are a beginner then you can refer to this doc to install Rasa.
Botkit and Rasa Integration
Botkit is an open-source bot development framework designed by the creators of Howdy. It basically provides a set of tools for building bots on Facebook Messenger, Slack, Twilio, Kik and other popular platforms. They have also come up with an IDE for bot development called Botkit Studio. To summarize, Botkit is a tool that allows us to write the bot once and deploy it on multiple messaging platforms.
Botkit also has a support for middleware, which can be used to extend the functionality of Botkit. Integrations with database, CRM, NLU, and statistical tools are provided via middleware, which makes the framework extensible. This design also allows us to easily add integrations with other tools and software by just writing middleware modules for them.
I’ve integrated Slack and botkit for this demo. You can use this boilerplate template to set up Botkit for Slack. We have extended Botkit-Rasa middleware, which you can find here.
Botkit-Rasa has two functions, receive and hear, which override the default Botkit behavior.
receive
is invoked when Botkit receives a message. It sends the user’s message to Rasa and stores the intent and entities into the botkitmessage
object.hears
overrides the default Botkit "hears
" method,controller.hears
. The defaulthears
method uses regex to search the given patterns in the user’s message while the hears method from Botkit-Rasa middleware searches for the intent.
let Botkit = require('botkit');
let rasa = require('./Middleware/rasa')({rasa_uri: 'http://localhost:5000'});
let controller = Botkit.slackbot({
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
scopes: ['bot'],
json_file_store: __dirname + '/.db/'
});
// Override receive method in botkit
controller.middleware.receive.use(rasa.receive);
// Override hears method in botkit
controller.changeEars(function (patterns, message) {
return rasa.hears(patterns, message);
});
controller.setupWebserver(3000, function (err, webserver) {
// Configure a route to receive webhooks from slack
controller.createWebhookEndpoints(webserver);
});
Let’s try an example: “My phone is not turning on.” Rasa will return the following:
Intent: device_failure
Entities: device=phone
If you notice carefully, the input I gave is a not present in my training file. Rasa has some intelligence built into it to identify the intent and entities correctly for such combinations.
We need to add a hears
method listening to the intent device_failure
to process this input. Remember that intent and entities returned by Rasa will be stored in the message
object by Rasa-Botkit middleware.
controller.hears(['device_failure'], 'direct_message,direct_mention,mention',
function (bot, message) {
let reply = 'Try pressing the power button for 30 seconds. ';
reply += 'Bring the phone to the service center if it does not start. ';
reply += 'Don't forget to carry your warranty card.';
bot.reply(message, reply);
});
You should be able run this bot with Slack and see the output as shown below (support_bot is the name of my bot).
You are now familiar with the process of building chatbots with a bot development framework and an NLU. Hopefully, this helps you get started on your bot very quickly.
Published at DZone with permission of Arjun Hariharan. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
13 Impressive Ways To Improve the Developer’s Experience by Using AI
-
How To Scan and Validate Image Uploads in Java
-
Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
-
What Is Envoy Proxy?
Comments