Every hiring manager and tech recruiter today is looking for one thing: AI literacy. Building your own AI tool is the ultimate resume booster. Today, we are going from absolute zero to a fully functional conversational AI chatbot using Python. (Ghokante)

The Architecture: How It Works
Before we write a single line of code, let's understand the flow. We aren't training a massive AI model from scratch (that takes supercomputers!). Instead, we are using an API (Application Programming Interface). Our Python script will act as the messenger, taking your typed text, securely sending it to OpenAI's servers, and printing the AI's reply.
Step 1: The Setup
You need two things to start:
- Python Installed: Make sure Python is installed on your computer.
- An API Key: Go to the OpenAI Developer Platform, create an account, and generate a secret API key. Treat this key like a password!
Open your terminal or command prompt and install the official OpenAI library and a tool to hide our secret key:
pip install openai python-dotenvNext, create a file named .env in your project folder and paste your key inside it like this:
OPENAI_API_KEY=sk-your_secret_key_here_do_not_shareStep 2: The "Hello World" of AI
Create a file called bot.py. We are going to write a simple script that asks the AI a single question and prints the answer.
import os
from openai import OpenAI
from dotenv import load_dotenv
# Load the secret key from the .env file
load_dotenv()
# Initialize the AI Client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Make the request to the AI
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Explain black holes in one simple sentence."}
]
)
# Print the AI's reply
print(response.choices[0].message.content)
Run your file (python bot.py) and watch the magic happen! You just successfully communicated with an AI model via code.
Step 3: Building the Continuous Chat Loop
A real chatbot doesn't just answer one question and quit. It remembers what you said previously and keeps the conversation going. To do this, we use a while loop and a list to store the "Memory" of the chat.
Replace the code in bot.py with this final, upgraded version:
import os
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# This array is the "Memory" of the chatbot
# The "system" role tells the AI how to behave.
chat_history = [
{"role": "system", "content": "You are a sarcastic, funny programmer bot."}
]
print("🤖 Chatbot initialized! (Type 'quit' to exit)\n")
while True:
# 1. Get user input
user_input = input("You: ")
# Check if user wants to quit
if user_input.lower() in ["quit", "exit"]:
print("Bot: See ya later!")
break
# 2. Add user message to history
chat_history.append({"role": "user", "content": user_input})
# 3. Send the entire history to the AI
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=chat_history
)
# 4. Extract the AI's reply
bot_reply = response.choices[0].message.content
print(f"\nBot: {bot_reply}\n")
# 5. Add AI's reply to history so it remembers for the next loop
chat_history.append({"role": "assistant", "content": bot_reply})
🚀 Level Up Challenge!
You now have a working AI in your terminal. For your next challenge: Try changing the "system" prompt in the code. Change it from a "sarcastic programmer" to a "Shakespearean poet" or a "fitness coach" and see how dramatically the chatbot's personality changes!
Leave a comment
Your email address will not be published. Required fields are marked *
