OpenAI SDK
Configure the URL and key in Python applications.
Install and configure
Install the openai package and set XHUB_BASE_URL and XHUB_API_KEY. The root URL excludes /v1; the SDK appends its endpoint path.
Send a chat request
Replace the public model name in the example. It uses a non-streaming request. For streaming, set stream=True and iterate over events.
Handle errors
Handle authentication, permission, rate limit and connection errors. Set timeouts and bounded retries. Retries may add calls and charges; apply application idempotency rules.
Request example
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["XHUB_API_KEY"],
base_url=os.environ["XHUB_BASE_URL"].rstrip("/"),
)
response = client.chat.completions.create(
model="YOUR_MODEL_NAME",
messages=[{"role": "user", "content": "Hello, introduce yourself."}],
)
print(response.choices[0].message.content)