You want to build an AI app but don't know where to start? This tutorial takes you from zero to a working web-based AI chatbot in 30 minutes.
What We're Building
┌─────────────────────────────────────┐
│ 🤖 AI ChatBot │
├─────────────────────────────────────┤
│ │
│ You: What's the weather like? │
│ │
│ Bot: I don't have access to │
│ real-time weather data, but │
│ I can help with other things! │
│ │
│ You: Tell me a joke │
│ │
│ Bot: Why do programmers prefer │
│ dark mode? Because light │
│ attracts bugs! 🐛 │
│ │
│ [Type a message... ] [Send] │
└─────────────────────────────────────┘
Prerequisites
- Python installed (3.8+)
- An OpenAI account
- 30 minutes of time
- No prior AI experience needed!
Step 1: Set Up Your Project (5 minutes)
Create project folder:
# Open terminal and run:
mkdir ai-chatbot
cd ai-chatbotCreate virtual environment:
# Mac/Linux
python3 -m venv venv
source venv/bin/activate
# Windows
python -m venv venv
venv\Scripts\activateInstall dependencies:
pip install flask openai python-dotenvCreate project structure:
ai-chatbot/
├── app.py # Main application
├── templates/
│ └── index.html # Web interface
├── .env # API key (secret!)
└── requirements.txt # Dependencies
Step 2: Get Your OpenAI API Key (3 minutes)
- Go to https://platform.openai.com
- Sign up or log in
- Click "API keys" in sidebar
- Create new key
- Copy it immediately!
Create .env file:
# .env - NEVER commit this to git!
OPENAI_API_KEY=sk-your-key-paste-hereCreate .gitignore:
# .gitignore
.env
venv/
__pycache__/Step 3: Build the Backend (10 minutes)
Create app.py:
# app.py
from flask import Flask, render_template, request, jsonify
from openai import OpenAI
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Initialize Flask app
app = Flask(__name__)
# Initialize OpenAI client
client = OpenAI()
# Store conversation history (in memory for now)
conversation_history = []
@app.route('/')
def home():
"""Render the chat interface."""
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
"""Handle chat messages."""
# Get message from request
data = request.json
user_message = data.get('message', '').strip()
if not user_message:
return jsonify({'error': 'No message provided'}), 400
# Add user message to history
conversation_history.append({
"role": "user",
"content": user_message
})
try:
# Create messages array with system prompt
messages = [
{
"role": "system",
"content": "You are a friendly and helpful AI assistant. Be concise but informative."
}
] + conversation_history
# Call OpenAI API
response = client.chat.completions.create(
model="gpt-4o-mini", # Cheap and fast!
messages=messages,
max_tokens=500
)
# Extract response
bot_message = response.choices[0].message.content
# Add to history
conversation_history.append({
"role": "assistant",
"content": bot_message
})
# Keep history manageable (last 20 messages)
if len(conversation_history) > 20:
conversation_history.pop(0)
conversation_history.pop(0)
return jsonify({'response': bot_message})
except Exception as e:
print(f"Error: {e}")
return jsonify({'error': 'Something went wrong. Please try again.'}), 500
@app.route('/clear', methods=['POST'])
def clear():
"""Clear conversation history."""
global conversation_history
conversation_history = []
return jsonify({'status': 'cleared'})
if __name__ == '__main__':
app.run(debug=True, port=5000)Step 4: Build the Frontend (10 minutes)
Create templates/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI ChatBot</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.chat-container {
width: 100%;
max-width: 600px;
background: white;
border-radius: 20px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
overflow: hidden;
}
.chat-header {
background: #667eea;
color: white;
padding: 20px;
text-align: center;
}
.chat-header h1 {
font-size: 1.5rem;
margin-bottom: 5px;
}
.chat-messages {
height: 400px;
overflow-y: auto;
padding: 20px;
background: #f5f5f5;
}
.message {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.message.user {
align-items: flex-end;
}
.message.bot {
align-items: flex-start;
}
.message-content {
max-width: 80%;
padding: 12px 18px;
border-radius: 18px;
line-height: 1.5;
}
.message.user .message-content {
background: #667eea;
color: white;
border-bottom-right-radius: 4px;
}
.message.bot .message-content {
background: white;
color: #333;
border-bottom-left-radius: 4px;
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}
.message-label {
font-size: 0.75rem;
color: #888;
margin-bottom: 4px;
}
.chat-input {
display: flex;
padding: 20px;
background: white;
border-top: 1px solid #eee;
}
.chat-input input {
flex: 1;
padding: 12px 20px;
border: 2px solid #eee;
border-radius: 25px;
font-size: 1rem;
outline: none;
transition: border-color 0.3s;
}
.chat-input input:focus {
border-color: #667eea;
}
.chat-input button {
margin-left: 10px;
padding: 12px 25px;
background: #667eea;
color: white;
border: none;
border-radius: 25px;
font-size: 1rem;
cursor: pointer;
transition: background 0.3s;
}
.chat-input button:hover {
background: #5a6fd6;
}
.chat-input button:disabled {
background: #ccc;
cursor: not-allowed;
}
.typing-indicator {
display: none;
padding: 12px 18px;
background: white;
border-radius: 18px;
border-bottom-left-radius: 4px;
color: #888;
font-style: italic;
}
.typing-indicator.show {
display: inline-block;
}
.clear-btn {
background: #ff6b6b !important;
}
.clear-btn:hover {
background: #ee5a5a !important;
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
<h1>🤖 AI ChatBot</h1>
<p>Powered by OpenAI</p>
</div>
<div class="chat-messages" id="chatMessages">
<div class="message bot">
<span class="message-label">Bot</span>
<div class="message-content">
Hello! I'm your AI assistant. How can I help you today?
</div>
</div>
</div>
<div class="chat-input">
<input type="text" id="messageInput" placeholder="Type a message..." autocomplete="off">
<button onclick="sendMessage()" id="sendBtn">Send</button>
<button onclick="clearChat()" class="clear-btn">Clear</button>
</div>
</div>
<script>
const messagesContainer = document.getElementById('chatMessages');
const messageInput = document.getElementById('messageInput');
const sendBtn = document.getElementById('sendBtn');
// Send on Enter key
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendMessage();
}
});
function addMessage(content, isUser) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
messageDiv.innerHTML = `
<span class="message-label">${isUser ? 'You' : 'Bot'}</span>
<div class="message-content">${content}</div>
`;
messagesContainer.appendChild(messageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function showTyping() {
const typingDiv = document.createElement('div');
typingDiv.className = 'message bot';
typingDiv.id = 'typingIndicator';
typingDiv.innerHTML = `
<span class="message-label">Bot</span>
<div class="typing-indicator show">Thinking...</div>
`;
messagesContainer.appendChild(typingDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function hideTyping() {
const typing = document.getElementById('typingIndicator');
if (typing) typing.remove();
}
async function sendMessage() {
const message = messageInput.value.trim();
if (!message) return;
// Add user message
addMessage(message, true);
messageInput.value = '';
// Disable input while processing
sendBtn.disabled = true;
messageInput.disabled = true;
// Show typing indicator
showTyping();
try {
const response = await fetch('/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message })
});
const data = await response.json();
hideTyping();
if (data.error) {
addMessage('Sorry, something went wrong. Please try again.', false);
} else {
addMessage(data.response, false);
}
} catch (error) {
hideTyping();
addMessage('Connection error. Please check your internet.', false);
}
// Re-enable input
sendBtn.disabled = false;
messageInput.disabled = false;
messageInput.focus();
}
async function clearChat() {
await fetch('/clear', { method: 'POST' });
messagesContainer.innerHTML = `
<div class="message bot">
<span class="message-label">Bot</span>
<div class="message-content">
Chat cleared! How can I help you?
</div>
</div>
`;
}
</script>
</body>
</html>Step 5: Run Your App! (2 minutes)
# Make sure you're in the project folder with venv activated
python app.pyOpen your browser: Go to http://localhost:5000
You should see your chatbot! Try typing:
- "Hello!"
- "Tell me a joke"
- "What can you help me with?"
Congratulations! 🎉
You just built your first AI application!
What You Learned
✅ Setting up Python virtual environment
✅ Using OpenAI API
✅ Building Flask web server
✅ Creating responsive chat interface
✅ Handling API requests/responses
✅ Managing conversation history
What's Next?
Improvements you can make:
- Add database - Store conversations permanently
- User accounts - Let users save their chats
- Streaming - Show responses word-by-word
- Deploy online - Put it on the internet
Quick upgrade - Add streaming:
# In app.py, add streaming endpoint
@app.route('/chat-stream', methods=['POST'])
def chat_stream():
def generate():
# ... setup code ...
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
yield f"data: {chunk.choices[0].delta.content}\n\n"
return Response(generate(), mimetype='text/event-stream')Troubleshooting
"Module not found" error:
pip install flask openai python-dotenv"Invalid API key" error:
- Check your
.envfile has the correct key - No spaces around the
=sign - Restart the Flask server
Blank responses:
- Check your OpenAI account has credits
- Try a simpler prompt first
Complete Code Files
requirements.txt:
flask==3.0.0
openai==1.12.0
python-dotenv==1.0.0
Need Help Going Further?
Building production AI apps requires more than tutorials. Our team can help with:
- Full-stack AI application development
- Deployment and scaling
- Security and compliance
- Custom AI solutions