Skip to content

Python Examples

Use the official openai SDK — just point base_url at 树新在线.

Install

bash
pip install openai

Basic chat

python
from openai import OpenAI

client = OpenAI(
    api_key="sk-your-token",
    base_url="https://api.treenew.online/v1",
)

resp = client.chat.completions.create(
    model="gpt-5.1",  # refer to the Model Square
    messages=[
        {"role": "user", "content": "Introduce yourself in one sentence"}
    ],
)
print(resp.choices[0].message.content)

Streaming output

python
stream = client.chat.completions.create(
    model="gpt-5.1",
    messages=[{"role": "user", "content": "Write a short poem about trees"}],
    stream=True,
)
for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
print()

List available models

python
for m in client.models.list():
    print(m.id)

Notes

  • Put the token in an environment variable rather than hard-coding it: api_key=os.environ["TREENEW_API_KEY"]
  • The OpenAI SDKs for other languages (Node.js, Go, etc.) work the same way — just change baseURL

Home · Console