AI Chatbot with Python

An AI Chatbot is one of the most achieved goals in the field of Artificial Intelligence. Today Chatbot can help you in every way from assisting you to order something online to guide you through a Business Plan. Every Brand is having an AI Chatbot for their purposes. In this article, I will show you how you can build your own AI Chatbot.

What is a Chatbot?

A chatbot is an Artificial Intelligence program which is built on the purpose of interacting with a user based on their respective queries. It uses the process of Natural Language for interaction with a user. A chatbot is based on doing the analysis of the question of the user and then returning the response of the user. This is the idea behind a Chatbot.

Also, read – 10 Machine Learning Projects to Boost your Portfolio.

Building an AI Chatbot

AI ChatBot
Source – zipwip

Now I will show you how you can build your own AI Chatbot using python. Now I will import the necessary libraries we need for this purpose:

#Importing modules
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainerCode language: Python (python)

This chatbot will be based on some bank queries, to move further let’s make instances:

BankBot = ChatBot(name = 'BankBot',
                  read_only = False,                  
                  logic_adapters = ["chatterbot.logic.BestMatch"],                 
                  storage_adapter = "chatterbot.storage.SQLStorageAdapter")Code language: Python (python)

Training an AI Chatbot using Data

Training your chatbot using data is quite simple. To do that you need to instantiate a ChatterBotCorpusTrainer object. It will take the name of your objective as a parameter.

corpus_trainer = ChatterBotCorpusTrainer(BankBot)
corpus_trainer.train("chatterbot.corpus.english")Code language: Python (python)

You can also train a chatterbot on your custom instances. You can see how to do this below:

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)Code language: Python (python)

Run The ChatBot

Once you have trained a chatterbot using your instances, you can run this by building a simple front-end.

while (True):
    user_input = input()
    if (user_input == 'quit'):
        break
    response = BankBot.get_response(user_input)
    print (response)Code language: Python (python)

This was just a simple AI Chatbot; it was only to provide you knowledge on how to use instances and make your list of questions on which you can quickly run it. You can make a better one by adding more instances to your list. The process will remain the same to keep adding cases to make your chatbot even smarter.

Sometimes the chatbots like this can give mistakes while running because of the lack of proper training data. An excellent approach to make a better AI Chatbot is by using the training data provided by Google, Amazon and Microsoft. You can use any training data, make sure you follow the same process as above.

Also, read – Grid Search for Model Tuning.

I hope you liked this article on AI ChatBot, Feel free to ask your valuable questions in the comments section below.

Follow Us:

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1609

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading