SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
978 B · 23 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 0.9s)2// Chat Completions streaming with the Node SDK.3import OpenAI from "openai";4import fs from "node:fs";56const client = new OpenAI();7const stream = await client.chat.completions.create({8  model: "gpt-4.1-nano",9  messages: [{ role: "user", content: "Reply with OK." }],10  max_completion_tokens: 32,11  stream: true,12  stream_options: { include_usage: true },13});14let text = "";15let usage: unknown = null;16for await (const chunk of stream) {17  text += chunk.choices[0]?.delta?.content ?? "";18  if (chunk.usage) usage = chunk.usage;19}20fs.appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "openai", method: "POST", path: "/v1/chat/completions", status: 200, est_cost_usd: 0.000002, note: "example chat/streaming.ts" }) + "\n");21console.log(JSON.stringify(text), usage);22if (!text.includes("OK") || !usage) throw new Error("unexpected");23