/gemini/v1beta/models/{model}:streamGenerateContentGemini 流式内容生成。请求体格式与 generateContent 相同,响应以流式 JSON 逐块返回。
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| contents | object[] | 必填 | 对话内容数组,格式同 generateContent。 |
| generationConfig | object | 可选 | 生成配置,格式同 generateContent。 |
import { GoogleGenAI } from "@google/genai"
// SDK 默认把 apiKey 发在 x-goog-api-key(网关只读 Authorization),
// 必须用 httpOptions.headers 显式补 Authorization: Bearer <key>。
const ai = new GoogleGenAI({
apiKey: "gw_memene_<keyId>_<secret>",
httpOptions: {
baseUrl: "https://llmapi.memene.cn/gemini",
headers: { Authorization: "Bearer gw_memene_<keyId>_<secret>" },
},
})
const stream = await ai.models.generateContentStream({
model: "gemini-2.5-flash",
contents: "Write a short poem about the sea.",
config: { maxOutputTokens: 256 },
})
for await (const chunk of stream) {
process.stdout.write(chunk.text ?? "")
}{
"candidates": [
{
"content": {
"parts": [
{
"text": "Waves crash upon the shore,\n"
}
],
"role": "model"
}
}
]
}注意:流式响应由多个 JSON 对象组成,每个对象包含部分生成内容。查询参数 `alt=sse` 会自动附加。