import { chat, rawSSE, save, MODELS, short } from "./lib.ts"; const out: Record = {}; // (1) json_object + stream (docs: incompatible) for (const model of MODELS) { const s = await rawSSE("/chat/completions", { model, messages: [{ role: "user", content: "Return a JSON object with key answer = 4." }], max_completion_tokens: 150, response_format: { type: "json_object" }, stream: true, reasoning_effort: model === "gpt-oss-120b" ? "low" : "none", }); let content = ""; for (const e of s.events) if (e.data !== "[DONE]" && e.data?.choices?.[0]?.delta?.content) content += e.data.choices[0].delta.content; out[`${model}:json_object_stream`] = { status: s.status, error: s.error, nEvents: s.events.length, content }; console.log(model, "json_object+stream:", s.status, s.error ? short(s.error, 300) : `events=${s.events.length} content=${JSON.stringify(content)}`); } // (2) qwen over-context (retry after TPM window) { const words = ["alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliet", "kilo", "lima"]; const parts: string[] = []; for (let i = 0; i < 140_000; i++) parts.push(words[i % words.length]); const r = await chat({ model: "qwen-3.8-27b", messages: [{ role: "user", content: `Reply OK.\n${parts.join(" ")}` }], max_completion_tokens: 10, reasoning_effort: "none" }); out["qwen:ctx140k"] = { status: r.status, body: r.body, headers: r.headers }; console.log("qwen ctx140k:", r.status, short(r.body, 300), r.status === 429 ? JSON.stringify(r.headers) : ""); } // (3) logprobs + reasoning_logprobs on reasoning models for (const model of ["qwen-3.8-27b", "gpt-oss-120b"]) { const r = await chat({ model, messages: [{ role: "user", content: "Reply with the single word: pong" }], max_completion_tokens: 150, logprobs: true, top_logprobs: 2, reasoning_effort: "low" }); const b: any = r.body; const ch = b?.choices?.[0]; out[`${model}:logprobs`] = { status: r.status, choiceKeys: ch && Object.keys(ch), logprobs: short(ch?.logprobs, 300), reasoning_logprobs: short(ch?.reasoning_logprobs, 300), finish: ch?.finish_reason }; console.log(model, "logprobs:", r.status, "choice keys", ch && Object.keys(ch), "\n logprobs:", short(ch?.logprobs, 200), "\n reasoning_logprobs:", short(ch?.reasoning_logprobs, 200)); } // (4) qwen with `stop` and reasoning none to see stop works on content { const r = await chat({ model: "qwen-3.8-27b", messages: [{ role: "user", content: "Count from 1 to 10 separated by commas." }], max_completion_tokens: 60, stop: [", 5"], reasoning_effort: "none" }); const b: any = r.body; out["qwen:stop"] = { status: r.status, content: b?.choices?.[0]?.message?.content, finish: b?.choices?.[0]?.finish_reason }; console.log("qwen stop:", r.status, JSON.stringify(b?.choices?.[0]?.message?.content), b?.choices?.[0]?.finish_reason); } // (5) seed determinism (gemma, 2 calls) { const mk = () => chat({ model: "gemma-4-31b", messages: [{ role: "user", content: "Give me a random 6-digit number, digits only." }], max_completion_tokens: 20, seed: 7, temperature: 1 }); const a: any = (await mk()).body, b: any = (await mk()).body; out["gemma:seed"] = { a: a?.choices?.[0]?.message?.content, b: b?.choices?.[0]?.message?.content, fp: [a?.system_fingerprint, b?.system_fingerprint] }; console.log("gemma seed=7 twice:", JSON.stringify(out["gemma:seed"])); } // (6) prompt caching: repeat same long-ish prefix twice { const prefix = "You are a helpful assistant. " + "Context paragraph about Montreal geography and history. ".repeat(40); const mk = () => chat({ model: "gpt-oss-120b", messages: [{ role: "system", content: prefix }, { role: "user", content: "Reply OK." }], max_completion_tokens: 20, reasoning_effort: "low" }); const a: any = (await mk()).body, b: any = (await mk()).body; out["gptoss:cache"] = { first: a?.usage, second: b?.usage }; console.log("cache: first cached", a?.usage?.prompt_tokens_details?.cached_tokens, "second cached", b?.usage?.prompt_tokens_details?.cached_tokens, "of", b?.usage?.prompt_tokens); } // (7) OpenAI SDK compatibility (openai@7) streaming with reasoning field { const { openai } = await import("./lib.ts"); const stream = await openai.chat.completions.create({ model: "gpt-oss-120b", messages: [{ role: "user", content: "Say hi." }], stream: true, max_completion_tokens: 100, reasoning_effort: "low" } as any); let reasoning = "", content = "", usage: unknown; for await (const chunk of stream as any) { const d = chunk.choices?.[0]?.delta ?? {}; if (d.reasoning) reasoning += d.reasoning; if (d.content) content += d.content; if (chunk.usage) usage = chunk.usage; } out["openai-sdk-stream"] = { reasoning: short(reasoning, 100), content, usage }; console.log("openai sdk stream ok: reasoning?", reasoning.length > 0, "content", JSON.stringify(content), "usage?", !!usage); } save("08-misc.json", out);