Skip to content

Ask AI

Powered by Blaze

Answers come from E2E Cloud documentation. AI can make mistakes.

Call a model with the OpenAI SDK

View as Markdown

Token Factory is OpenAI-compatible, so code written for the OpenAI SDK works with three changes: the base URL, the API key, and the model ID. Every model shares the base URL https://blaze.e2enetworks.net/v1, and the model ID in each request decides which model answers. For tools, JSON output, images, and reasoning with the same client, see Use tools, JSON output, vision, and reasoning.

You need a Token Factory API key from your project; to create one, see Create an API key. The examples call Gemma 4 31B, whose model ID is google/gemma-4-31b-it. To call another model, copy its ID from Token Factory’s model list, as Choose a model describes.

Store the key in an environment variable on the machine that runs your code, rather than in the code itself, and never in a web page or app that runs on your users’ devices:

export TOKEN_FACTORY_API_KEY="<your-api-key>"

<your-api-key> is the key you copied when you created it.

  1. Install the SDK:

    pip install -U openai
  2. Save this in a file named hello.py. It creates a client that points at Token Factory and sends one request:

    import os
    from openai import OpenAI
    client = OpenAI(
    api_key=os.environ["TOKEN_FACTORY_API_KEY"],
    base_url="https://blaze.e2enetworks.net/v1",
    )
    response = client.chat.completions.create(
    model="google/gemma-4-31b-it",
    messages=[{"role": "user", "content": "Implement hello world in Python"}],
    )
    print(response.choices[0].message.content)
    print(response.usage)
  3. Run it:

    python hello.py

The script prints the reply, then the tokens the request used: prompt tokens, completion tokens, and how many of the prompt tokens came from the prompt cache, a detail some models leave empty. These are the tokens your project is billed for; see Tokens and billing.

  1. Install the SDK:

    npm install openai
  2. Save this in a file named hello.mjs. The .mjs ending lets Node run the import and await lines as written.

    import OpenAI from "openai";
    const client = new OpenAI({
    apiKey: process.env.TOKEN_FACTORY_API_KEY,
    baseURL: "https://blaze.e2enetworks.net/v1",
    });
    const response = await client.chat.completions.create({
    model: "google/gemma-4-31b-it",
    messages: [{ role: "user", content: "Implement hello world in Python" }],
    });
    console.log(response.choices[0].message.content);
  3. Run it:

    node hello.mjs

To show the reply as the model writes it, turn streaming on. Stream long replies: a request that is not streamed fails if the whole reply takes longer than four minutes.

The stream ends with one extra chunk that carries only the token usage and has no choices, so skip chunks without choices. A stream that stops before a chunk with a finish reason arrives has failed partway, so keep the last finish reason you see, and send the request again if there is none:

stream = client.chat.completions.create(
model="google/gemma-4-31b-it",
messages=[{"role": "user", "content": "Count from 1 to 10."}],
stream=True,
)
finish = None
for chunk in stream:
if not chunk.choices:
print("\n", chunk.usage)
continue
print(chunk.choices[0].delta.content or "", end="", flush=True)
finish = chunk.choices[0].finish_reason or finish
if finish is None:
print("The stream stopped partway. Send the request again.")

In Node, the same loop reads the stream with for await:

const stream = await client.chat.completions.create({
model: "google/gemma-4-31b-it",
messages: [{ role: "user", content: "Count from 1 to 10." }],
stream: true,
});
let finish = null;
for await (const chunk of stream) {
if (!chunk.choices?.length) continue;
process.stdout.write(chunk.choices[0].delta?.content ?? "");
finish = chunk.choices[0].finish_reason ?? finish;
}
if (!finish) console.log("The stream stopped partway. Send the request again.");

If a request fails, Troubleshooting TIR Token Factory lists each error and what to do about it.

Was this page helpful?