TypeScript 97.4%
SQL 1%
JavaScript 0.9%
CSS 0.6%
1import { post, rawSSE, raw, save, short } from "./lib.ts";23const results: Record<string, any> = {};4const q = { role: "user", content: "Is 221 a prime number? Answer briefly." };56// Reasoning non-stream full shape on both hybrid models + glm7for (const model of ["magistral-medium-latest", "mistral-small-latest", "glm-5-2"]) {8 const r = await post("/chat/completions", { model, messages: [q], max_tokens: 400, reasoning_effort: "high" });9 const b: any = r.body;10 const msg = b?.choices?.[0]?.message;11 console.log(`\n=== ${model} reasoning_effort=high ${r.status} finish=${b?.choices?.[0]?.finish_reason} usage=${JSON.stringify(b?.usage)}`);12 console.log("content", short(msg?.content, 1200));13 results[`${model}_high`] = r;14}1516// Streaming shape with reasoning on medium: record the sequence of delta.content shapes and the raw first thinking chunk17const s = await rawSSE("/chat/completions", { model: "magistral-medium-latest", messages: [q], max_tokens: 400, reasoning_effort: "high", stream: true });18const seq: string[] = [];19let firstThink: any, transition: any, firstText: any;20for (const e of s.events) {21 if (e.data === "[DONE]") continue;22 const c = e.data?.choices?.[0]?.delta?.content;23 const shape = c === undefined ? "undef" : typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c;24 if (seq[seq.length - 1] !== shape) seq.push(shape);25 if (!firstThink && Array.isArray(c) && c.some((x: any) => x.type === "thinking")) firstThink = e.data;26 if (!transition && Array.isArray(c) && c.length > 1) transition = e.data;27 if (!firstText && typeof c === "string" && c.length && firstThink) firstText = e.data;28}29console.log("\nstream shapes seq", seq, "events", s.events.length);30console.log("first thinking chunk", short(firstThink, 700));31console.log("transition chunk", short(transition, 700));32console.log("first text chunk after thinking", short(firstText, 400));33console.log("last", short(s.events[s.events.length - 2]?.data, 500));34results.streamReasoning = s;3536// Multi-turn replay including the thinking chunk37const first: any = results["magistral-medium-latest_high"].body;38if (first?.choices?.[0]?.message) {39 const replay = await post("/chat/completions", {40 model: "magistral-medium-latest",41 reasoning_effort: "high",42 max_tokens: 200,43 messages: [q, first.choices[0].message, { role: "user", content: "And 223?" }],44 });45 console.log("\nreplay with thinking chunk", replay.status, short((replay.body as any)?.choices?.[0]?.message?.content ?? replay.body, 300));46 results.replay = replay;47 // replay with thinking stripped to text only48 const textOnly = Array.isArray(first.choices[0].message.content)49 ? first.choices[0].message.content.filter((c: any) => c.type === "text").map((c: any) => c.text).join("")50 : first.choices[0].message.content;51 const replay2 = await post("/chat/completions", {52 model: "magistral-medium-latest",53 reasoning_effort: "high",54 max_tokens: 200,55 messages: [q, { role: "assistant", content: textOnly }, { role: "user", content: "And 223?" }],56 });57 console.log("replay text-only", replay2.status, short((replay2.body as any)?.choices?.[0]?.message?.content ?? replay2.body, 200));58 results.replayTextOnly = replay2;59}6061// prompt_mode reasoning on small (legacy Magistral param) — shape62const pm = await post("/chat/completions", { model: "mistral-small-latest", messages: [q], max_tokens: 300, prompt_mode: "reasoning" });63console.log("\nprompt_mode reasoning (small)", pm.status, JSON.stringify((pm.body as any)?.usage), short((pm.body as any)?.choices?.[0]?.message?.content ?? pm.body, 500));64results.promptMode = pm;6566// max_tokens truncation during thinking -> finish_reason?67const trunc = await post("/chat/completions", { model: "magistral-medium-latest", messages: [q], max_tokens: 20, reasoning_effort: "high" });68console.log("truncated during thinking", trunc.status, "finish", (trunc.body as any)?.choices?.[0]?.finish_reason, JSON.stringify((trunc.body as any)?.usage), short((trunc.body as any)?.choices?.[0]?.message?.content, 300));69results.trunc = trunc;7071// Labs + third-party quick chat72for (const model of ["labs-leanstral-1-5", "ministral-3b-latest", "ministral-14b-latest", "voxtral-small-latest"]) {73 const r = await post("/chat/completions", { model, messages: [{ role: "user", content: "Say OK." }], max_tokens: 20 });74 console.log(`\n${model} ${r.status} ${short((r.body as any)?.choices?.[0]?.message?.content ?? r.body, 200)} usage=${JSON.stringify((r.body as any)?.usage)} rl=${(r.headers as any)["x-ratelimit-limit-req-minute"]}/${(r.headers as any)["x-ratelimit-limit-tokens-minute"]}`);75 results[`tiny_${model}`] = r;76}7778// FIM on codestral79const fim = await post("/fim/completions", { model: "codestral-latest", prompt: "def fib(n):\n", suffix: "\n return fib(n-1) + fib(n-2)", max_tokens: 40 });80console.log("\nFIM codestral", fim.status, short((fim.body as any)?.choices?.[0]?.message?.content ?? fim.body, 200));81results.fim = fim;82const fimSmall = await post("/fim/completions", { model: "mistral-small-latest", prompt: "def fib(n):\n", max_tokens: 10 });83console.log("FIM on small", fimSmall.status, short(fimSmall.body, 200));84results.fimSmall = fimSmall;8586// web_search tool on chat completions (docs list WebSearchTool in tools[])87const ws = await post("/chat/completions", { model: "mistral-medium-latest", messages: [{ role: "user", content: "What is today's date and one headline from Le Devoir today? Cite." }], tools: [{ type: "web_search" }], max_tokens: 200 });88console.log("\nweb_search on chat completions", ws.status, JSON.stringify((ws.body as any)?.usage), short(ws.body, 1200));89results.webSearchChat = ws;9091// Conversations API: one call with web_search, store false92const conv = await post("/conversations", {93 model: "mistral-medium-latest",94 inputs: "In one sentence, what is the latest stable Node.js version? Cite a source.",95 tools: [{ type: "web_search" }],96 store: false,97 completion_args: { max_tokens: 200 },98});99console.log("\nconversations web_search", conv.status, short(conv.body, 1500));100results.conversation = conv;101// Conversations stream, no tools102const cs = await rawSSE("/conversations", { model: "mistral-small-latest", inputs: "Say hello in 3 words.", store: false, stream: true, completion_args: { max_tokens: 30 } });103console.log("\nconversations stream", cs.status, "events", cs.events.length, "types", [...new Set(cs.events.map((e) => e.event ?? e.data?.type))]);104console.log("first", short(cs.events[0], 400), "\nlast", short(cs.events[cs.events.length - 1], 500));105results.conversationStream = cs;106107// Token counting / tokenize endpoints?108for (const p of ["/tokenize", "/chat/tokenize", "/models/mistral-small-latest/tokenize"]) {109 const t = await post(p, { model: "mistral-small-latest", messages: [{ role: "user", content: "Hello" }] });110 console.log("tokenize", p, t.status, short(t.body, 150));111}112const usage = await raw("/usage");113console.log("GET /usage", usage.status, short(usage.body, 150));114save("09-reasoning-misc.json", results);115