Hello AI, Using OpenAI API with Python

Recently, I tried using OpenAI’s Python library for the first time — and I was amazed at how easy it is to create a conversational AI assistant with just a few lines of code.

In this post, I’ll share my experience and a simple Python example that lets you chat directly with GPT right from your terminal.

First, install the official OpenAI Python package:

pip install openai

Then, make sure your API key is available as an environment variable:

export OPENAI_API_KEY="your_api_key_here"

Here’s the full program I wrote:

from openai import OpenAI
import os

# Read API Key
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

print("Welcome to my first GPT assistant! (Type 'exit' to exit)")

messages = [
    {"role": "system", "content": "You are a helpful assistant that answers in concise English."}
]

while True:
    user_input = input("You:")
    if user_input.lower() in ["exit", "quit"]:
        print("bye!")
        break

    messages.append({"role": "user", "content": user_input})

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages
    )

    reply = response.choices[0].message.content
    print(f"GPT:{reply}\n")
    
    messages.append({"role": "assistant", "content": reply})

Here’s how the interaction looks in the terminal:

> python3 chat_demo.py
Welcome to my first GPT assistant! (Type 'exit' to exit)
You:Hello OpenAI
GPT:Hello! How can I assist you today?
You:exit
bye!

It’s exciting to see how easily you can build an intelligent assistant using OpenAI’s API.
Even this simple example unlocks countless possibilities — from chatbots and study helpers to automation scripts that can interact naturally with users.

If you’re curious about AI or just enjoy coding, give this a try!
It’s an excellent way to explore how modern conversational AI works — right from your terminal.

In my example, I’m using the gpt-4o-mini model. It’s a great choice to start with because it’s both powerful and affordable.

Here’s the approximate cost for gpt-4o-mini (8k context):

  • Input: $0.15 per 1M tokens
  • Output: $0.60 per 1M tokens

In other words, just $5 can handle thousands of chat requests, making it an ideal tool for learning and experimentation.

Building this first chatbot gave me a real sense of how easy it is to integrate AI into everyday programming projects. The OpenAI Python API makes the whole process clean, intuitive, and fun.

Stay tuned — in my next post, I’ll share how I plan to enhance this chatbot with new features and a simple web interface!! 😊

Published by dbaliw

Highly experienced Oracle Database Administrator and Exadata Specialist with over 15 years of expertise in managing complex database environments. Skilled in cloud technologies, DevOps practices, and automation. Certified Oracle Cloud Infrastructure Architect and Oracle Certified Master with a strong background in performance tuning, high availability solutions, and database migrations.

Leave a comment