
Perfect for beginners who want to build a real AI tool with just a few lines of code.
✅ What You’ll Get in This Post:
- A complete, working chatbot built in Python
- A quick intro to the OpenAI API (GPT-4 / GPT-3.5)
- A step-by-step tutorial anyone can follow
- Bonus: How to expand your chatbot later
🧠 Who This Is For:
- You’ve learned a bit of Python and want to build something cool
- You’re curious about ChatGPT or OpenAI but don’t know how to use the API
- You want a project that looks impressive but is beginner-friendly
🧰 What You Need Before We Start
- Python 3.7 or higher installed
- An OpenAI account + API key (free trial works!)
- A code editor (like VS Code or even just Notepad)
- About 30 minutes!
🛠️ Step 1: Install the OpenAI Python Library
Open your terminal and run:
pip install openai
This installs the official OpenAI SDK.
🔑 Step 2: Get Your OpenAI API Key
- Go to platform.openai.com/account/api-keys
- Create a new key
- Copy it and keep it somewhere safe
You’ll use this key to talk to the GPT model from your Python script.
💬 Step 3: Write Your First Chatbot Script
Here’s a simple but functional chatbot using gpt-3.5-turbo
:
import openai
openai.api_key = "your-api-key-here" # Replace this with your actual API key
print("🤖 Hello! I’m your Python Chatbot. Type 'exit' to quit.")
chat_history = []
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("👋 Goodbye!")
break
chat_history.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=chat_history
)
reply = response.choices[0].message.content
chat_history.append({"role": "assistant", "content": reply})
print("Bot:", reply)
🔍 What’s Happening Here:
- The chatbot remembers context using a message history
- You send
user
messages and receiveassistant
replies - You can expand this into a GUI or voice bot later!
🧪 Example Chat:
You: What's the capital of France?
Bot: The capital of France is Paris.
You: Who is the president?
Bot: As of 2025, the President of France is Emmanuel Macron.
🛠️ Optional: Add Environment Variables (for safety)
Instead of hardcoding your API key, store it in an .env
file:
.env
:
OPENAI_API_KEY=your-real-api-key
Update your script:
from dotenv import load_dotenv
import os
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
Install dotenv with:
pip install python-dotenv
🔁 How to Expand This Project Later
- Add a GUI using Tkinter or PyQt
- Build a voice assistant using
speech_recognition
andpyttsx3
- Connect it to Telegram or Discord
- Save conversations in a file
- Train it with custom system prompts
🎁 Bonus: Add a Personality
Add this to the beginning of your chat history:
chat_history = [
{"role": "system", "content": "You are a friendly Python chatbot that explains things clearly."}
]
✅ Final Thoughts
In under 30 minutes, you’ve:
- Built a working chatbot
- Used the OpenAI API with Python
- Created your first real AI-powered app
🚀 Now go show it off on GitHub, Reddit, or Twitter!
0 Comments