#!/usr/bin/env python3
# Usage:    uv run chatbot.py
# Requires: openai, python-dotenv
#           OPENAI_API_KEY in environment or .env file

from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI()

history = [
    {"role": "system", "content": "You are a helpful assistant."}
]

print("Chatbot ready. Type 'quit' to exit.\n")

while True:
    user_input = input("You: ").strip()
    if user_input.lower() == "quit":
        break
    if not user_input:
        continue

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

    stream = client.chat.completions.create(
        model="gpt-5.4-mini",
        messages=history,
        stream=True
    )

    print("Assistant: ", end="", flush=True)
    reply = ""
    for chunk in stream:
        delta = chunk.choices[0].delta.content
        if delta:
            print(delta, end="", flush=True)
            reply += delta
    print()

    history.append({"role": "assistant", "content": reply})
