import { mistral, openai, post, save, short } from "./lib.ts"; const results: Record = {}; // (k) OpenAI SDK against api.mistral.ai/v1 — non-stream try { const r = await openai.chat.completions.create({ model: "mistral-small-latest", messages: [{ role: "user", content: "Say OK." }], max_tokens: 10, }); results.openaiNonStream = r; console.log("openai sdk non-stream OK", r.model, JSON.stringify(r.usage), short(r.choices[0].message.content, 80)); } catch (e: any) { results.openaiNonStream = { error: e.status, body: e.error ?? e.message }; console.log("openai sdk non-stream ERR", e.status, short(e.error ?? e.message, 300)); } // OpenAI SDK stream with include_usage + reasoning_effort high on the hybrid model try { const stream = await openai.chat.completions.create({ model: "magistral-medium-latest", messages: [{ role: "user", content: "Is 17 prime? One sentence." }], max_tokens: 200, stream: true, stream_options: { include_usage: true }, // @ts-expect-error mistral-specific reasoning_effort: "high", } as any); const shapes: string[] = []; let usage: any, finish: any, n = 0, text = "", thinking = ""; for await (const chunk of stream as any) { n++; const d = chunk.choices?.[0]?.delta; if (d?.content !== undefined) { const c = d.content; const shape = typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c; if (shapes[shapes.length - 1] !== shape) shapes.push(shape); if (typeof c === "string") text += c; else if (Array.isArray(c)) for (const part of c) { if (part.type === "thinking") for (const t of part.thinking ?? []) thinking += t.text ?? ""; if (part.type === "text") text += part.text ?? ""; } } if (chunk.choices?.[0]?.finish_reason) finish = chunk.choices[0].finish_reason; if (chunk.usage) usage = { usage: chunk.usage, choicesLen: chunk.choices?.length }; } results.openaiStream = { chunks: n, shapes, usage, finish, text, thinkingLen: thinking.length }; console.log("openai sdk stream OK", JSON.stringify(results.openaiStream)); } catch (e: any) { results.openaiStream = { error: e.status, body: e.error ?? e.message }; console.log("openai sdk stream ERR", e.status, short(e.error ?? e.message, 300)); } // OpenAI SDK: invalid key error class try { const { default: OpenAI } = await import("openai"); const bad = new OpenAI({ apiKey: "sk-bad", baseURL: "https://api.mistral.ai/v1", maxRetries: 0 }); await bad.chat.completions.create({ model: "mistral-small-latest", messages: [{ role: "user", content: "x" }], max_tokens: 1 }); } catch (e: any) { results.openaiBadKey = { name: e.constructor?.name, status: e.status, error: e.error, message: e.message }; console.log("openai sdk bad key:", e.constructor?.name, e.status, short(e.error, 200), "|", short(e.message, 200)); } // Mistral SDK: complete + stream try { const r = await mistral.chat.complete({ model: "mistral-small-latest", messages: [{ role: "user", content: "Say OK." }], maxTokens: 10 }); results.mistralComplete = r; console.log("mistral sdk complete OK", r.model, JSON.stringify(r.usage), short(r.choices?.[0]?.message?.content, 80)); } catch (e: any) { console.log("mistral sdk complete ERR", short(e.message, 300)); } try { const s = await mistral.chat.stream({ model: "magistral-small-latest", messages: [{ role: "user", content: "Is 17 prime? One sentence." }], maxTokens: 200, reasoningEffort: "high", } as any); const shapes: string[] = []; let n = 0, last: any; for await (const ev of s) { n++; last = ev; const c = ev.data?.choices?.[0]?.delta?.content; const shape = c === undefined ? "undefined" : typeof c === "string" ? "string" : Array.isArray(c) ? "array:" + c.map((x: any) => x.type).join(",") : typeof c; if (shapes[shapes.length - 1] !== shape) shapes.push(shape); if (n <= 2) console.log("mistral sdk stream event", n, short(ev, 400)); } results.mistralStream = { events: n, shapes, last }; console.log("mistral sdk stream OK events", n, shapes, "last", short(last, 400)); } catch (e: any) { console.log("mistral sdk stream ERR", short(e.message, 300)); } try { const { Mistral } = await import("@mistralai/mistralai"); const bad = new Mistral({ apiKey: "sk-bad", retryConfig: { strategy: "none" } }); await bad.chat.complete({ model: "mistral-small-latest", messages: [{ role: "user", content: "x" }], maxTokens: 1 }); } catch (e: any) { results.mistralBadKey = { name: e.constructor?.name, statusCode: e.statusCode, message: e.message, body: e.body }; console.log("mistral sdk bad key:", e.constructor?.name, e.statusCode, short(e.message, 200), short(e.body, 200)); } // (j) prompt caching: same long prefix twice with prompt_cache_key const 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(" "); const cachePayload = (q: string) => ({ model: "mistral-small-latest", max_tokens: 10, prompt_cache_key: "polyllm-cache-probe-1", messages: [{ role: "system", content: "You are a terse assistant. Context: " + filler }, { role: "user", content: q }], }); const c1 = await post("/chat/completions", cachePayload("Say A.")); const c2 = await post("/chat/completions", cachePayload("Say B.")); const c3 = await post("/chat/completions", { ...cachePayload("Say C."), prompt_cache_key: undefined }); console.log("cache #1", c1.status, JSON.stringify((c1.body as any)?.usage), "cost hdr", (c1.headers as any)["x-ratelimit-tokens-query-cost"]); console.log("cache #2", c2.status, JSON.stringify((c2.body as any)?.usage), "cost hdr", (c2.headers as any)["x-ratelimit-tokens-query-cost"]); console.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"]); results.cache = { c1, c2, c3 }; save("08-sdks-cache.json", results);