Install
npm install openai
Setup
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://kymaapi.com/v1",
apiKey: "kyma-your-api-key",
});
Basic chat
const response = await client.chat.completions.create({
model: "llama-3.3-70b",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is machine learning?" },
],
});
console.log(response.choices[0].message.content);
Streaming
const stream = await client.chat.completions.create({
model: "llama-3.3-70b",
messages: [{ role: "user", content: "Write a story" }],
stream: true,
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) process.stdout.write(content);
}
With fetch (no SDK)
const response = await fetch("https://kymaapi.com/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer kyma-your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "llama-3.3-70b",
messages: [{ role: "user", content: "Hello!" }],
}),
});
const data = await response.json();
console.log(data.choices[0].message.content);