Python Client Example

import websocket
import json
import time
import sys

URL = "wss://chat-proxy.bitseek.ai/v2/chat?apikey=<API-KEY>"

def on_message(ws, message):
    msg = json.loads(message)
    event = msg["event"]
    if event == "auth-connect-complete":
        print("Auth successful:", msg["data"])
    elif event == "set-model-complete":
        print("Set model complete:", msg["data"])
    elif event == "conv-message":
        print("Conversation message:", msg["data"])
    elif event == "message":
        print(msg["data"]["output"], end="", flush=True)
        if msg["data"]["stop"]: print("\n--- Stream End ---")
    elif event == "pong":
        print("📍 Pong received:", msg["data"])
    elif event == "error":
        print("❌ Error:", msg["data"]["error"])
    else:
        print("📨 Unknown event:", event, msg["data"])

def on_open(ws):
    print("✅ Connected")
    ws.send(json.dumps({"event": "ping"}))
    # sending message
    print("📤 Sending message...")
    ws.send(json.dumps({
        "event": "gen",
        "data": {
            "prompt": "Hello, please explain the concept of artificial intelligence",
            "temperature": 0.2,
            "enableWebSearch": True,
            "webSearch": {
                "enabled": True,
                "mode": "force",
                "maxResults": 5,
                "freshness": "week",
                "lang": "auto"
            },
            "tools": [
                {
                    "type": "function",
                    "function": {
                        "name": "get_weather",
                        "description": "Get weather by city",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "city": {"type": "string"}
                            },
                            "required": ["city"]
                        }
                    }
                }
            ]
        }
    }))

def on_close(ws, close_status_code, close_msg):
    print(f"⚠️ Connection closed: {close_status_code} - {close_msg}")
    reconnect()

def on_error(ws, error):
    print("❌ WebSocket error:", error)
    ws.close()

def reconnect():
    print("🔄 Attempting reconnect in 1s...")
    time.sleep(1)
    run_ws()

def run_ws():
    ws = websocket.WebSocketApp(
        URL,
        on_open=on_open,
        on_message=on_message,
        on_close=on_close,
        on_error=on_error
    )
    ws.run_forever()

run_ws()

Notes

  • Send chat requests with event: "gen" and data.prompt.
  • event: "gen" supports temperature, tools, enableWebSearch, and webSearch.
  • conv-message includes conversationId and messageId.
  • Stream completion is event: "message" with data.stop == True.

results matching ""

    No results matching ""