Learn how to build your own AI chatbot with Python. Step-by-step tutorial covering rule-based systems, NLP, and AI API integration with working examples.
🕒 9 min read
Hey there! 👋 Remember when we talked about machine learning and deep learning? Well, today we’re putting those concepts into practice by building something truly exciting: your own AI chatbot!
I’ll never forget the first time I built a chatbot. It was incredibly simple - it could only answer basic questions about the weather. But seeing it respond to my messages felt like magic! ✨
Since then, I’ve built chatbots for customer service, personal assistants, and even mental health support. And today, I’m going to show you exactly how to create your own chatbot from scratch.
Before we dive into code, let me share why chatbots are more relevant than ever:
We’ll create three versions of increasing complexity:
By the end, you’ll have a fully functional chatbot you can customize and expand!
Let’s start with the basics - a chatbot that follows simple rules:
import random
import re
class SimpleChatbot:
def __init__(self):
self.responses = {
'hello': ['Hello!', 'Hi there!', 'Hey!'],
'how are you': ['I'm doing great!', 'Pretty good!', 'Awesome!'],
'what can you do': ['I can chat with you!', 'I answer questions!'],
'weather': ['I wish I knew! Check your weather app?'],
'joke': ['Why did the chatbot go to school? To improve its conversation skills!'],
'bye': ['Goodbye!', 'See you later!', 'Bye! 👋']
}
def get_response(self, user_input):
"""Get response based on user input"""
user_input = user_input.lower().strip()
# Check for matches in our responses
for pattern, responses in self.responses.items():
if re.search(pattern, user_input):
return random.choice(responses)
# Default response if no match found
return "I'm still learning! Can you try asking something else?"
# Let's test our chatbot!
def chat_with_bot():
bot = SimpleChatbot()
print("🤖 ChatBot: Hello! Type 'bye' to exit.")
while True:
user_input = input("You: ")
if user_input.lower() == 'bye':
print("🤖 ChatBot: Goodbye! 👋")
break
response = bot.get_response(user_input)
print(f"🤖 ChatBot: {response}")
# Start chatting!
if __name__ == "__main__":
chat_with_bot()
Sample Conversation:
You: hello
🤖 ChatBot: Hi there!
You: how are you
🤖 ChatBot: Pretty good!
You: tell me a joke
🤖 ChatBot: Why did the chatbot go to school? To improve its conversation skills!
You: bye
🤖 ChatBot: Goodbye! 👋
Pretty cool, right? But this is just the beginning…
Version 2: NLP-Powered Chatbot Now let’s make our chatbot smarter using natural language processing:
import re
import random
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import numpy as np
class NLPChatbot:
def __init__(self):
# Training data - questions and their categories
self.training_questions = [
("hello", "greeting"),
("hi there", "greeting"),
("how are you", "greeting"),
("what's your name", "identity"),
("who are you", "identity"),
("what can you do", "capabilities"),
("tell me a joke", "entertainment"),
("bye", "farewell"),
("goodbye", "farewell"),
("see you later", "farewell")
]
self.responses = {
"greeting": ["Hello! 👋", "Hi there!", "Hey! How can I help?"],
"identity": ["I'm your friendly Python chatbot!", "I'm a chatbot created to help you!"],
"capabilities": ["I can chat, answer questions, and tell jokes!", "I'm here to have conversations with you!"],
"entertainment": [
"Why do programmers prefer dark mode? Because light attracts bugs!",
"How many programmers does it take to change a light bulb? None, that's a hardware problem!"
],
"farewell": ["Goodbye! 👋", "See you later!", "Bye! Come chat again soon!"],
"default": ["I'm still learning! Can you rephrase that?", "Interesting! Tell me more."]
}
# Train our classifier
self.vectorizer = CountVectorizer()
self.classifier = MultinomialNB()
self.train_model()
def train_model(self):
"""Train the NLP model"""
questions = [q[0] for q in self.training_questions]
categories = [q[1] for q in self.training_questions]
# Convert text to numerical features
X = self.vectorizer.fit_transform(questions)
# Train classifier
self.classifier.fit(X, categories)
def get_response(self, user_input):
"""Get response using NLP"""
# Convert input to features
features = self.vectorizer.transform([user_input])
# Predict category
category = self.classifier.predict(features)[0]
# Get random response from category
if category in self.responses:
return random.choice(self.responses[category])
else:
return random.choice(self.responses["default"])
# Test our NLP chatbot
def test_nlp_chatbot():
bot = NLPChatbot()
test_inputs = [
"hello",
"what's your name",
"tell me a joke",
"how are you doing",
"goodbye"
]
for input_text in test_inputs:
response = bot.get_response(input_text)
print(f"Input: '{input_text}' -> Response: '{response}'")
# test_nlp_chatbot()
What’s happening here?
We’re using machine learning to categorize user messages
The chatbot learns patterns from our training data
It can handle variations of the same question
Version 3: AI-Powered Chatbot with OpenAI Now for the really exciting part - let’s integrate with powerful AI models!
Step 1: Set Up OpenAI API First, install the package:
pip install openai
Get your API key from OpenAI and set it as an environment variable.
Step 2: Create the AI Chatbot
import openai
import os
from datetime import datetime
class AIChatbot:
def __init__(self, api_key=None):
# Set up OpenAI API
self.api_key = api_key or os.getenv('OPENAI_API_KEY')
if self.api_key:
openai.api_key = self.api_key
else:
print("⚠️ No API key found. Using fallback mode.")
self.conversation_history = []
# System message to define chatbot personality
self.system_message = {
"role": "system",
"content": "You are a helpful, friendly AI assistant. Keep responses concise and engaging."
}
def get_ai_response(self, user_input):
"""Get response from OpenAI API"""
if not self.api_key:
return "I'd love to chat, but I need an API key to use my AI powers! 🔑"
try:
# Add user message to history
self.conversation_history.append({"role": "user", "content": user_input})
# Prepare messages (system + conversation history)
messages = [self.system_message] + self.conversation_history[-6:] # Keep last 6 messages
# Call OpenAI API
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150,
temperature=0.7
)
# Extract AI response
ai_response = response.choices[0].message.content
# Add AI response to history
self.conversation_history.append({"role": "assistant", "content": ai_response})
return ai_response
except Exception as e:
return f"Oops! I encountered an error: {str(e)}"
def get_fallback_response(self, user_input):
"""Fallback response when API is unavailable"""
fallback_responses = [
"That's interesting! Tell me more.",
"I'm learning so much from our conversation!",
"Can you elaborate on that?",
"What do you think about that topic?",
"That's a great point! Let me think about it..."
]
return random.choice(fallback_responses)
# Complete chatbot with all three approaches
class SmartChatbot:
def __init__(self, openai_key=None):
self.simple_bot = SimpleChatbot()
self.nlp_bot = NLPChatbot()
self.ai_bot = AIChatbot(openai_key)
self.use_ai = openai_key is not None
def chat(self):
"""Main chat interface"""
print("🤖 SmartChatbot: Hello! I'm your AI assistant. Type 'quit' to exit.")
print(" I have three brains: Simple, NLP, and AI-powered! 🧠")
while True:
user_input = input("\nYou: ").strip()
if user_input.lower() in ['quit', 'exit', 'bye']:
print("🤖 SmartChatbot: Thanks for chatting! Goodbye! 👋")
break
# Choose response method based on complexity
if len(user_input.split()) < 3:
# Simple queries -> simple bot
response = self.simple_bot.get_response(user_input)
source = "Simple"
elif self.use_ai and len(user_input.split()) > 5:
# Complex queries -> AI bot
response = self.ai_bot.get_ai_response(user_input)
source = "AI"
else:
# Medium complexity -> NLP bot
response = self.nlp_bot.get_response(user_input)
source = "NLP"
print(f"🤖 ChatBot ({source}): {response}")
# Start your chatbot!
if __name__ == "__main__":
# Replace with your actual OpenAI API key or set OPENAI_API_KEY environment variable
API_KEY = "your-openai-api-key-here"
bot = SmartChatbot(API_KEY)
bot.chat()
Making Your Chatbot Even Better Add Memory and Context
def add_memory_feature(self):
"""Remember user preferences and context"""
self.user_memory = {
'name': None,
'interests': [],
'previous_topics': []
}
def remember_user_info(self, user_input):
"""Extract and remember user information"""
# Simple name extraction
name_patterns = [
r"my name is (\w+)",
r"i'm (\w+)",
r"call me (\w+)"
]
for pattern in name_patterns:
match = re.search(pattern, user_input.lower())
if match:
self.user_memory['name'] = match.group(1).title()
return f"Nice to meet you, {self.user_memory['name']}! I'll remember that."
return None
Add File Processing Capability
def process_file(self, file_path):
"""Let your chatbot read and understand files"""
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# Use AI to summarize or answer questions about the file
if self.use_ai:
prompt = f"Please summarize this document: {content[:2000]}..."
return self.ai_bot.get_ai_response(prompt)
else:
return f"I read the file! It contains {len(content.split())} words."
except Exception as e:
return f"Sorry, I couldn't read that file: {str(e)}"
Deploying Your Chatbot Web Interface with Flask
from flask import Flask, render_template, request, jsonify
app = Flask(__name__)
chatbot = SmartChatbot() # Your chatbot instance
@app.route('/')
def home():
return render_template('chat.html')
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.json['message']
response = chatbot.get_response(user_message) # Use appropriate method
return jsonify({'response': response})
if __name__ == '__main__':
app.run(debug=True)
Simple Web Interface (HTML)
<!-- templates/chat.html -->
<!DOCTYPE html>
<html>
<head>
<title>My AI Chatbot</title>
<style>
.chat-container { max-width: 600px; margin: 0 auto; padding: 20px; }
.messages { height: 400px; border: 1px solid #ccc; overflow-y: scroll; padding: 10px; }
.message { margin: 10px 0; padding: 10px; border-radius: 10px; }
.user { background: #007bff; color: white; margin-left: 20%; }
.bot { background: #f1f1f1; margin-right: 20%; }
input { width: 80%; padding: 10px; }
button { width: 18%; padding: 10px; }
</style>
</head>
<body>
<div class="chat-container">
<h1>🤖 My Python Chatbot</h1>
<div class="messages" id="messages"></div>
<div class="input-area">
<input type="text" id="userInput" placeholder="Type your message...">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
function addMessage(text, isUser) {
const messages = document.getElementById('messages');
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
messageDiv.textContent = text;
messages.appendChild(messageDiv);
messages.scrollTop = messages.scrollHeight;
}
async function sendMessage() {
const input = document.getElementById('userInput');
const message = input.value.trim();
if (message) {
addMessage(message, true);
input.value = '';
// Send to backend
const response = await fetch('/chat', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({message: message})
});
const data = await response.json();
addMessage(data.response, false);
}
}
// Enter key support
document.getElementById('userInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') sendMessage();
});
</script>
</body>
</html>
Real-World Applications Customer Service Bot
class CustomerServiceBot(SmartChatbot):
def __init__(self):
super().__init__()
self.faq_responses = {
'shipping': 'We offer free shipping on orders over $50!',
'returns': 'You can return items within 30 days of purchase.',
'hours': 'We\'re open Monday-Friday, 9AM-5PM EST.'
}
def handle_customer_query(self, query):
# First check FAQ, then use AI for complex questions
for topic, response in self.faq_responses.items():
if topic in query.lower():
return response
return self.get_response(query)
What’s Next? Advanced AI Applications! In our next AI post, we’ll explore “Image Recognition for Beginners” where we’ll:
Build an image classification system
Train models to recognize objects
Create a web app that identifies images
Explore computer vision applications
Your Chatbot Mission This week, build and customize:
Start with the simple rule-based chatbot
Add 5 custom responses for your interests
Test it with friends and gather feedback
Share in comments: What’s the most creative response your chatbot gave?
Wrapping Up Today you built not one, but THREE different chatbots! You learned:
✅ Rule-based systems for simple interactions
✅ NLP techniques for smarter conversations
✅ AI API integration for powerful capabilities
✅ Web deployment to share with others
✅ Customization techniques for specific use cases
Remember: Every expert chatbot developer started with a simple “Hello World”. Your chatbot might be basic today, but it’s the foundation for something amazing!
The most important step is to start building and keep iterating. What will you teach your chatbot to do next?
Share your chatbot experiments in the comments! What surprising conversations did you have? What features are you excited to add? Let’s build the future of conversation together!
Comments & Discussion
Join the conversation using your GitHub account. Comments are powered by Utterances.