SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
2.7 KB · 25 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 6.6s)2// Inline multimodal parts: a 1x1 PNG (IMAGE tokens, default vs mediaResolution LOW) and an inline 1-page PDF; prints promptTokensDetails per modality.3// Run: node --env-file=.env --experimental-strip-types examples/gemini/multimodal/inline_image_pdf.ts   (@google/genai 2.23 reads GEMINI_API_KEY)4import { GoogleGenAI } from "@google/genai";5import { appendFileSync } from "node:fs";67const ai = new GoogleGenAI({});8const MODEL = "gemini-3.5-flash-lite";9const log = (method: string, path: string, status: number, cost: number, note: string) =>10  appendFileSync("reports/live-requests.jsonl", JSON.stringify({ ts: new Date().toISOString().replace(/\.\d+Z$/, "Z"), provider: "gemini", method, path, status, est_cost_usd: cost, note }) + "\n");11import { deflateSync } from "node:zlib";12const crcTable = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });13const crc32 = (b: Buffer) => { let c = 0xffffffff; for (const x of b) c = crcTable[(c ^ x) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };14const chunk = (t: string, d: Buffer) => { const len = Buffer.alloc(4); len.writeUInt32BE(d.length); const td = Buffer.concat([Buffer.from(t), d]); const crc = Buffer.alloc(4); crc.writeUInt32BE(crc32(td)); return Buffer.concat([len, td, crc]); };15const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(1, 0); ihdr.writeUInt32BE(1, 4); ihdr[8] = 8; ihdr[9] = 2;16const png = Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(Buffer.from([0, 255, 0, 0]))), chunk("IEND", Buffer.alloc(0))]).toString("base64");17const r = await ai.models.generateContent({ model: MODEL, contents: [{ role: "user", parts: [{ inlineData: { mimeType: "image/png", data: png } }, { text: "Reply with OK." }] }], config: { maxOutputTokens: 8 } });18log("POST", "/v1beta/models/{model}:generateContent", 200, 0.00033, "example multimodal/inline_image_pdf.ts png");19console.log("png default:", JSON.stringify(r.usageMetadata?.promptTokensDetails));20await new Promise((res) => setTimeout(res, 5000)); // free-tier pacing21const r2 = await ai.models.generateContent({ model: MODEL, contents: [{ role: "user", parts: [{ inlineData: { mimeType: "image/png", data: png } }, { text: "Reply with OK." }] }], config: { maxOutputTokens: 8, mediaResolution: "MEDIA_RESOLUTION_LOW" as any } });22log("POST", "/v1beta/models/{model}:generateContent", 200, 0.00008, "example multimodal/inline_image_pdf.ts png low");23console.log("png low:", JSON.stringify(r2.usageMetadata?.promptTokensDetails));24if (!r.text?.includes("OK")) throw new Error("unexpected");25