The official OpenAI SDK works with Ambient as-is. The only changes from a stock OpenAI setup are the base URL and the key.
Prerequisites#
- An Ambient API key from app.ambient.xyz/keys
- The OpenAI SDK installed for your language
Python#
Install:
pip install openaiUsage#
ambient_openai.pypython
from openai import OpenAI
client = OpenAI(
base_url="https://api.ambient.xyz/v1",
api_key="your-ambient-api-key",
)
response = client.chat.completions.create(
model="ambient/large",
messages=[
{"role": "user", "content": "Hello, how are you?"}
],
)
message = response.choices[0].message
if message.reasoning_content:
print("Reasoning:", message.reasoning_content)
print("Response:", message.content)Streaming#
ambient_openai_stream.pypython
from openai import OpenAI
client = OpenAI(
base_url="https://api.ambient.xyz/v1",
api_key="your-ambient-api-key",
)
stream = client.chat.completions.create(
model="ambient/large",
messages=[
{"role": "user", "content": "Write a short poem about coding."}
],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta
if delta.reasoning_content:
print(delta.reasoning_content, end="", flush=True)
if delta.content:
print(delta.content, end="", flush=True)JavaScript / TypeScript#
Install:
npm install openaiUsage#
ambient-openai.jsjavascript
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.ambient.xyz/v1",
apiKey: "your-ambient-api-key",
});
const response = await client.chat.completions.create({
model: "ambient/large",
messages: [
{ role: "user", content: "Hello, how are you?" }
],
});
const message = response.choices[0].message;
if (message.reasoning_content) {
console.log("Reasoning:", message.reasoning_content);
}
console.log("Response:", message.content);Streaming#
ambient-openai-stream.jsjavascript
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.ambient.xyz/v1",
apiKey: "your-ambient-api-key",
});
const stream = await client.chat.completions.create({
model: "ambient/large",
messages: [
{ role: "user", content: "Write a short poem about coding." }
],
stream: true,
});
for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta;
if (delta?.reasoning_content) {
process.stdout.write(delta.reasoning_content);
}
if (delta?.content) {
process.stdout.write(delta.content);
}
}Streaming covers the full behavior: non-streamed fallback responses, multi-line SSE events, and stall detection.
Environment variables#
You can also configure the SDK through environment variables:
export OPENAI_BASE_URL=https://api.ambient.xyz/v1
export OPENAI_API_KEY=your-ambient-api-keyThen instantiate the client without arguments:
# Python
client = OpenAI()// JavaScript
const client = new OpenAI();