Appearance
@google/genai(Gemini 原生)接入
bash
npm install @google/genai纯文本对话
javascript
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: "sk-xxxxxxxx", httpOptions: { baseUrl: "https://iliu.ai" } });
const resp = await ai.models.generateContent({ model: "gemini-3-pro-preview", contents: "你好" });
console.log(resp.text);多模态识图
javascript
import { GoogleGenAI } from "@google/genai";
import fs from "node:fs";
const ai = new GoogleGenAI({ apiKey: "sk-xxxxxxxx", httpOptions: { baseUrl: "https://iliu.ai" } });
const imageBytes = fs.readFileSync("photo.jpg");
const resp = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: [
{ role: "user", parts: [{ inlineData: { mimeType: "image/jpeg", data: imageBytes.toString("base64") } }, { text: "这张图里有什么?" }] }
]
});
console.log(resp.text);文生图(Nano Banana)
💡 建议用非流式
generateContent(对应POST /v1beta/models/{model}:generateContent):一次调用即同步返回完整图片。generateContentStream/:streamGenerateContent也能拿到图,但结果分散在流事件里需额外处理,通常不必要。
javascript
import { GoogleGenAI } from "@google/genai";
import fs from "node:fs";
const ai = new GoogleGenAI({ apiKey: "sk-xxxxxxxx", httpOptions: { baseUrl: "https://iliu.ai" } });
const resp = await ai.models.generateContent({
model: "gemini-3-pro-image-preview",
contents: "画一只戴宇航头盔的柴犬,卡通风格",
});
// 图片以 Base64 inlineData 返回,遍历 parts 取出
for (const part of resp.candidates[0].content.parts) {
if (part.inlineData) fs.writeFileSync("out.png", Buffer.from(part.inlineData.data, "base64"));
}配置
- Base URL:
https://iliu.ai(注意不加/v1) - API Key:
sk-xxxxxxxx - 心流仅支持
inlineDataBase64,不支持fileUri
相关链接
- 完整的 Gemini 原生接口文档:见「语言大模型 → Google」
- OpenAI 兼容协议:Gemini 也可走
/v1/chat/completions(见「OpenAI Python SDK 接入」)
