import { CHAT_MODELS, post, save, short } from "./lib.ts"; const schema = { type: "object", properties: { city: { type: "string" }, country: { type: "string" }, population_millions: { type: "number" }, landmarks: { type: "array", items: { type: "string" } }, }, required: ["city", "country", "population_millions", "landmarks"], additionalProperties: false, }; const results: Record = {}; for (const model of CHAT_MODELS) { const r = await post("/chat/completions", { model, messages: [{ role: "user", content: "Give facts about Montreal." }], max_tokens: 200, response_format: { type: "json_schema", json_schema: { name: "city_facts", strict: true, schema, description: "City facts" } }, }); const body: any = r.body; let parsed: any = null; const c = body?.choices?.[0]?.message?.content; try { parsed = JSON.parse(typeof c === "string" ? c : c?.find((x: any) => x.type === "text")?.text); } catch {} console.log(`${model.padEnd(28)} ${r.status} finish=${body?.choices?.[0]?.finish_reason} valid=${!!parsed} keys=${parsed ? Object.keys(parsed) : ""} ${short(c ?? body, 160)}`); results[model] = r; } // strict:false + schema with unsupported keyword, and json_object without mentioning JSON const r2 = await post("/chat/completions", { model: "mistral-small-latest", messages: [{ role: "user", content: "Give facts about Montreal." }], max_tokens: 100, response_format: { type: "json_schema", json_schema: { name: "x", strict: true, schema: { type: "object", properties: { n: { type: "integer", minimum: 0, maximum: 10 } }, required: ["n"], additionalProperties: false } } }, }); console.log("min/max keyword", r2.status, short(r2.body, 300)); const r3 = await post("/chat/completions", { model: "mistral-small-latest", messages: [{ role: "user", content: "Give facts about Montreal." }], max_tokens: 100, response_format: { type: "json_object" }, }); console.log("json_object without JSON in prompt", r3.status, short(r3.body?.choices?.[0]?.message?.content ?? r3.body, 300)); results.minmax = r2; results.jsonObjectNoMention = r3; save("04-structured.json", results);