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%
5.9 KB · 120 lines typescript
Raw Blame History
1import { mistral, openai, post, save, short } from "./lib.ts";23const results: Record<string, any> = {};45// (k) OpenAI SDK against api.mistral.ai/v1 — non-stream6try {7  const r = await openai.chat.completions.create({8    model: "mistral-small-latest",9    messages: [{ role: "user", content: "Say OK." }],10    max_tokens: 10,11  });12  results.openaiNonStream = r;13  console.log("openai sdk non-stream OK", r.model, JSON.stringify(r.usage), short(r.choices[0].message.content, 80));14} catch (e: any) {15  results.openaiNonStream = { error: e.status, body: e.error ?? e.message };16  console.log("openai sdk non-stream ERR", e.status, short(e.error ?? e.message, 300));17}18// OpenAI SDK stream with include_usage + reasoning_effort high on the hybrid model19try {20  const stream = await openai.chat.completions.create({21    model: "magistral-medium-latest",22    messages: [{ role: "user", content: "Is 17 prime? One sentence." }],23    max_tokens: 200,24    stream: true,25    stream_options: { include_usage: true },26    // @ts-expect-error mistral-specific27    reasoning_effort: "high",28  } as any);29  const shapes: string[] = [];30  let usage: any, finish: any, n = 0, text = "", thinking = "";31  for await (const chunk of stream as any) {32    n++;33    const d = chunk.choices?.[0]?.delta;34    if (d?.content !== undefined) {35      const c = d.content;36      const shape = typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c;37      if (shapes[shapes.length - 1] !== shape) shapes.push(shape);38      if (typeof c === "string") text += c;39      else if (Array.isArray(c))40        for (const part of c) {41          if (part.type === "thinking") for (const t of part.thinking ?? []) thinking += t.text ?? "";42          if (part.type === "text") text += part.text ?? "";43        }44    }45    if (chunk.choices?.[0]?.finish_reason) finish = chunk.choices[0].finish_reason;46    if (chunk.usage) usage = { usage: chunk.usage, choicesLen: chunk.choices?.length };47  }48  results.openaiStream = { chunks: n, shapes, usage, finish, text, thinkingLen: thinking.length };49  console.log("openai sdk stream OK", JSON.stringify(results.openaiStream));50} catch (e: any) {51  results.openaiStream = { error: e.status, body: e.error ?? e.message };52  console.log("openai sdk stream ERR", e.status, short(e.error ?? e.message, 300));53}54// OpenAI SDK: invalid key error class55try {56  const { default: OpenAI } = await import("openai");57  const bad = new OpenAI({ apiKey: "sk-bad", baseURL: "https://api.mistral.ai/v1", maxRetries: 0 });58  await bad.chat.completions.create({ model: "mistral-small-latest", messages: [{ role: "user", content: "x" }], max_tokens: 1 });59} catch (e: any) {60  results.openaiBadKey = { name: e.constructor?.name, status: e.status, error: e.error, message: e.message };61  console.log("openai sdk bad key:", e.constructor?.name, e.status, short(e.error, 200), "|", short(e.message, 200));62}6364// Mistral SDK: complete + stream65try {66  const r = await mistral.chat.complete({ model: "mistral-small-latest", messages: [{ role: "user", content: "Say OK." }], maxTokens: 10 });67  results.mistralComplete = r;68  console.log("mistral sdk complete OK", r.model, JSON.stringify(r.usage), short(r.choices?.[0]?.message?.content, 80));69} catch (e: any) {70  console.log("mistral sdk complete ERR", short(e.message, 300));71}72try {73  const s = await mistral.chat.stream({74    model: "magistral-small-latest",75    messages: [{ role: "user", content: "Is 17 prime? One sentence." }],76    maxTokens: 200,77    reasoningEffort: "high",78  } as any);79  const shapes: string[] = [];80  let n = 0, last: any;81  for await (const ev of s) {82    n++;83    last = ev;84    const c = ev.data?.choices?.[0]?.delta?.content;85    const shape = c === undefined ? "undefined" : typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c;86    if (shapes[shapes.length - 1] !== shape) shapes.push(shape);87    if (n <= 2) console.log("mistral sdk stream event", n, short(ev, 400));88  }89  results.mistralStream = { events: n, shapes, last };90  console.log("mistral sdk stream OK events", n, shapes, "last", short(last, 400));91} catch (e: any) {92  console.log("mistral sdk stream ERR", short(e.message, 300));93}94try {95  const { Mistral } = await import("@mistralai/mistralai");96  const bad = new Mistral({ apiKey: "sk-bad", retryConfig: { strategy: "none" } });97  await bad.chat.complete({ model: "mistral-small-latest", messages: [{ role: "user", content: "x" }], maxTokens: 1 });98} catch (e: any) {99  results.mistralBadKey = { name: e.constructor?.name, statusCode: e.statusCode, message: e.message, body: e.body };100  console.log("mistral sdk bad key:", e.constructor?.name, e.statusCode, short(e.message, 200), short(e.body, 200));101}102103// (j) prompt caching: same long prefix twice with prompt_cache_key104const filler = Array.from({ length: 120 }, (_, i) => `Fact ${i}: The quick brown fox number ${i} jumps over the lazy dog while reciting prime ${i * 7 + 3}.`).join(" ");105const cachePayload = (q: string) => ({106  model: "mistral-small-latest",107  max_tokens: 10,108  prompt_cache_key: "polyllm-cache-probe-1",109  messages: [{ role: "system", content: "You are a terse assistant. Context: " + filler }, { role: "user", content: q }],110});111const c1 = await post("/chat/completions", cachePayload("Say A."));112const c2 = await post("/chat/completions", cachePayload("Say B."));113const c3 = await post("/chat/completions", { ...cachePayload("Say C."), prompt_cache_key: undefined });114console.log("cache #1", c1.status, JSON.stringify((c1.body as any)?.usage), "cost hdr", (c1.headers as any)["x-ratelimit-tokens-query-cost"]);115console.log("cache #2", c2.status, JSON.stringify((c2.body as any)?.usage), "cost hdr", (c2.headers as any)["x-ratelimit-tokens-query-cost"]);116console.log("cache #3 (no key)", c3.status, JSON.stringify((c3.body as any)?.usage), "cost hdr", (c3.headers as any)["x-ratelimit-tokens-query-cost"]);117results.cache = { c1, c2, c3 };118119save("08-sdks-cache.json", results);120