How to make chatbots in Python – A quick guide

Hey, pythonists! Welcome to another great tutorial in python. In this chatbots in the python tutorial, you will learn how to create chatbots using python. Really this is an amazing topic and getting more popular these days.

Chatbots are simply assistants in the websites to promote products or rectify user queries without a human assistant to the customer. Trust me these are not 100 % accurate and also not that bad but it works well for your business.

Making chatbots using python is really fun and easy to even if you are a beginner in python. Alright, guys, let’s get started.

What you learn

  • How to make chatbots in python using trained conversations
  • How to create a chatbot using python by our own conversations

If you feel difficulty in the titles, don’t worry i will give a clear picture.

What you need

  • Basics of Python
  • Pip

That’s enough guys! really you don’t need anything other than this. of course you will need an Internet-connected PC or laptop.

Installation Guide

First you must install pip package in your system, and now install our python chatbot package called chatterbot.

To install , copy and paste the below command in your command prompt or terminal.

pip install ChatterBot
pip install chatterbot-corpus

Now you ready build your first chatbot in python. so open your favorite code editor and lets start coding.

Read more interesting Articles

Clean your Desktop using python – Python project
Python project – Download songs using python

How to make chatbots using Python

Already I stated in the above lines that we are going to build chatbots in 2 ways. So follow me to build your funny chatbot.

1. How to make chatbots in python using trained conversations

First, we need to import the necessary packages for creating chatbots. here I gave you the entire code, and I will explain in a short while.

from chatterbot import ChatBot 
from chatterbot.trainers import ChatterBotCorpusTrainer 

bot = ChatBot('jp')

trainer = ChatterBotCorpusTrainer(bot)

trainer.train(
    "chatterbot.corpus.english"
)

print('Hi, and welcome! ask me something!\n')

while True:
  inp = input('\nYou:')
  if inp =='done':
    break
  else:
    response = bot.get_response(inp)
    print('\n\n\t\t\tBot:',response)


In the first two lines I imported the chatbot classes for initializing and training of a chatbot.

I used bot as variable to define my chatbot and gave a name as “jp”, you can give your own.

I used ChatterBotCorpusTrainer to train my bot, there is another corpus trainer called UbuntuCorpusTrainer. It’s a large file, it will definitely take a longer time to download and run in your system.

Now I initialized the training to my python chatbot. Here I trained bot with “chatterbot.corpus.english” conversations.

In this corpus, you will get multiple topics like greetings, travel, economy, science, etc. so that we can interact with our bot in multiple ways. here is what it download

You can ask your favorite questions to chatbot using the above topics.

To ask questions endless I used while loop in the above code lines when you gave input as done then it will break the while loop, so that’s the end of our chatbot in python.

This is how i chatted with my bot. lets try with your chatbot.

Here we are giving pre trained corpus to our chatbot to train it, but how it works for me. how to use it for my own.

Ok, that’s a reasonable question, isn’t it? Yes, you can build a chatbot using python for your own services, but I gave you here a basic idea. you can build advanced chatbot using Machine learning and deep learning.

2. How to create chatbot using python by our own conversations

This is the basic way to train our own conversations to our chatbot. so lets get started.

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer


bot = ChatBot('jp')


conversation =[
    'hi',
    'Hi, there',
    'welcome to our channel',
    'who is jp?',
    'jp is the creater of jp NaN and devpyjp.com',
    'bye',
    'thank you','welcome','Please subscribe to our channel and share',
    'subscribe','share','services:youtube channel and www.devpyjp.com',
    'jp','services',
    
]

training = ListTrainer(bot)
training.train(conversation)


print('Hi, and welcome! ask me something!\n')
while True:
  inp = input('\nYou:')
  if inp =='done':
    break
  else:
    response = bot.get_response(inp)
    print('\n\n\t\t\tBot:',response)

ListTrainer helps us to train our own conversations. I created a list called conversation, you can add your own list of sentences.

I trained the bot using our conversations and The rest of the things same as above.

It’s really fun to chat with chatbots I enjoyed a lot by asking some stupid questions. You can learn more bout chatbot using python here: Docs

I hope you love this tutorial, to get more notifications on interesting python projects please subscribe to our newsletter. Thank you.

1 thought on “How to make chatbots in Python – A quick guide”

  1. Hi I am getting below error when i install chatterbot package:
    “Cannot uninstall ‘PyYAML’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall”

    Can you please help in resolving this

Leave a Reply