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.9 KB · 70 lines typescript
Raw Blame History
1import { chat, save, MODELS, short } from "./lib.ts";2import { deflateSync } from "node:zlib";34// Build a 32x32 solid red PNG (no deps)5function crc32(buf: Buffer) {6  let c,7    crc = 0xffffffff;8  for (let n = 0; n < buf.length; n++) {9    c = (crc ^ buf[n]) & 0xff;10    for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;11    crc = (crc >>> 8) ^ c;12  }13  return (crc ^ 0xffffffff) >>> 0;14}15function chunk(type: string, data: Buffer) {16  const len = Buffer.alloc(4);17  len.writeUInt32BE(data.length);18  const td = Buffer.concat([Buffer.from(type), data]);19  const crc = Buffer.alloc(4);20  crc.writeUInt32BE(crc32(td));21  return Buffer.concat([len, td, crc]);22}23function png(w: number, h: number) {24  const ihdr = Buffer.alloc(13);25  ihdr.writeUInt32BE(w, 0);26  ihdr.writeUInt32BE(h, 4);27  ihdr[8] = 8; // bit depth28  ihdr[9] = 2; // RGB29  const rows: Buffer[] = [];30  for (let y = 0; y < h; y++) {31    const row = Buffer.alloc(1 + w * 3);32    for (let x = 0; x < w; x++) {33      row[1 + x * 3] = 220;34      row[2 + x * 3] = 30;35      row[3 + x * 3] = 30;36    }37    rows.push(row);38  }39  return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(Buffer.concat(rows))), chunk("IEND", Buffer.alloc(0))]);40}4142const dataUrl = `data:image/png;base64,${png(32, 32).toString("base64")}`;43const out: Record<string, unknown> = {};44for (const model of MODELS) {45  const r = await chat({46    model,47    messages: [{ role: "user", content: [{ type: "text", text: "What colour is this image? One word." }, { type: "image_url", image_url: { url: dataUrl } }] }],48    max_completion_tokens: 60,49  });50  const b: any = r.body;51  out[`${model}:png32`] = { status: r.status, content: short(b?.choices?.[0]?.message?.content, 100), usage: b?.usage, error: r.status !== 200 ? b : undefined };52  console.log(model, "vision 32x32 png:", r.status, short(r.status === 200 ? { content: b?.choices?.[0]?.message?.content, usage: b?.usage } : b, 400));53}54// external URL + detail on the vision-capable model55const rUrl = await chat({56  model: "qwen-3.8-27b",57  messages: [{ role: "user", content: [{ type: "text", text: "Describe." }, { type: "image_url", image_url: { url: "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png", detail: "low" } }] }],58  max_completion_tokens: 40,59});60out["qwen:https-url"] = rUrl;61console.log("qwen https url:", rUrl.status, short(rUrl.body, 300));62const rDetail = await chat({63  model: "qwen-3.8-27b",64  messages: [{ role: "user", content: [{ type: "text", text: "Colour? One word." }, { type: "image_url", image_url: { url: dataUrl, detail: "high" } }] }],65  max_completion_tokens: 40,66});67out["qwen:detail-high"] = { status: rDetail.status, body: rDetail.status === 200 ? short((rDetail.body as any)?.choices?.[0]?.message?.content, 100) : rDetail.body };68console.log("qwen detail=high:", rDetail.status, short(rDetail.body, 300));69save("05-vision.json", out);70