Skip to main content

Use the OpenAI SDK

Proxium speaks the OpenAI API, so an app that uses the OpenAI SDK needs no new library. You change three values:

ValueSet it to
Base URLhttps://proxium.tech/v1
API keyA virtual key of your project, from Keys in the console
ModelA model id or a tier name of your project. Route requests to models explains both

The examples read the key from PROXIUM_KEY and the model from PROXIUM_MODEL. The Quickstart sets both.

Connect your app​

client.py
import os
from openai import OpenAI

client = OpenAI(
base_url="https://proxium.tech/v1",
api_key=os.environ["PROXIUM_KEY"],
)

Send a request​

A request is a list of messages. The model reads them and writes one reply.

response = client.chat.completions.create(
model=os.environ["PROXIUM_MODEL"],
messages=[
{"role": "system", "content": "You answer in one sentence."},
{"role": "user", "content": "What is a virtual key?"},
],
)
print(response.choices[0].message.content)

The answer is the vendor's answer, in the OpenAI shape:

FieldHolds
choices[0].message.contentThe reply
modelThe model that answered
usageThe token counts. Proxium prices the request from them

If the first model fails, Proxium sends the request to the next model of its chain. Route requests to models shows the order.

Stream the reply​

Without streaming, your app waits until the model has written the whole reply. With streaming, the reply arrives in small pieces while the model writes it. Use streaming to show the reply as it grows, for example in a chat window.

To stream, set stream to true. Proxium sends the pieces as server-sent events, in the OpenAI format.

stream = client.chat.completions.create(
model=os.environ["PROXIUM_MODEL"],
messages=[{"role": "user", "content": "Write a haiku about queues."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()
note

The last piece can carry only the token counts, with an empty choices list. Proxium asks the vendor for them, to price the request. The examples check choices before they read it.

Handle errors​

When Proxium refuses a request, or every model fails, your app gets an error. The SDK raises it as an exception. The body names the reason in code:

Error body
{
"error": {
"message": "invalid or revoked key",
"type": "invalid_key",
"code": "invalid_key"
}
}

The errors you meet first:

StatuscodeCauseFix
401missing_keyThe request has no Authorization: Bearer headerSet the API key of the client
401invalid_keyThe key is wrong or revokedCreate a new key on Keys
400no_routeYour project has no vendor that can serve the request. The message is no models available for routeAdd a vendor on Providers
429source_cappedYour app reached a ceiling that you setWait the seconds in the retry-after header
503upstream_failedEvery model of the chain failed. The message gives the last reasonCheck the vendor on Requests, in What failed
import openai

try:
response = client.chat.completions.create(
model=os.environ["PROXIUM_MODEL"],
messages=[{"role": "user", "content": "Hello"}],
)
except openai.APIStatusError as e:
print(e.status_code, e.code, e.message)

Errors lists every code.

Next steps​