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.1 KB · 49 lines typescript
Raw Blame History
1import { CHAT_MODELS, post, save, short } from "./lib.ts";23const schema = {4  type: "object",5  properties: {6    city: { type: "string" },7    country: { type: "string" },8    population_millions: { type: "number" },9    landmarks: { type: "array", items: { type: "string" } },10  },11  required: ["city", "country", "population_millions", "landmarks"],12  additionalProperties: false,13};14const results: Record<string, any> = {};15for (const model of CHAT_MODELS) {16  const r = await post("/chat/completions", {17    model,18    messages: [{ role: "user", content: "Give facts about Montreal." }],19    max_tokens: 200,20    response_format: { type: "json_schema", json_schema: { name: "city_facts", strict: true, schema, description: "City facts" } },21  });22  const body: any = r.body;23  let parsed: any = null;24  const c = body?.choices?.[0]?.message?.content;25  try {26    parsed = JSON.parse(typeof c === "string" ? c : c?.find((x: any) => x.type === "text")?.text);27  } catch {}28  console.log(`${model.padEnd(28)} ${r.status} finish=${body?.choices?.[0]?.finish_reason} valid=${!!parsed} keys=${parsed ? Object.keys(parsed) : ""} ${short(c ?? body, 160)}`);29  results[model] = r;30}31// strict:false + schema with unsupported keyword, and json_object without mentioning JSON32const r2 = await post("/chat/completions", {33  model: "mistral-small-latest",34  messages: [{ role: "user", content: "Give facts about Montreal." }],35  max_tokens: 100,36  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 } } },37});38console.log("min/max keyword", r2.status, short(r2.body, 300));39const r3 = await post("/chat/completions", {40  model: "mistral-small-latest",41  messages: [{ role: "user", content: "Give facts about Montreal." }],42  max_tokens: 100,43  response_format: { type: "json_object" },44});45console.log("json_object without JSON in prompt", r3.status, short(r3.body?.choices?.[0]?.message?.content ?? r3.body, 300));46results.minmax = r2;47results.jsonObjectNoMention = r3;48save("04-structured.json", results);49