In our previous Python tutorial, we have explained about What is the ChatGPT, it’s benefits and limitations. In this tutorial, we will explain how to develop your own AI ChatBot using Python.
A ChatBot is a automated system that uses artificial intelligence (AI) and natural language processing (NLP) to simulate and process human conversation.
ChatBots are becomming popular due to its importance. Most of companies started using ChatBots to complete their tasks related to customer support, generating information, etc. The ChatBots are worked as a knowledge base, deliver personalized responses, and help customers complete tasks.
It is expected that in coming years chatbots will take over entirely of all customer support related tasks.
So here in this tutorial, we will make a ChatBot using ChatterBot
module.
So let’s code to make chatBot using Python:
Creating ChatBot using ChatterBot Library
The ChatterBot
is a Python library that generate automated responses to users’ input by using machine learning algorithms to create chatbots.
So we will install ChatterBot
module using below command.
pip install chatterbot
After installing ChatterBot
module, now we will make our own ChatBot.
First we will import ChatterBot
module.
from chatterbot import ChatBot
As we will implement the Chatbot with List Trainer, so we will also import the chatterbot.trainers
. The list trainer takes a list of statements that represent a conversation.
from chatterbot.trainers import ListTrainer
We will create our chatbot and give a name. Here I am giving the name “webdamn”.
chatbot = ChatBot('webdamn')
We will create ListTrainer
object using our created chatbot
. Then we will pass conversation data to trainer.train()
function.
trainer = ListTrainer(chatbot) trainer.train([ "Hello", "Hi, there!", "How are you doing?", "I'm doing great.", "That is good to hear.", "how can i help you?", "I need help regarding order.", "Please open your order to get updated details.", "Thank you.", "You're welcome." ])
Then we will check process our chatbot by creating a while loop and taking the user input. We will check for user input “quit” text to exit from the chatbot otherwise get the response using the get_response()
method and print the result.
print("enter 'quit' to stop") while True: textInput = input("You: ") if textInput == 'quit': break print("Bot:", chatbot.get_response(textInput))
Here is complete code of our chatbot:
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer chatbot = ChatBot('webdamn') trainer = ListTrainer(chatbot) trainer.train([ "Hello", "Hi, there!", "How are you doing?", "I'm doing great.", "That is good to hear", "how can i help you?", "I need help regarding order", "Please open your order to get updated details", "Thank you.", "You're welcome." ]) print("enter 'quit' to stop") while True: textInput = input("You: ") if textInput == 'quit': break print("Bot:", chatbot.get_response(textInput))
Output:
Conclusion
This is a basic tutorial to create your own chatbot with ChatterBot library using List Trainer from Python. You can also enhance this and can ChatterBot Corpus (ChatterBotCorpusTrainer
) that contains data to train chatbots to communicate.