SDK clients
The inference API is compatible with the OpenAI API. You can also send Anthropic Messages requests. Thus you can use the official SDKs of both. Nuemetrix has no SDK of its own. This page also shows Rust with an HTTP client.
Before you start#
- Make an API key with the scope
inference. See API keys. - Set two environment variables in your shell.
export EMBER_API_URL="<the API URL of your region>"
export EMBER_API_KEY="<your key>"
OpenAI SDK for Python#
Install the SDK with pip install openai. Then change only the base URL and the key.
import os
from openai import OpenAI
client = OpenAI(
base_url=os.environ["EMBER_API_URL"] + "/v1",
api_key=os.environ["EMBER_API_KEY"],
)
reply = client.chat.completions.create(
model="<model ID>",
messages=[{"role": "user", "content": "Hello"}],
)
print(reply.choices[0].message.content)
OpenAI SDK for TypeScript#
Install the SDK with npm install openai.
import OpenAI from "openai";
const client = new OpenAI({
baseURL: `${process.env["EMBER_API_URL"]}/v1`,
apiKey: process.env["EMBER_API_KEY"],
});
const reply = await client.chat.completions.create({
model: "<model ID>",
messages: [{ role: "user", content: "Hello" }],
});
console.log(reply.choices[0]?.message.content);
Rust#
There is no official OpenAI SDK for Rust. Send the JSON with reqwest and read it with serde_json.
// Cargo.toml:
// reqwest = { version = "0.13", features = ["json"] }
// serde_json = "1"
// tokio = { version = "1", features = ["full"] }
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let url = std::env::var("EMBER_API_URL")?;
let key = std::env::var("EMBER_API_KEY")?;
let client = reqwest::Client::new();
// List the models that your key can use.
let models: serde_json::Value = client
.get(format!("{url}/v1/models"))
.bearer_auth(&key)
.send()
.await?
.error_for_status()?
.json()
.await?;
for model in models["data"].as_array().into_iter().flatten() {
println!("{}", model["id"]);
}
// Send one chat request.
let reply: serde_json::Value = client
.post(format!("{url}/v1/chat/completions"))
.bearer_auth(&key)
.json(&json!({
"model": "<model ID>",
"messages": [{"role": "user", "content": "Hello"}]
}))
.send()
.await?
.error_for_status()?
.json()
.await?;
println!("{}", reply["choices"][0]["message"]["content"].as_str().unwrap_or(""));
Ok(())
}
To get the answer in pieces, see the Rust tab of the streaming sample in Chat completions.
Anthropic SDK#
The Anthropic SDKs work with the Messages endpoint. See Anthropic Messages API for the base URL and the fields that are supported.
Other clients#
Any HTTP client works. Send JSON with the header Authorization: Bearer <key>. The API reference lists each endpoint and parameter.
Code from the playground#
The dashboard playground has a View code button. It shows the current request as curl, Python, TypeScript or Rust. The code reads the key from EMBER_API_KEY, so it never contains a key.