How to Make a Chatbot Project in Python [With Source Code] in 2026
By Kechit Goyal
Updated on Dec 02, 2025 | 11 min read | 44.79K+ views
Share:
Working professionals
Fresh graduates
More
By Kechit Goyal
Updated on Dec 02, 2025 | 11 min read | 44.79K+ views
Share:
Table of Contents
A Chatbot Project in Python lets you build a program that understands user input, responds intelligently, and automates conversations. You can create one using simple rule-based logic, machine-learning models, or NLP tools like NLTK and spaCy.
With Python’s clean syntax, you can handle text processing, intent detection, and response generation in a structured, step-by-step way. You also get full control over how the chatbot behaves, learns, and interacts with users.
In this guide, you'll see a full chatbot project in python with source code along.
Looking to build your career in one of the fastest-growing data science fields? Explore our online Data Science Course and gain the skills that employers are actively looking for all from the convenience of your home!
Popular AI Programs
If you have ever wondered how to make a chatbot in python that actually learns, you are in the right place. The following steps will help you set up the environment required to run the chatbot using python source code provided later in this guide.
Prerequisites
The foundation of any efficient chat bot python application lies in its libraries. By installing NLTK and scikit-learn, you are equipping your system with the tools needed to learn how to make chatbot in python effectively.
Open your terminal or command prompt and run the following commands:
Bash
pip install nltk
pip install scikit-learn
We will also need to download a specific NLTK data package called punkt for tokenization and wordnet for lemmatization. Open a Python shell by typing python in your terminal, and then run this chatbot code:
Python
import nltk
nltk.download('punkt')
nltk.download('wordnet')
This data structure is the 'brain' of our bot. The JSON format below is a standard approach for a simple chatbot in python source code.
This file will contain categories of user inputs (we'll call these "tags" or "intents"), patterns of what users might say for each intent, and a list of possible responses for the bot.
Create a file named intents.json and add the following content:
JSON
{
"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Hey", "Good morning", "Good afternoon"],
"responses": ["Hello!", "Hi there!", "Hey! How can I help you?"]
},
{
"tag": "goodbye",
"patterns": ["Bye", "Goodbye", "See you later", "Take care"],
"responses": ["Goodbye!", "See you later!", "Take care!"]
},
{
"tag": "thanks",
"patterns": ["Thanks", "Thank you", "That's helpful", "Thanks a lot"],
"responses": ["You're welcome!", "Happy to help!", "Any time!"]
},
{
"tag": "about",
"patterns": ["Who are you?", "What are you?", "Tell me about yourself"],
"responses": ["I am a chatbot created with Python and NLTK.", "I'm your friendly neighborhood chatbot!"]
},
{
"tag": "help",
"patterns": ["Help", "Can you help me?", "I need help", "What can you do?"],
"responses": ["I can answer your questions. What do you need help with?", "Sure, I'm here to help. What's up?"]
}
]
}
You can add as many tags, patterns, and responses as you want to make your chatbot more knowledgeable.
Also Read: How to Open a JSON File? A Complete Guide on Creating and Reading JSON
Now we need to write a Python script to read this JSON file and prepare the data for training our model. This involves a few key NLP steps:
Let's start our Python script, which we'll call chatbot.py.
Python
import json
import random
import pickle
import numpy as np
import nltk
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
lemmatizer = WordNetLemmatizer()
# Load the intents file
with open('intents.json') as file:
intents = json.load(file)
words = []
classes = []
documents = []
ignore_letters = ['?', '!', '.', ',']
# Process each intent
for intent in intents['intents']:
for pattern in intent['patterns']:
# Tokenize each word in the sentence
word_list = nltk.word_tokenize(pattern)
words.extend(word_list)
# Add the pair to documents
documents.append((word_list, intent['tag']))
# Add the tag to our classes list if not already there
if intent['tag'] not in classes:
classes.append(intent['tag'])
# Lemmatize words and remove duplicates
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_letters]
words = sorted(list(set(words)))
classes = sorted(list(set(classes)))
# Save the processed words and classes
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))
With our data preprocessed, we can now create the training data and build our machine learning model. We will convert our text data into numerical vectors using TfidfVectorizer and then train a Logistic Regression classifier. This model will learn to predict the correct "tag" or "intent" for a given user input. This is a core part of completing a chatbot project in python with source code.
Python
# Create training data
training = []
output_empty = [0] * len(classes)
# Prepare documents for TF-IDF
corpus = [" ".join(doc[0]) for doc in documents]
tags = [doc[1] for doc in documents]
# Vectorize the corpus
vectorizer = TfidfVectorizer(tokenizer=lambda txt: [lemmatizer.lemmatize(w.lower()) for w in nltk.word_tokenize(txt) if w not in ignore_letters])
X = vectorizer.fit_transform(corpus)
y = np.array(tags)
# Train the Logistic Regression model
model = LogisticRegression(random_state=42)
model.fit(X, y)
# Save the trained model and vectorizer
pickle.dump(model, open('chatbot_model.pkl', 'wb'))
pickle.dump(vectorizer, open('vectorizer.pkl', 'wb'))
print("Training is complete!")
After running this script, you will have four new files: words.pkl, classes.pkl, vectorizer.pkl, and chatbot_model.pkl. These files contain the processed data and the trained AI model, ready to be used.
The final step is to create the functions that will handle the conversation. We need to:
This step brings all our chatbot programming efforts together into a usable application.
Python
# This part of the code is for the interactive chat
def get_response(user_input):
# Load the saved model, vectorizer, words, and classes
model = pickle.load(open('chatbot_model.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
classes = pickle.load(open('classes.pkl', 'rb'))
# Transform user input
user_input_vec = vectorizer.transform([user_input])
# Predict the tag
prediction = model.predict(user_input_vec)[0]
# Get a random response from the predicted tag
for intent in intents['intents']:
if intent['tag'] == prediction:
response = random.choice(intent['responses'])
return response
return "I'm not sure how to respond to that."
# Main chat loop
print("Chatbot is live! Type 'quit' to exit.")
while True:
message = input("You: ")
if message.lower() == 'quit':
break
response = get_response(message)
print(f"Bot: {response}")
You can now run this entire script. The first part will train and save the model, and the second part will launch the interactive chat session in your terminal.
Also Read: Top 50 Python Project Ideas with Source Code in 2025
Below, you will find the full chatbot source code in python. We have combined the training and chat execution into a single flow. You can copy this chatbot in python source code directly into your IDE to see it in action.
JSON
{
"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Hey", "Good morning", "Good afternoon", "What's up"],
"responses": ["Hello!", "Hi there!", "Hey! How can I help you?"]
},
{
"tag": "goodbye",
"patterns": ["Bye", "Goodbye", "See you later", "Take care", "I have to go"],
"responses": ["Goodbye!", "See you later!", "Take care!"]
},
{
"tag": "thanks",
"patterns": ["Thanks", "Thank you", "That's helpful", "Thanks a lot", "Much appreciated"],
"responses": ["You're welcome!", "Happy to help!", "Any time!", "My pleasure!"]
},
{
"tag": "about",
"patterns": ["Who are you?", "What are you?", "Tell me about yourself", "Your name?"],
"responses": ["I am a chatbot created with Python and NLTK.", "I'm your friendly neighborhood chatbot!", "You can call me Bot."]
},
{
"tag": "help",
"patterns": ["Help", "Can you help me?", "I need help", "What can you do?", "Support"],
"responses": ["I can answer your questions based on my training. What do you need help with?", "Sure, I'm here to help. What's up?"]
},
{
"tag": "name",
"patterns": ["What is your name?", "What should I call you?"],
"responses": ["You can call me ChatBot.", "I don't have a name, I am a bot."]
}
]
}
Python
import json
import random
import pickle
import numpy as np
import nltk
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
# -----------------
# 1. DATA PREPARATION AND MODEL TRAINING
# -----------------
def train_model():
lemmatizer = WordNetLemmatizer()
# Load the intents file
with open('intents.json') as file:
intents = json.load(file)
words = []
classes = []
documents = []
ignore_letters = ['?', '!', '.', ',']
# Process each intent
for intent in intents['intents']:
for pattern in intent['patterns']:
word_list = nltk.word_tokenize(pattern)
words.extend(word_list)
documents.append((word_list, intent['tag']))
if intent['tag'] not in classes:
classes.append(intent['tag'])
# Lemmatize words, remove duplicates
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_letters]
words = sorted(list(set(words)))
classes = sorted(list(set(classes)))
# Save the processed words and classes
pickle.dump(words, open('words.pkl', 'wb'))
pickle.dump(classes, open('classes.pkl', 'wb'))
# Prepare documents for TF-IDF
corpus = [" ".join(doc[0]) for doc in documents]
tags = [doc[1] for doc in documents]
# Vectorize the corpus
vectorizer = TfidfVectorizer(tokenizer=lambda txt: [lemmatizer.lemmatize(w.lower()) for w in nltk.word_tokenize(txt) if w not in ignore_letters])
X = vectorizer.fit_transform(corpus)
y = np.array(tags)
# Train the Logistic Regression model
model = LogisticRegression(random_state=42, max_iter=200)
model.fit(X, y)
# Save the trained model and vectorizer
pickle.dump(model, open('chatbot_model.pkl', 'wb'))
pickle.dump(vectorizer, open('vectorizer.pkl', 'wb'))
print("Training is complete! Model and necessary files are saved.")
return intents
# -----------------
# 2. CHATBOT INTERACTION
# -----------------
def run_chatbot(intents):
# Load the saved model, vectorizer, and classes
model = pickle.load(open('chatbot_model.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizer.pkl', 'rb'))
def get_response(user_input):
# Transform user input
user_input_vec = vectorizer.transform([user_input])
# Predict the tag
prediction = model.predict(user_input_vec)[0]
# Get a random response from the predicted tag
for intent in intents['intents']:
if intent['tag'] == prediction:
response = random.choice(intent['responses'])
return response
return "I'm not sure how to respond to that, but I'm still learning."
# Main chat loop
print("\nChatbot is live! Type 'quit' to exit.")
while True:
message = input("You: ")
if message.lower() == 'quit':
break
response = get_response(message)
print(f"Bot: {response}")
# -----------------
# MAIN EXECUTION
# -----------------
if __name__ == "__main__":
trained_intents = train_model()
run_chatbot(trained_intents)Machine Learning Courses to upskill
Explore Machine Learning Courses for Career Progression
You've successfully built a foundational chatbot project in python with source code. Now, let's explore some ways to make it even better.
A command-line interface works, but a graphical chat window is far more engaging. You can use Python's built-in Tkinter library to create a simple GUI.
Example Snippet:
Python
import tkinter as tk
def setup_gui():
# This is a basic structure for a GUI
window = tk.Tk()
window.title("My Chatbot")
chat_history = tk.Text(window, state='disabled')
chat_history.pack(padx=10, pady=10)
message_entry = tk.Entry(window, width=50)
message_entry.pack(padx=10, pady=5)
send_button = tk.Button(window, text="Send")
send_button.pack(pady=5)
window.mainloop()
# You would integrate your chatbot logic with this GUI
# setup_gui()
To make your chatbot remember conversations or user preferences, you can connect it to a database like SQLite. You could store each conversation with a timestamp, allowing you to analyze what users are asking most frequently.
You can turn your chatbot into a web application using a web framework like Flask or Django. This would allow you to create an API that a website's frontend can communicate with, letting users interact with your chatbot directly in their browser.
Also Read: The Ultimate Guide to Python Web Development: Fundamental Concepts Explained
While our model works well, you can achieve even more human-like conversations using state-of-the-art models. The Hugging Face Transformers library provides easy access to powerful pre-trained models like BERT and GPT-2, which can understand context and nuance far better. This is a great next step after mastering the fundamentals of chatbot code.
Also Read: Natural Language Processing: The Only Guide You'll Ever Need!
Now, let's understand what a chatbot is and why Python is the perfect tool for the job. A chatbot is a computer program designed to simulate human conversation through text or voice commands. The goal is to create a program that a user can interact with in a natural, intuitive way.
There are generally two types of chatbots:
In this tutorial, we will build an AI-based chatbot. It will be simple enough for beginners but powerful enough to understand user intent and provide intelligent responses. This approach provides a solid foundation in chatbot programming.
Python has emerged as the leading language for Artificial Intelligence and Natural Language Processing (NLP). If you are wondering how to make a chatbot in python, here is why it is the best choice for developers and students alike:
Even when working with a straightforward script, errors can happen. Here are common issues developers face when running this chatbot project with source code and how to fix them:
Congratulations on completing your chatbot project in python with source code! We hope this chatbot project with source code has given you a solid foundation in NLP. You have successfully built an intelligent application that can understand and respond to human language. You've learned the entire pipeline of a chatbot project, from gathering and preprocessing data with NLTK to training a machine learning model with scikit-learn and building an interactive user interface.
NLTK, or the Natural Language Toolkit, is a powerful library used in this chatbot project in python for essential NLP tasks. It handles tokenization (splitting text into words) and lemmatization (reducing words to their root form), which are crucial steps for preparing text data for the machine learning model.
Yes, but not directly. To deploy the chatbot project in python with source code, you would first need to wrap the logic in a web framework like Flask or Django to create an API. Your website's front-end can then send user messages to this API and display the bot's responses.
To add more knowledge to this simple chatbot code in python, you simply need to edit the intents.json file. You can add new "tags" (categories), and under each tag, include more "patterns" and "responses." After updating the JSON file, remember to re-run the training script.
This is an AI chatbot. Many people asking how to make a ai chatbot in python confuse this with simple rule-based scripts. While it uses a structured JSON file, the core logic relies on a machine learning model to predict the user's intent rather than just matching keywords.
Both are techniques to reduce words to their root form, which is vital when building a chatbot using python. Stemming is a cruder process that chops off the ends of words (e.g., "studies" becomes "studi"), while lemmatization is dictionary-based and reduces words to their actual root (e.g., "studies" becomes "study").
To verify the performance of your chatbot project with source code, you should split your intents.json data into a training set and a testing set. Train the model on one and test it on the other to compare predictions against actual tags and calculate an accuracy score.
When learning how to make chatbot in python, you might also encounter libraries like spaCy, which is known for speed, or Hugging Face Transformers. These provide access to state-of-the-art, pre-trained language models for more advanced NLP tasks.
Yes. A chat bot python script can be easily enhanced with a Text-to-Speech (TTS) library like gTTS or pyttsx3. After the bot generates a text response, you pass it to the TTS library to convert it into an audio file.
The more, the better. However, for a simple chatbot project in python with source code like this, a few dozen intents with 5-10 patterns each can produce decent results. Production-level bots often require thousands of examples.
A "bag-of-words" is a method to represent text data numerically. In our chatbot source code in python, it describes word occurrences while disregarding grammar and order. Our TF-IDF vectorizer is a more advanced version of this concept.
Training takes time. We use pickle to save the trained model so you can simply load the chatbot using python source code and start chatting immediately without having to retrain the algorithm every time you run the application.
This simple chatbot in python source code has limited ability to handle typos. Because it is trained on a specific vocabulary, unknown misspelled words are often ignored. Advanced bots use fuzzy matching or character-level embeddings to solve this.
Logistic Regression is the classification algorithm acting as the "brain" of this chatbot project in python. After converting text into numbers, this algorithm maps those vectors to the correct intent (e.g., greeting or goodbye).
Yes. You can modify the chatbot in python source code to use a neural network built with TensorFlow or PyTorch. This would replace the Logistic Regression section, though the data preprocessing steps would remain largely the same.
To make this chatbot project in python with source code context-aware, you need to implement "state management." This involves storing information from previous interactions to inform future responses, a feature found in frameworks like Rasa.
Yes. In this simple chatbot code in python, once the model predicts an intent, the script randomly selects one response from the available list for that intent to prevent the conversation from feeling repetitive.
A corpus is a structured collection of text data. In this project, the collection of all "patterns" inside intents.json serves as the training corpus that our chat bot python model learns from.
The current chat bot using python source code includes a fallback response. A more advanced approach would be to check prediction probabilities; if confidence is low, the bot explicitly states it is unsure rather than guessing.
The random_state parameter ensures reproducibility. When running your chatbot project in python, setting this to a specific number guarantees that you get the exact same training results every time, which is helpful for debugging.
While this chatbot source code in python is an excellent educational tool, a commercial-grade bot requires a more robust framework, extensive error handling, and a dynamic database to handle high user traffic effectively.
95 articles published
Kechit Goyal is a Technology Leader at Azent Overseas Education with a background in software development and leadership in fast-paced startups. He holds a B.Tech in Computer Science from the Indian I...
Speak with AI & ML expert
By submitting, I accept the T&C and
Privacy Policy
Top Resources