Recommended SDKs
Tuna supports OpenAI-compatible endpoints and features, and is tested with these two SDKs:
| SDK | npm packages |
|---|---|
| OpenAI JavaScript SDK | openai |
| Vercel AI SDK | ai and @ai-sdk/openai-compatible |
Other OpenAI-compatible SDKs can be used with Tuna when they support a custom base URL and the endpoints your application needs. The Python examples in these docs use the official OpenAI Python SDK.
OpenAI JavaScript SDK
Install it with npm install openai, then point it at Tuna:
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api-us.tuna.ai/v1",
apiKey: "tuna_yourApiKeyHereWithTunaUnderscore",
});
const response = await client.chat.completions.create({
model: "tuna-us/example-lab/example-model-id",
messages: [{
role: "user",
content: "Is the tuna one of the fastest sustained swimming fish species? And does it have a streamlined body?",
}],
});
console.log(response.choices[0].message.content);
Vercel AI SDK
Install it with npm install ai @ai-sdk/openai-compatible, then create a Tuna provider:
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";
const tuna = createOpenAICompatible({
name: "tuna",
baseURL: "https://api-us.tuna.ai/v1",
apiKey: "tuna_yourApiKeyHereWithTunaUnderscore",
includeUsage: true,
});
const { text } = await generateText({
model: tuna("tuna-us/example-lab/example-model-id"),
prompt: "Is the tuna one of the fastest sustained swimming fish species? And does it have a streamlined body?",
});
console.log(text);
includeUsage: true makes streamed replies report their token usage too. See Streaming.
Without an SDK
You don't need an SDK. The API is plain HTTPS with JSON bodies, so any HTTP client works:
const response = await fetch("https://api-us.tuna.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer tuna_yourApiKeyHereWithTunaUnderscore",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "tuna-us/example-lab/example-model-id",
messages: [{
role: "user",
content: "Is the tuna one of the fastest sustained swimming fish species? And does it have a streamlined body?",
}],
}),
});
const data = await response.json();
if (!response.ok) throw new Error(data.error.message);
console.log(data.choices[0].message.content);
SDKs retry failed requests for you. Without one, add your own retries. See Retries.
Compress Requests with gzip
Long conversations, documents, and images make request bodies large. You can compress the body with gzip and send the Content-Encoding: gzip header. JSON usually shrinks to a small part of its size, so your app uploads less and large requests reach Tuna sooner.
import { gzipSync } from "node:zlib";
const response = await fetch("https://api-us.tuna.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer tuna_yourApiKeyHereWithTunaUnderscore",
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
body: gzipSync(JSON.stringify({ model: "tuna-us/example-lab/example-model-id", messages })),
});
gzip -c request.json | curl https://api-us.tuna.ai/v1/chat/completions \
-H "Authorization: Bearer tuna_yourApiKeyHereWithTunaUnderscore" \
-H "Content-Type: application/json" \
-H "Content-Encoding: gzip" \
--data-binary @-
import gzip
import json
import requests
response = requests.post(
"https://api-us.tuna.ai/v1/chat/completions",
headers={
"Authorization": "Bearer tuna_yourApiKeyHereWithTunaUnderscore",
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
data=gzip.compress(json.dumps({
"model": "tuna-us/example-lab/example-model-id",
"messages": messages,
}).encode()),
)
