Skip to main content

NodeJS OpenAI API

npm i openai

import OpenAI from "openai";
const openai = new OpenAI({
apiKey: 'YOUR_KEY',
baseURL: 'https://inference.samaira.ai/openai',
});

Chat Completations (Streaming)

import OpenAI from "openai";

const openai = new OpenAI({
apiKey: 'YOUR_KEY',
baseURL: 'https://inference.samaira.ai/openai'
});

async function streamCompletion() {
const stream = await openai.chat.completions.create({
model: "qwq-32b", // Your model ID
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Explain quantum physics simply." },
],
stream: true, // ✅ Enable streaming
});

for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}

console.log("\n\n[Stream complete]");
}

streamCompletion();

Generate (Non Streaming)


async function streamCompletion() {
const stream = await openai.completions.create({
model: "qwq-32b", // Your model ID
prompt='Why is the sky blue?',
stream: true, // ✅ Enable streaming
});

for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.text || "");
}

console.log("\n\n[Stream complete]");
}

streamCompletion();

Tools Call Support

We are still testing Tools calling in supported models, we do support function call but its on beta and not recommeded at the moment.