Skip to content

First Chatbot

In this guide we'll write a simple command-line chatbot with Python and the openai SDK. We'll make it remember history and stream responses.

Setup

bash
pip install openai
export QEVRON_API_KEY="sk-..."   # your own key

1. Create the client

python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["QEVRON_API_KEY"],
    base_url="https://app.qevron.ai/v1",
)

2. A loop that keeps history

For the chatbot to remember context, we accumulate every message in a messages list:

python
messages = [
    {"role": "system", "content": "You are an assistant that answers briefly and clearly."},
]

print("Chat started (type 'q' to quit)\n")
while True:
    user_input = input("You: ")
    if user_input.strip().lower() == "q":
        break

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

    # Stream the response
    print("Bot: ", end="", flush=True)
    full = ""
    stream = client.chat.completions.create(
        model="verinova",
        messages=messages,
        stream=True,
    )
    for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        full += delta
        print(delta, end="", flush=True)
    print("\n")

    # Add the bot's reply to history too
    messages.append({"role": "assistant", "content": full})

How it works

  • The messages list grows each turn, so the bot "remembers" the prior conversation.
  • stream=True prints the response word by word (see Streaming).
  • Adding the bot's reply (full) back into messages as the assistant role is critical for keeping context.

Improvements

  • Limit context: In very long chats, trim old messages (save tokens).
  • Customize the system message: Define the bot's personality and task with the system message.
  • Error handling: On 429/503, wait briefly and retry (see Errors).

Next: teach your bot your own documents with RAG: Embedding + Rerank.

Qevron — AI gateway. Arpanet / OpenAI / Anthropic / Gemini compatible.