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%
4.0 KB · 61 lines typescript
Raw Blame History
1import { rawPost, rawGet, rawSSE, save, short } from "./lib.ts";23const out: any = {};4// A) Responses API web_search on kimi-k3 (streamed) — server-side tool5const s = await rawSSE("/responses", { model: "kimi-k3", input: "What was the closing value of the S&P 500 yesterday? Cite the source URL.", tools: [{ type: "web_search" }], reasoning: { effort: "low" }, max_output_tokens: 4000 });6const types = [...new Set(s.chunks.filter((c) => c !== "[DONE]").map((c: any) => c.type))];7console.log("responses web_search:", s.status, "event types:", types, "error:", short(s.error, 400));8const completed = s.chunks.find((c: any) => c?.type === "response.completed");9if (completed) {10  const resp = (completed as any).response;11  console.log("  output item types:", resp.output?.map((o: any) => o.type), "usage:", JSON.stringify(resp.usage));12  const txt = resp.output?.find((o: any) => o.type === "message");13  console.log("  text:", short(txt?.content?.[0]?.text, 600));14  console.log("  annotations:", short(txt?.content?.[0]?.annotations, 800));15  const ws = resp.output?.find((o: any) => o.type === "web_search_call");16  console.log("  web_search_call item:", short(ws, 800));17  console.log("  response keys:", Object.keys(resp));18}19const evLines = s.rawLines.filter((l) => l.startsWith("event:")).slice(0, 5);20console.log("  sample event: lines:", evLines);21out.responsesWebSearch = { status: s.status, types, chunks: s.chunks, error: s.error, headers: s.headers };2223// B) Responses API plain (no tools) — check basic shape + reasoning24const s2 = await rawPost("/responses", { model: "kimi-k3", input: "Say hi in 3 words.", reasoning: { effort: "low" }, max_output_tokens: 2000 });25console.log("responses plain:", s2.status, short((s2.body as any)?.output ?? s2.body, 600), JSON.stringify((s2.body as any)?.usage));26out.responsesPlain = s2;27// Responses on k2.6 (docs: k3 only)28const s3 = await rawPost("/responses", { model: "kimi-k2.6", input: "Say hi in 3 words.", max_output_tokens: 500 });29console.log("responses k2.6:", s3.status, short(s3.body, 300));30out.responsesK26 = s3;3132// C) $web_search builtin on kimi-k2.6 (thinking enabled) verbatim round trip33{34  const model = "kimi-k2.6";35  const tools = [{ type: "builtin_function", function: { name: "$web_search" } }];36  const messages: any[] = [{ role: "user", content: "What was the closing value of the S&P 500 yesterday? Search the web and cite the source URL." }];37  const r1 = await rawPost("/chat/completions", { model, messages, tools, max_tokens: 6000 });38  const m = (r1.body as any)?.choices?.[0]?.message;39  console.log(`[k2.6] $web_search step1 ${r1.status} finish=${(r1.body as any)?.choices?.[0]?.finish_reason} usage=${JSON.stringify((r1.body as any)?.usage)} msg=${short(m ?? r1.body, 700)}`);40  out.k26step1 = r1;41  if (m?.tool_calls?.length) {42    const toolMsgs = m.tool_calls.map((tc: any) => ({ role: "tool", tool_call_id: tc.id, name: tc.function.name, content: tc.function.arguments }));43    const r2 = await rawPost("/chat/completions", { model, messages: [...messages, m, ...toolMsgs], tools, max_tokens: 6000 });44    console.log(`[k2.6] $web_search step2 verbatim ${r2.status} usage=${JSON.stringify((r2.body as any)?.usage)} ->`, short((r2.body as any)?.choices?.[0]?.message?.content ?? r2.body, 900));45    out.k26step2 = r2;46  }47}48// D) $web_search on k2.7-code (one shot)49{50  const r = await rawPost("/chat/completions", { model: "kimi-k2.7-code", messages: [{ role: "user", content: "Search the web: who won the 2026 Stanley Cup?" }], tools: [{ type: "builtin_function", function: { name: "$web_search" } }], max_tokens: 4000 });51  console.log(`[k2.7-code] $web_search step1 ${r.status}`, short((r.body as any)?.choices?.[0]?.message ?? r.body, 500));52  out.k27step1 = r;53}54// E) formulas (official tools) discovery55for (const p of ["/formulas", "/formulas/moonshot/web-search/tools", "/formulas/web-search/tools"]) {56  const r = await rawGet(p);57  console.log(`GET ${p}:`, r.status, short(r.body, 500));58  out[`formulas:${p}`] = r;59}60save("07-websearch-alt", out);61