OpenAI SDK (JS/TS)
Installation
npm install @xeroml/openai @xeroml/otel @opentelemetry/sdk-nodeSetup
-
Initialize OpenTelemetry at application startup:
import { NodeSDK } from "@opentelemetry/sdk-node";import { XeroMLSpanProcessor } from "@xeroml/otel";const sdk = new NodeSDK({spanProcessors: [new XeroMLSpanProcessor()],});sdk.start(); -
Use the XeroML OpenAI client:
import OpenAI from "@xeroml/openai";const openai = new OpenAI();const response = await openai.chat.completions.create({model: "gpt-4o",messages: [{ role: "system", content: "You are a helpful assistant." },{ role: "user", content: "Explain XeroML in one sentence." },],});
Adding Context
import { propagateAttributes } from "@xeroml/tracing";import OpenAI from "@xeroml/openai";
const openai = new OpenAI();
async function handleChat(message: string, userId: string, sessionId: string) { return await propagateAttributes({ userId, sessionId }, async () => { return await openai.chat.completions.create({ model: "gpt-4o", messages: [{ role: "user", content: message }], }); });}Streaming
const stream = await openai.chat.completions.create({ model: "gpt-4o", messages: [{ role: "user", content: "Tell me a story" }], stream: true,});
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? "");}