import { MODELS, rawPost, save, short, pngDataUrl } from "./lib.ts"; const out: Record = {}; await Promise.all(MODELS.map(async (model) => { const res: any = {}; // (e) JSON mode with a schema described in prompt; streaming off; thinking model → check content parses 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 }); const jc = (j.body as any)?.choices?.[0]?.message?.content; let parsed: any = null; try { parsed = JSON.parse(jc); } catch {} 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)}`); res.jsonObject = { status: j.status, content: jc, parses: parsed !== null, usage: (j.body as any)?.usage, error: j.status === 200 ? null : j.body }; // json_schema strict with nested + enum 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 }; 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 }); const jsc = (js.body as any)?.choices?.[0]?.message?.content; let parsed2: any = null; try { parsed2 = JSON.parse(jsc); } catch {} 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)}`); res.jsonSchema = { status: js.status, content: jsc, parses: parsed2 !== null, error: js.status === 200 ? null : js.body }; // (f) vision: 32x32 red PNG; object-form image_url (OpenAI style) 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 }); 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)}`); 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 }; // image_url as plain string (docs show string form) 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 }); 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)}`); res.vision32String = { status: v2.status, content: (v2.body as any)?.choices?.[0]?.message?.content, error: v2.status === 200 ? null : v2.body }; // tiny 2x2 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 }); 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)}`); 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 }; out[model] = res; })); // public URL image (docs say unsupported) — one model const 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" } }); console.log(`[kimi-k2.6] vision public URL ${u.status} ${short((u.body as any)?.choices?.[0]?.message?.content ?? (u.body as any)?.error, 300)}`); out["publicUrl"] = u; save("04-json-vision", out);