Python
Use the official OpenAI Python SDK, pointed at Astrodyne.
Install
Shell
pip install openai
A request
Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"],
)
resp = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)
Streaming
Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"],
)
stream = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
Listing your models
Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"],
)
for model in client.models.list():
print(model.id)
Handling errors
Python
import os
import openai
from openai import OpenAI
client = OpenAI(
base_url="https://api.astrodyne.ai/v1",
api_key=os.environ["ASTRODYNE_API_KEY"],
)
try:
resp = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "Hello"}],
)
except openai.AuthenticationError:
... # 401 - key missing, revoked or expired
except openai.PermissionDeniedError:
... # 403 - model not allowed for this account
except openai.NotFoundError:
... # 404 - no such model
except openai.RateLimitError:
... # 429 - rate or spending limit; retry with backoff
except openai.APIStatusError as e:
if e.status_code == 402:
... # insufficient_funds - add funds, do not retry
raise
Reading the request id
Use
with_raw_response to reach the headers and keep
X-Astrodyne-Request-Id alongside your own logs.