Building an AI based Chatbot in Python
Chatbots have become extremely popular in recent years and their use in the industry has skyrocketed. The chatbot market is projected to grow from $2.6 billion in 2019 to $9.4 billion by 2024. This really doesn’t come as a surprise when you look at the immense benefits chatbots bring to businesses. According to a study by IBM, chatbots can reduce customer services cost by up to 30%.
In the third blog of A Beginners Guide to Chatbots, we’ll be taking you through how to build a simple AI-based chatbot with Chatterbot; a Python library for building chatbots.
Chatterbot is a python-based library that makes it easy to build AI-based chatbots. The library uses machine learning to learn from conversation datasets and generate responses to user inputs. The library allows developers to train their chatbot instance with pre-provided language datasets as well as build their own datasets.
A newly initialized Chatterbot instance starts off with no knowledge of how to communicate. To allow it to properly respond to user inputs, the instance needs to be trained to understand how conversations flow. Since Chatterbot relies on machine learning at its backend, it can very easily be taught conversations by providing it with datasets of conversations.
Chatterbot’s training process works by loading example conversations from provided datasets into its database. The bot uses the information to build a knowledge graph of known input statements and their probable responses. This graph is constantly improved and upgraded as the chatbot is used.
The Chatterbot Corpus is an open-source user-built project that contains conversational datasets on a variety of topics in 22 languages. These datasets are perfect for training a chatbot on the nuances of languages – such as all the different ways a user could greet the bot. This means that developers can jump right to training the chatbot on their custom data without having to spend time teaching common greetings.
Chatterbot has built-in functions to download and use datasets from the Chatterbot Corpus for initial training.
Chatterbot uses Logic Adapters to determine the logic for how a response to a given input statement is selected.
A typical logic adapter designed to return a response to an input statement will use two main steps to do this. The first step involves searching the database for a known statement that matches or closely matches the input statement. Once a match is selected, the second step involves selecting a known response to the selected match. Frequently, there will be a number of existing statements that are responses to the known match. In such situations, the Logic Adapter will select a response randomly. If more than one Logic Adapter is used, the response with the highest cumulative confidence score from all Logic Adapters will be selected.
Chatterbot stores its knowledge graph and user conversation data in a SQLite database. Developers can interface with this database using Chatterbot’s Storage Adapters.
Storage Adapters allow developers to change the default database from SQLite to MongoDB or any other database supported by the SQLAlchemy ORM. Developers can also use these Adapters to add, remove, search and modify user statements and responses in the Knowledge Graph as well as create, modify and query other databases that Chatterbot might use.
In this tutorial, we will be using the Chatterbot Python library to build an AI-based Chatbot.
We will be following the steps below to build our chatbot
- Importing Dependencies
- Instantiating a ChatBot Instance
- Training on Chatbot-Corpus Data
- Training on Custom Data
- Building a frontend
The first thing we’ll need to do is import the modules we’ll be using. The
ChatBot module contains the fundamental Chatbot class that will be used to instantiate our chatbot object. The
ListTrainer module allows us to train our chatbot on a custom list of statements that we will define. The
ChatterBotCorpusTrainer module contains code to download and train our chatbot on datasets part of the ChatterBot Corpus Project.
#Importing modules from chatterbot import ChatBot from chatterbot.trainers import ListTrainer from chatterbot.trainers import ChatterBotCorpusTrainer
Instantiating a ChatBot Instance
A chatbot instance can be created by creating a
ChatBot object. The
ChatBot object needs to have a name of the chatbot and must reference any logic or storage adapters you might want to use.
In the case you don’t want your chatbot to learn from user inputs after it has been trained, you can set the
read_only parameter to
BankBot = ChatBot(name = 'BankBot', read_only = False, logic_adapters = ["chatterbot.logic.BestMatch"], storage_adapter = "chatterbot.storage.SQLStorageAdapter")
Training on Chatterbot-Corpus Data
Training your chatbot agent on data from the Chatterbot-Corpus project is relatively simple. To do that, you need to instantiate a
ChatterBotCorpusTrainer object and call the
train() method. The
ChatterBotCorpusTrainer takes in the name of your ChatBot object as an argument. The
train() method takes in the name of the dataset you want to use for training as an argument.
Detailed information about ChatterBot-Corpus Datasets is available on the project’s Github repository.
corpus_trainer = ChatterBotCorpusTrainer(BankBot) corpus_trainer.train("chatterbot.corpus.english")
Training on Custom List Data
You can also train ChatterBot on custom conversations. This can be done by using the module’s
In this case, you will need to pass in a list of statements where the order of each statement is based on its placement in a given conversation. Each statement in the list as a possible response to it’s predecessor in the list.
The training can be undertaken by instantiating a
ListTrainer object and calling the
train() method. It is important to note that the
train() method must be individually called for each list to be used.
greet_conversation = [ "Hello", "Hi there!", "How are you doing?", "I'm doing great.", "That is good to hear", "Thank you.", "You're welcome." ] open_timings_conversation = [ "What time does the Bank open?", "The Bank opens at 9AM", ] close_timings_conversation = [ "What time does the Bank close?", "The Bank closes at 5PM", ] #Initializing Trainer Object trainer = ListTrainer(BankBot) #Training BankBot trainer.train(greet_conversation) trainer.train(open_timings_conversation) trainer.train(close_timings_conversation)
Building a Frontend
Once the chatbot has been trained, it can be used by calling Chatterbot’s
get_response() method. The method takes a user string as an input and returns a response string.
while (True): user_input = input() if (user_input == 'quit'): break response = BankBot.get_response(user_input) print (response)
This blog was a hands-on to building a simple AI-based chatbot in Python. The functionality of this bot can easily be increased by adding more training examples. You could, for example, add more lists of custom responses related to your application.
As we saw, building an AI-based chatbot is relatively easy compared to building and maintaining a Rule-based Chatbot. Despite this ease, chatbots such as this are very prone to mistakes and usually give robotic responses because of a lack of good training data.
A better way of building robust AI-based Chatbots is to use Conversational AI Tools offered by companies like Google and Amazon. These tools are based on complex machine learning models with AI that has been trained on millions of datasets. This makes them extremely intelligent and, in most cases, are almost indistinguishable from human operators.
In the next blog in the series, we’ll be looking at how to build a simple AI-based Chatbot using Google’s DialogFlow Conversational AI Platform.
Want to upgrade your Python abilities? Checkout Data Science Dojo’s Introduction to Python for Data Science.