SPB Git forge

spb/polyllm

Public
15commits 1branches 0releases
2.2 MBsize
maindefault branch
13 days agolast push
TypeScript 97.4% SQL 1% JavaScript 0.9% CSS 0.6%
2.7 KB · 61 lines typescript
Raw Blame History
1import { CHAT_MODELS, post, rawSSE, save, short } from "./lib.ts";23const results: Record<string, any> = {};4for (const model of CHAT_MODELS) {5  const t0 = Date.now();6  const r = await post("/chat/completions", {7    model,8    messages: [{ role: "user", content: "What is 2+2? Answer in one short sentence." }],9    max_tokens: 200,10  });11  const ms = Date.now() - t0;12  const body: any = r.body;13  const msg = body?.choices?.[0]?.message;14  console.log(15    `\n=== ${model} non-stream ${r.status} ${ms}ms model=${body?.model} finish=${body?.choices?.[0]?.finish_reason}`,16  );17  console.log("headers", r.headers);18  console.log("message keys", msg ? Object.keys(msg) : body);19  console.log("content type", Array.isArray(msg?.content) ? "array:" + msg.content.map((c: any) => c.type).join(",") : typeof msg?.content);20  console.log("content", short(msg?.content, 600));21  console.log("usage", JSON.stringify(body?.usage));22  console.log("top-level keys", body ? Object.keys(body) : "");2324  // streaming25  const t1 = Date.now();26  const s = await rawSSE("/chat/completions", {27    model,28    messages: [{ role: "user", content: "Say hello in French, 5 words max." }],29    max_tokens: 200,30    stream: true,31  });32  const ms2 = Date.now() - t1;33  const deltaKeys = new Set<string>();34  const contentShapes: string[] = [];35  let usageChunk: any = null;36  let finish: any = null;37  let text = "";38  for (const e of s.events) {39    if (e.data === "[DONE]") continue;40    const ch = e.data?.choices?.[0];41    if (ch?.delta) for (const k of Object.keys(ch.delta)) deltaKeys.add(k);42    if (ch?.delta?.content !== undefined) {43      const c = ch.delta.content;44      const shape = typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c;45      if (contentShapes[contentShapes.length - 1] !== shape) contentShapes.push(shape);46      if (typeof c === "string") text += c;47    }48    if (ch?.finish_reason) finish = ch.finish_reason;49    if (e.data?.usage) usageChunk = { usage: e.data.usage, choicesLen: e.data.choices?.length, keys: Object.keys(e.data) };50  }51  console.log(`--- stream ${s.status} ${ms2}ms events=${s.events.length} headers=${JSON.stringify(s.headers)}`);52  console.log("first event", short(s.events[0]?.data, 500));53  console.log("second event", short(s.events[1]?.data, 500));54  console.log("last-2 events", short(s.events[s.events.length - 2]?.data, 500), "|", short(s.events[s.events.length - 1]?.data, 100));55  console.log("delta keys", [...deltaKeys], "content shapes", contentShapes, "finish", finish);56  console.log("usage chunk", JSON.stringify(usageChunk));57  console.log("text", short(text, 200));58  results[model] = { nonStream: r, stream: s };59}60save("01-chat-stream.json", results);61