Build Your First AI Chatbot
Hour 1: Pain Demo
Show a rule-based chatbot - the old way:
# rule_based_bot.py - The OLD way
user_input = input("You: ").lower()
if "hello" in user_input:
print("Bot: Hi there!")
elif "how are you" in user_input:
print("Bot: I'm doing well!")
elif "weather" in user_input:
print("Bot: I don't know the weather.")
else:
print("Bot: I don't understand.")
Now try:
- "hey there!" - ❌ Doesn't work
- "what's up?" - ❌ Doesn't work
- "howdy!" - ❌ Doesn't work
Ask the student: "How many if statements would you need to handle every possible way someone might say hello? 100? 1000? It's infinite!"
This is why we need AI. Let's connect to a real LLM.
Hours 2-3: Guided Build
A real AI chatbot powered by OpenAI's GPT models.
Concepts Learned Through Building:
- OpenAI API setup - getting your API key
- API keys and authentication - keeping them secret
- Chat completions - how to talk to the AI
- Message format - roles (user/assistant)
Step 1: Get Your API Key
- Go to platform.openai.com
- Sign up or log in
- Go to API Keys section
- Create a new secret key
- Save it somewhere safe! You won't see it again.
Step 2: Install the OpenAI Package
pip install openai
Step 3: Set Up Your API Key
Create a file called .env:
OPENAI_API_KEY=sk-your-key-here
Or set it in your terminal:
# Windows
set OPENAI_API_KEY=sk-your-key-here
# Mac/Linux
export OPENAI_API_KEY=sk-your-key-here
Step 4: Your First API Call
# first_ai.py
from openai import OpenAI
# Create the client (it reads the API key automatically)
client = OpenAI()
# Make a request to the AI
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Hello!"}
]
)
# Print the AI's response
print(response.choices[0].message.content)
Run it! You just talked to AI!
Step 5: Understanding Messages
The AI uses a conversation format with roles:
messages = [
{"role": "user", "content": "What is Python?"}, # You
{"role": "assistant", "content": "Python is..."}, # AI's response
{"role": "user", "content": "How do I install it?"} # You again
]
Step 6: Build a Chat Loop
# chatbot.py
from openai import OpenAI
client = OpenAI()
messages = [] # Store conversation history
print("Chat with AI! Type 'quit' to exit.\n")
while True:
# Get user input
user_input = input("You: ")
if user_input.lower() == 'quit':
print("Goodbye!")
break
# Add user message to history
messages.append({"role": "user", "content": user_input})
# Get AI response
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
# Extract the response text
ai_message = response.choices[0].message.content
# Add AI response to history
messages.append({"role": "assistant", "content": ai_message})
# Print it
print(f"AI: {ai_message}\n")
Try it! Now say "hey there", "what's up", "howdy" - it understands everything!
Hours 4-5: Independent Build
- Add conversation history display:
Print "Conversation so far:" showing all messages
- Add a command to clear history:
When user types "clear", reset the messages list
- Count messages:
Show how many messages have been exchanged
- Experiment with different questions:
- Ask it to explain something
- Ask it to write code
- Ask follow-up questions (it remembers!)
Hours 6-7: Challenge Project
🏆 Challenge: Chatbot That Remembers Your Name
Build a chatbot that asks for your name at the start and uses it throughout the conversation.
Requirements:
- Ask for the user's name when starting
- The AI should use the name naturally in responses
- If you tell it facts about yourself, it should remember them
Hint: You can "prime" the conversation:
name = input("What's your name? ")
messages = [
{"role": "user", "content": f"My name is {name}. Please use my name occasionally when talking to me."},
{"role": "assistant", "content": f"Nice to meet you, {name}! I'll remember that. What would you like to talk about?"}
]
# Now continue with the chat loop...
Hour 8: Review & Reflect
✅ Concepts Mastered Today
- OpenAI API - connecting to GPT models
- API key management - keeping secrets safe
- Chat completions - the main way to interact with LLMs
- Message format - user/assistant roles
- Conversation history - maintaining context
📝 Builds Completed
- First API call - "Hello World" to AI
- Interactive chatbot with history
- Named chatbot (challenge)
You just built your first AI application! From now on, every project will be AI-powered. The rule-based era is over.