Skip to content

Building Chatbots

Build conversational AI chatbots with IndoxHub's chat completions API.

Basic Chatbot

import requests

API_KEY = "YOUR_API_KEY"
BASE_URL = "https://api.indoxhub.com/api/v1"

conversation = [
    {"role": "system", "content": "You are a friendly customer support agent."}
]

def reply(user_input):
    conversation.append({"role": "user", "content": user_input})

    response = requests.post(
        f"{BASE_URL}/chat/completions",
        headers={"Authorization": f"Bearer {API_KEY}"},
        json={
            "model": "openai/gpt-4o-mini",
            "messages": conversation,
            "temperature": 0.7,
            "max_tokens": 500
        }
    )
    answer = response.json()["data"]
    conversation.append({"role": "assistant", "content": answer})
    return answer

# Interactive loop
while True:
    user_input = input("You: ")
    if user_input.lower() in ("quit", "exit"):
        break
    print(f"Bot: {reply(user_input)}")

Streaming Chatbot

For real-time typing effect:

import requests

def stream_reply(messages):
    response = requests.post(
        "https://api.indoxhub.com/api/v1/chat/completions",
        headers={"Authorization": "Bearer YOUR_API_KEY"},
        json={
            "model": "anthropic/claude-haiku-4.5",
            "messages": messages,
            "stream": True
        },
        stream=True
    )
    full = ""
    for line in response.iter_lines():
        if line:
            text = line.decode("utf-8")
            if text.startswith("data: ") and text != "data: [DONE]":
                chunk = text[6:]
                full += chunk
                print(chunk, end="", flush=True)
    print()
    return full

Tips

  • Keep context manageable — Trim old messages when the conversation gets long
  • Use system messages — Define the bot's personality and constraints
  • Choose the right modelopenai/gpt-4o-mini for speed, openai/gpt-4o for quality
  • Stream responses — Better UX for longer answers
  • Handle errors gracefully — Check success field and retry on 5xx errors
Documentation last built on May 23, 2026