import { CHAT_MODELS, post, rawSSE, save, short } from "./lib.ts"; const results: Record = {}; for (const model of CHAT_MODELS) { const t0 = Date.now(); const r = await post("/chat/completions", { model, messages: [{ role: "user", content: "What is 2+2? Answer in one short sentence." }], max_tokens: 200, }); const ms = Date.now() - t0; const body: any = r.body; const msg = body?.choices?.[0]?.message; console.log( `\n=== ${model} non-stream ${r.status} ${ms}ms model=${body?.model} finish=${body?.choices?.[0]?.finish_reason}`, ); console.log("headers", r.headers); console.log("message keys", msg ? Object.keys(msg) : body); console.log("content type", Array.isArray(msg?.content) ? "array:" + msg.content.map((c: any) => c.type).join(",") : typeof msg?.content); console.log("content", short(msg?.content, 600)); console.log("usage", JSON.stringify(body?.usage)); console.log("top-level keys", body ? Object.keys(body) : ""); // streaming const t1 = Date.now(); const s = await rawSSE("/chat/completions", { model, messages: [{ role: "user", content: "Say hello in French, 5 words max." }], max_tokens: 200, stream: true, }); const ms2 = Date.now() - t1; const deltaKeys = new Set(); const contentShapes: string[] = []; let usageChunk: any = null; let finish: any = null; let text = ""; for (const e of s.events) { if (e.data === "[DONE]") continue; const ch = e.data?.choices?.[0]; if (ch?.delta) for (const k of Object.keys(ch.delta)) deltaKeys.add(k); if (ch?.delta?.content !== undefined) { const c = ch.delta.content; const shape = typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c; if (contentShapes[contentShapes.length - 1] !== shape) contentShapes.push(shape); if (typeof c === "string") text += c; } if (ch?.finish_reason) finish = ch.finish_reason; if (e.data?.usage) usageChunk = { usage: e.data.usage, choicesLen: e.data.choices?.length, keys: Object.keys(e.data) }; } console.log(`--- stream ${s.status} ${ms2}ms events=${s.events.length} headers=${JSON.stringify(s.headers)}`); console.log("first event", short(s.events[0]?.data, 500)); console.log("second event", short(s.events[1]?.data, 500)); console.log("last-2 events", short(s.events[s.events.length - 2]?.data, 500), "|", short(s.events[s.events.length - 1]?.data, 100)); console.log("delta keys", [...deltaKeys], "content shapes", contentShapes, "finish", finish); console.log("usage chunk", JSON.stringify(usageChunk)); console.log("text", short(text, 200)); results[model] = { nonStream: r, stream: s }; } save("01-chat-stream.json", results);