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%
1.2 KB · 18 lines typescript
Raw Blame History
1// STATUS: LIVE_VERIFIED — executed 2026-09-18 (exit 0 in 0.8s)2// streamGenerateContent with ?alt=sse (SSE framing) — prints each chunk's text and the final finishReason/usage.3// Run: node --env-file=.env --experimental-strip-types examples/gemini/streaming/stream.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");11let n = 0;12for await (const chunk of await ai.models.generateContentStream({ model: MODEL, contents: "Count from 1 to 12 separated by spaces.", config: { maxOutputTokens: 32 } })) {13  n++;14  console.log(JSON.stringify(chunk.text), chunk.candidates?.[0]?.finishReason, chunk.usageMetadata?.candidatesTokenCount);15}16log("POST", "/v1beta/models/{model}:streamGenerateContent", 200, 0.00007, `example streaming/stream.ts (${n} chunks)`);17if (n < 2) throw new Error("expected several chunks");18