Introduction
Building an AI chatbot used to require a PhD in machine learning. Not anymore. With the OpenAI API and Python, you can have a working chatbot running on your machine in under two hours — no ML experience needed.
In this tutorial, we'll build a command-line chatbot that:
- Remembers the conversation history
- Responds intelligently using GPT-4
- Handles errors gracefully
- Can be extended into a web app
Let's get started.
Setting Up Your Environment
First, make sure you have Python 3.9 or higher installed. Then create a virtual environment and install the required package:
# Create a virtual environment
python -m venv chatbot-env
# Activate it (Mac/Linux)
source chatbot-env/bin/activate
# Activate it (Windows)
chatbot-env\Scripts\activate
# Install the OpenAI SDK
pip install openai python-dotenv
Next, create a .env file in your project root and add your OpenAI API key:
OPENAI_API_KEY=sk-your-api-key-here
You can get an API key by signing up at platform.openai.com. New accounts get free credits to get started.
Building the Core Chat Logic
Create a file called chatbot.py. This is where all the magic happens:
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def chat(conversation_history: list, user_message: str) -> str:
"""Send a message and get a response from GPT-4."""
conversation_history.append({
"role": "user",
"content": user_message
})
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=conversation_history,
max_tokens=500,
temperature=0.7,
)
assistant_message = response.choices[0].message.content
conversation_history.append({
"role": "assistant",
"content": assistant_message
})
return assistant_message
def main():
print("Chatbot ready! Type 'quit' to exit.\n")
system_prompt = {
"role": "system",
"content": (
"You are a helpful assistant. Be concise and friendly. "
"If you don't know something, say so honestly."
)
}
conversation_history = [system_prompt]
while True:
user_input = input("You: ").strip()
if not user_input:
continue
if user_input.lower() in ("quit", "exit", "bye"):
print("Chatbot: Goodbye!")
break
try:
response = chat(conversation_history, user_input)
print(f"Chatbot: {response}\n")
except Exception as e:
print(f"Error: {e}\n")
if __name__ == "__main__":
main()
Run it with:
python chatbot.py
You'll see Chatbot ready! Type 'quit' to exit. and you can start chatting.
Adding Memory and Personality
The key to a good chatbot is the system prompt and maintaining conversation history. Let's enhance both:
def create_chatbot(persona: str = "default"):
"""Factory function to create chatbots with different personas."""
personas = {
"default": "You are a helpful, friendly assistant.",
"teacher": (
"You are a patient Python tutor. Explain concepts simply, "
"use code examples, and encourage learners."
),
"reviewer": (
"You are an expert code reviewer. Analyze code for bugs, "
"performance issues, and best practices. Be specific."
),
}
system_message = personas.get(persona, personas["default"])
history = [{"role": "system", "content": system_message}]
def send(message: str) -> str:
return chat(history, message)
return send
# Usage
python_tutor = create_chatbot("teacher")
answer = python_tutor("What is a list comprehension?")
print(answer)
This pattern lets you create specialized chatbots for different use cases — a customer support bot, a code reviewer, or even a creative writing assistant.
Handling Errors and Rate Limits
Production-ready chatbots need proper error handling. OpenAI has rate limits, and your users might send unexpected input:
import time
from openai import RateLimitError, APIError
def safe_chat(history: list, message: str, retries: int = 3) -> str:
"""Chat with automatic retry on rate limit errors."""
for attempt in range(retries):
try:
return chat(history, message)
except RateLimitError:
if attempt < retries - 1:
wait = 2 ** attempt # Exponential backoff: 1s, 2s, 4s
print(f"Rate limit hit. Waiting {wait}s...")
time.sleep(wait)
else:
return "Sorry, I'm too busy right now. Try again in a moment."
except APIError as e:
return f"API error: {e}"
except Exception as e:
return f"Unexpected error: {e}"
What's Next
You now have a fully working Python chatbot. Here are some ways to take it further:
- Add a web UI — Use Flask or FastAPI to serve the chatbot as an API, then build a React frontend
- Persist conversations — Save history to SQLite or PostgreSQL so users can resume chats
- Add streaming — Use
stream=Truein the API call for a real-time typing effect - Deploy it — Host on Render, Railway, or a VPS for a live URL
The foundation you've built here is solid enough to power a real product. The OpenAI API handles the hard AI work — your job is to build the experience around it.
Happy coding!