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:

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! 

Step-by-Step Guide: Your Chatbot Project in Python with Source Code 

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 

Step 1: Setting Up the Environment and Installing Libraries 

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') 
 

Step 2: Creating the Knowledge Base (intents.json) 

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 

Step 3: Preprocessing the Data 

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: 

  1. Loading the data from the JSON file. 
  2. Tokenization: Breaking down each sentence into individual words. 
  3. Lemmatization: Reducing words to their base or root form (e.g., "running" becomes "run"). This helps the model treat different forms of the same word as the same thing. 
  4. Creating a vocabulary and a bag-of-words representation for our training data. 

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')) 
 
 

Step 4: Building and Training the Model 

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. 

Step 5: Developing the Chatting Interface 

The final step is to create the functions that will handle the conversation. We need to: 

  1. Take user input from the command line. 
  2. Process that input in the same way we processed our training data. 
  3. Use our trained model to predict the intent of the input. 
  4. Select a random response from the list of responses for that intent. 
  5. Display the response to the user. 

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 

The Complete Chatbot Project in Python with Source Code 

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.

File 1: intents.json 

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."] 
    } 
  ] 
} 
 

File 2: chatbot.py (The Complete Python Script) 

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

360° Career Support

Executive PG Program12 Months
background

Liverpool John Moores University

Master of Science in Machine Learning & AI

Double Credentials

Master's Degree18 Months

Enhancing the Chatbot Project in Python

You've successfully built a foundational chatbot project in python with source code. Now, let's explore some ways to make it even better. 

1. Add a Graphical User Interface (GUI) 

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() 
 

2. Connect to a Database 

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. 

3. Deploy Your Chatbot on a Website 

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 

4. Use More Advanced NLP Models 

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! 

Understanding the Core of a Python Chatbot 

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. 

Types of Chatbots 

There are generally two types of chatbots: 

  1. Rule-Based Chatbots: These are the simplest form. They operate based on a predefined set of rules. If a user's query matches a specific keyword or pattern, the bot responds with a pre-written answer. They are easy to build but not very flexible. 
  2. AI/ML-Based Chatbots: These are more advanced. They use Machine Learning (ML) and Natural Language Processing (NLP) to understand the intent behind a user's query, not just the keywords. They can handle variations in language and learn from conversations to improve over time. 

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. 

Why Use Python for Chatbot Development? 

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:

  • Simplicity and Readability: Python’s syntax is clear and concise, making it the ideal language for beginners starting a chatbot project in python. Unlike more complex languages, you can focus on the logic of conversation rather than struggling with verbose syntax, which is perfect when you are looking for a simple chatbot project in python with source code to get started quickly.
  • Powerful NLP Libraries: The primary reason developers prefer building a chatbot using python is its massive ecosystem. Libraries like NLTK (Natural Language Toolkit), spaCy, and TensorFlow allow you to build a sophisticated chat bot python application without reinventing the wheel.
  • Scalability: What starts as a basic script can grow. You can take a standard chat bot using python source code and scale it into a fully functional AI assistant capable of handling thousands of queries, bridging the gap between a student project and a professional application.

Common Errors & Troubleshooting

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:

  • Indentation Errors: Python is very strict about spacing. If you copy-paste the simple chatbot code in python and see an IndentationError, ensure that all lines inside functions and loops are aligned correctly. Even a single misplaced space can prevent the script from running.
  • Missing Libraries: Before executing the chatbot source code in python, double-check that you have installed nltk and scikit-learn. If you get a ModuleNotFoundError, simply run pip install nltk scikit-learn in your terminal again.
  • JSON File Path: The bot relies on intents.json to understand user queries. If the chatbot in python source code cannot find this file, it will crash. Ensure that the JSON file is in the exact same folder as your Python script.
  • "Punkt" or "Wordnet" Not Found: This is a classic error when learning how to make a ai chatbot in python. The NLTK library requires specific data packages to be downloaded separately. Make sure you included the lines nltk.download('punkt') and nltk.download('wordnet') at the start of your script.
  • Pickle Version Conflicts: If you trained your model on one computer and tried to run the chatbot source code in python on another, you might face issues with the pickle files. It is best to re-run the training function train_model() whenever you switch environments to ensure compatibility.

Conclusion 

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. 

Frequently Asked Questions (FAQs)

1. What is NLTK and why is it used for chatbots?

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.

2. Can I use this chatbot on my website?

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.

3. How do I add more knowledge to my chatbot?

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.

4. Is this project considered a rule-based or an AI chatbot?

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.

5. What's the difference between stemming and lemmatization?

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").

6. How can I measure my chatbot's accuracy?

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.

7. What are some alternatives to NLTK for chatbot development?

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.

8. Can I make my chatbot speak its responses?

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.

9. How much data do I need to train a good chatbot?

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.

10. What's a "bag-of-words"?

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.

11. Why do we save the model to a file using pickle?

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.

12. Can this chatbot handle typos from users?

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.

13. What is Logistic Regression in the context of this chatbot project in python with source code?

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).

14. Could I use a neural network instead of Logistic Regression?

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.

15. How can I make the chatbot remember conversation context?

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.

16. Is the chatbot's response always random from the list?

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.

17. What is a "corpus" in NLP?

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.

18. How can I handle user inputs that don't match any intent?

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.

19. What's the purpose of the random_state parameter in the model?

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.

20. Is this chatbot project in python with source code scalable for a business?

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.

Kechit Goyal

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

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive Program in Generative AI for Leaders

76%

seats filled

View Program

Top Resources

Recommended Programs

LJMU

Liverpool John Moores University

Master of Science in Machine Learning & AI

Double Credentials

Master's Degree

18 Months

IIITB
bestseller

IIIT Bangalore

Executive Diploma in Machine Learning and AI

360° Career Support

Executive PG Program

12 Months

upGrad
new course

upGrad

Advanced Certificate Program in GenerativeAI

Generative AI curriculum

Certification

5 months