// Probe (f): vision with a tiny generated PNG (2x2, red/green/blue/white) as base64 data URL. import { deflateSync } from "node:zlib"; import { CHAT_MODELS, raw, save, short } from "./lib.ts"; function crc32(buf: Buffer) { let c, crc = 0xffffffff; for (let n = 0; n < buf.length; n++) { c = (crc ^ buf[n]) & 0xff; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; crc = (crc >>> 8) ^ c; } return (crc ^ 0xffffffff) >>> 0; } function chunk(type: string, data: Buffer) { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const td = Buffer.concat([Buffer.from(type, "ascii"), data]); const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(td)); return Buffer.concat([len, td, crc]); } // xAI rejects images < 8x8 ("Image dimensions 2x2 are too small. Both width and height must be at least 8 pixels.") // so we build an 8x8 PNG with four solid quadrants: red, green / blue, white. function png32x32() { // second rejection: "Image has 64 total pixels (8x8), which is below the minimum of 512 pixels." -> use 32x32 const W = 32, H = 32; const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(W, 0); ihdr.writeUInt32BE(H, 4); ihdr[8] = 8; ihdr[9] = 2; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0; const rows: number[] = []; for (let y = 0; y < H; y++) { rows.push(0); // filter byte for (let x = 0; x < W; x++) { const top = y < H / 2, left = x < W / 2; rows.push(...(top ? (left ? [255, 0, 0] : [0, 255, 0]) : left ? [0, 0, 255] : [255, 255, 255])); } } return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(Buffer.from(rows))), chunk("IEND", Buffer.alloc(0))]); } const dataUrl = `data:image/png;base64,${png32x32().toString("base64")}`; console.log("data url length", dataUrl.length); const results: Record = {}; await Promise.all( CHAT_MODELS.map(async (model) => { const r = await raw("/chat/completions", { method: "POST", body: JSON.stringify({ model, messages: [{ role: "user", content: [{ type: "image_url", image_url: { url: dataUrl, detail: "high" } }, { type: "text", text: "Describe this image in one sentence. What colors do you see?" }] }], max_completion_tokens: 200, }), }); const b: any = r.body; results[model] = { status: r.status, content: b.choices?.[0]?.message?.content, usage: b.usage, error: r.status !== 200 ? b : undefined }; console.log(model.padEnd(30), r.status, short(b.choices?.[0]?.message?.content ?? b, 160), "| usage", short(b.usage, 200)); }), ); save("05-vision.json", results);