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%
6.3 KB · 93 lines typescript
Raw Blame History
1import { client, rawPost, rawGet, save, short, last429, sleep } from "./lib.ts";2import { readFileSync } from "node:fs";34const out: any = {};5const q = "What was the closing value of the S&P 500 on its most recent trading day? Search the web and cite the source URL.";6const builtin = [{ type: "builtin_function", function: { name: "$web_search" } }];78// (a) k3 $web_search verbatim, default effort9{10  const messages: any[] = [{ role: "user", content: q }];11  const r1 = await rawPost("/chat/completions", { model: "kimi-k3", messages, tools: builtin, max_tokens: 8000 });12  const m = (r1.body as any)?.choices?.[0]?.message;13  console.log("[k3 default effort] step1", r1.status, short(m, 400));14  if (m?.tool_calls?.length) {15    const r2 = await rawPost("/chat/completions", { model: "kimi-k3", messages: [...messages, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }))], tools: builtin, max_tokens: 8000 });16    console.log("[k3 default effort] step2 verbatim", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 500));17    out.k3builtin = { r1, r2 };18  }19}20// (b) k2.7-code verbatim step2 from saved step121{22  const prev = JSON.parse(readFileSync(new URL("./out/07-websearch-alt.json", import.meta.url), "utf8"));23  const m = prev.k27step1?.body?.choices?.[0]?.message;24  if (m?.tool_calls?.length) {25    const messages = [{ role: "user", content: "Search the web: who won the 2026 Stanley Cup?" }];26    const r2 = await rawPost("/chat/completions", { model: "kimi-k2.7-code", messages: [...messages, m, ...m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }))], tools: builtin, max_tokens: 8000 });27    console.log("[k2.7-code] $web_search step2 verbatim", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 500));28    out.k27builtinStep2 = r2;29  }30}31// (c) formulas: official web-search tool via fibers on k332{33  const decl = await rawGet("/formulas/moonshot/web-search:latest/tools");34  console.log("[formula] GET tools", decl.status, short(decl.body, 300));35  const tools = (decl.body as any)?.tools ?? [];36  const messages: any[] = [{ role: "user", content: q }];37  const r1 = await rawPost("/chat/completions", { model: "kimi-k3", messages, tools, reasoning_effort: "low", max_tokens: 8000 });38  const m = (r1.body as any)?.choices?.[0]?.message;39  console.log("[formula] step1", r1.status, JSON.stringify((r1.body as any)?.usage), short(m, 500));40  out.formula = { decl, r1 };41  if (m?.tool_calls?.length) {42    const tc = m.tool_calls[0];43    const fiber = await rawPost("/formulas/moonshot/web-search:latest/fibers", { name: tc.function.name, arguments: tc.function.arguments });44    const fb: any = fiber.body;45    console.log("[formula] fiber", fiber.status, "keys:", Object.keys(fb ?? {}), "status:", fb?.status, "context keys:", Object.keys(fb?.context ?? {}), "output:", short(fb?.context?.output, 300), "encrypted:", short(fb?.context?.encrypted_output, 120));46    out.formula.fiber = fiber;47    const content = fb?.context?.encrypted_output ?? fb?.context?.output ?? "";48    const r2 = await rawPost("/chat/completions", { model: "kimi-k3", messages: [...messages, m, { role: "tool", tool_call_id: tc.id, content }], tools, reasoning_effort: "low", max_tokens: 8000 });49    console.log("[formula] step2", r2.status, JSON.stringify((r2.body as any)?.usage), short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 600));50    out.formula.r2 = r2;51  }52}53// (d) k2.6 json_schema strict again: thinking on and off54{55  const schema = { type: "object", properties: { city: { type: "string" }, population: { type: "integer" }, size: { type: "string", enum: ["small", "medium", "large"] } }, required: ["city", "population", "size"], additionalProperties: false };56  for (const thinking of [{ type: "enabled" }, { type: "disabled" }]) {57    const r = await rawPost("/chat/completions", { model: "kimi-k2.6", messages: [{ role: "user", content: "Describe Montreal." }], response_format: { type: "json_schema", json_schema: { name: "city_info", strict: true, schema } }, thinking, max_tokens: 3000 });58    const c = (r.body as any)?.choices?.[0]?.message?.content; let ok = false; try { JSON.parse(c); ok = true; } catch {}59    console.log(`[k2.6 json_schema thinking=${thinking.type}]`, r.status, "parses:", ok, short(c, 200), JSON.stringify((r.body as any)?.usage));60    out[`k26schema:${thinking.type}`] = r;61  }62}63// (e) highspeed logprobs single; 429 headers if any64{65  await sleep(2000);66  const r = await rawPost("/chat/completions", { model: "kimi-k2.7-code-highspeed", messages: [{ role: "user", content: "Reply: ok" }], logprobs: true, top_logprobs: 2, max_tokens: 60 });67  console.log("[highspeed logprobs]", r.status, r.attempts, short((r.body as any)?.choices?.[0]?.logprobs ?? r.body, 300));68  out.hsLogprobs = r;69  console.log("last429 headers:", JSON.stringify(last429?.headers), short(last429?.body, 200));70  out.last429 = last429;71}72// (f) OpenAI SDK smoke test: streaming + thinking + tools on k2.673{74  const stream = await client.chat.completions.create({75    model: "kimi-k2.6",76    messages: [{ role: "user", content: "Weather in Montreal? Use the tool." }],77    tools: [{ type: "function", function: { name: "get_weather", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] } } }],78    stream: true, stream_options: { include_usage: true }, max_tokens: 3000,79    ...( { thinking: { type: "enabled" } } as any ),80  });81  let reasoning = "", args = "", usage: any = null, finish: string | null = null, id = "";82  for await (const chunk of stream) {83    const d: any = chunk.choices[0]?.delta ?? {};84    if (d.reasoning_content) reasoning += d.reasoning_content;85    for (const tc of d.tool_calls ?? []) { if (tc.id) id = tc.id; args += tc.function?.arguments ?? ""; }86    if (chunk.choices[0]?.finish_reason) finish = chunk.choices[0].finish_reason;87    if (chunk.usage) usage = chunk.usage;88  }89  console.log("[SDK] finish:", finish, "reasoning chars:", reasoning.length, "tool id:", id, "args:", args, "usage:", JSON.stringify(usage));90  out.sdk = { finish, reasoningLen: reasoning.length, id, args, usage };91}92save("08-misc", out);93