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%
4.6 KB · 40 lines typescript
Raw Blame History
1import { MODELS, rawPost, save, short, pngDataUrl } from "./lib.ts";23const out: Record<string, unknown> = {};4await Promise.all(MODELS.map(async (model) => {5  const res: any = {};6  // (e) JSON mode with a schema described in prompt; streaming off; thinking model → check content parses7  const j = await rawPost("/chat/completions", { model, messages: [{ role: "system", content: "Output JSON only." }, { role: "user", content: "Give me a JSON object with fields city (string) and population (integer) for Montreal." }], response_format: { type: "json_object" }, max_tokens: 3000 });8  const jc = (j.body as any)?.choices?.[0]?.message?.content;9  let parsed: any = null; try { parsed = JSON.parse(jc); } catch {}10  console.log(`[${model}] json_object ${j.status} finish=${(j.body as any)?.choices?.[0]?.finish_reason} parses=${parsed !== null} ${short(jc, 120)} usage=${JSON.stringify((j.body as any)?.usage)}`);11  res.jsonObject = { status: j.status, content: jc, parses: parsed !== null, usage: (j.body as any)?.usage, error: j.status === 200 ? null : j.body };1213  // json_schema strict with nested + enum14  const schema = { type: "object", properties: { city: { type: "string" }, population: { type: "integer" }, tags: { type: "array", items: { type: "string" } }, size: { type: "string", enum: ["small", "medium", "large"] } }, required: ["city", "population", "tags", "size"], additionalProperties: false };15  const js = await rawPost("/chat/completions", { model, messages: [{ role: "user", content: "Describe Montreal." }], response_format: { type: "json_schema", json_schema: { name: "city_info", strict: true, schema } }, max_tokens: 3000 });16  const jsc = (js.body as any)?.choices?.[0]?.message?.content;17  let parsed2: any = null; try { parsed2 = JSON.parse(jsc); } catch {}18  console.log(`[${model}] json_schema ${js.status} finish=${(js.body as any)?.choices?.[0]?.finish_reason} parses=${parsed2 !== null} keysOk=${parsed2 && Object.keys(parsed2).sort().join() === "city,population,size,tags"} ${short(jsc, 120)}`);19  res.jsonSchema = { status: js.status, content: jsc, parses: parsed2 !== null, error: js.status === 200 ? null : js.body };2021  // (f) vision: 32x32 red PNG; object-form image_url (OpenAI style)22  const v = await rawPost("/chat/completions", { model, messages: [{ role: "user", content: [{ type: "text", text: "What colour is this image? One word." }, { type: "image_url", image_url: { url: pngDataUrl(32) } }] }], max_tokens: 2000 });23  console.log(`[${model}] vision 32x32 (image_url object) ${v.status} ${short((v.body as any)?.choices?.[0]?.message?.content ?? (v.body as any)?.error, 150)} usage=${JSON.stringify((v.body as any)?.usage)}`);24  res.vision32 = { status: v.status, content: (v.body as any)?.choices?.[0]?.message?.content, usage: (v.body as any)?.usage, error: v.status === 200 ? null : v.body };25  // image_url as plain string (docs show string form)26  const v2 = await rawPost("/chat/completions", { model, messages: [{ role: "user", content: [{ type: "text", text: "What colour is this image? One word." }, { type: "image_url", image_url: pngDataUrl(32, [20, 40, 220, 255]) }] }], max_tokens: 2000 });27  console.log(`[${model}] vision 32x32 (image_url string) ${v2.status} ${short((v2.body as any)?.choices?.[0]?.message?.content ?? (v2.body as any)?.error, 150)}`);28  res.vision32String = { status: v2.status, content: (v2.body as any)?.choices?.[0]?.message?.content, error: v2.status === 200 ? null : v2.body };29  // tiny 2x230  const v3 = await rawPost("/chat/completions", { model, messages: [{ role: "user", content: [{ type: "text", text: "Colour? One word." }, { type: "image_url", image_url: { url: pngDataUrl(2) } }] }], max_tokens: 2000 });31  console.log(`[${model}] vision 2x2 ${v3.status} ${short((v3.body as any)?.choices?.[0]?.message?.content ?? (v3.body as any)?.error, 150)} usage=${JSON.stringify((v3.body as any)?.usage)}`);32  res.vision2 = { status: v3.status, content: (v3.body as any)?.choices?.[0]?.message?.content, usage: (v3.body as any)?.usage, error: v3.status === 200 ? null : v3.body };33  out[model] = res;34}));35// public URL image (docs say unsupported) — one model36const u = await rawPost("/chat/completions", { model: "kimi-k2.6", messages: [{ role: "user", content: [{ type: "text", text: "Describe." }, { type: "image_url", image_url: { url: "https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png" } }] }], max_tokens: 500, thinking: { type: "disabled" } });37console.log(`[kimi-k2.6] vision public URL ${u.status} ${short((u.body as any)?.choices?.[0]?.message?.content ?? (u.body as any)?.error, 300)}`);38out["publicUrl"] = u;39save("04-json-vision", out);40