外观
Google GenAI SDK
使用 Gemini 原生格式时,可以直接使用 Google 官方的 GenAI SDK,只需把 base_url 指向本站。令牌需要属于支持 Gemini 的分组:
支持 Gemini 的分组
安装与初始化
python
# pip install -U google-genai
from google import genai
from google.genai import types
client = genai.Client(
api_key="sk-xxxxxxxx",
http_options=types.HttpOptions(base_url="https://vip.zen-ai.top"),
)js
// npm install @google/genai
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
apiKey: process.env.VIP_API_KEY,
httpOptions: { baseUrl: "https://vip.zen-ai.top" },
});base_url 不要带 /v1beta,SDK 会自动拼接。
基础对话
python
resp = client.models.generate_content(
model="gemini-3.5-flash",
contents="用三句话介绍一下长城",
)
print(resp.text)js
const resp = await ai.models.generateContent({
model: "gemini-3.5-flash",
contents: "用三句话介绍一下长城",
});
console.log(resp.text);流式输出
python
for chunk in client.models.generate_content_stream(
model="gemini-3.5-flash",
contents="写一首关于大海的现代诗",
):
print(chunk.text or "", end="", flush=True)js
const stream = await ai.models.generateContentStream({
model: "gemini-3.5-flash",
contents: "写一首关于大海的现代诗",
});
for await (const chunk of stream) {
process.stdout.write(chunk.text ?? "");
}系统指令与思考配置
python
resp = client.models.generate_content(
model="gemini-3.5-flash",
contents="一个水池有两个进水管,单开 A 管 6 小时注满,单开 B 管 3 小时注满,同时开需要多久?",
config=types.GenerateContentConfig(
system_instruction="你是一位数学老师,回答要给出步骤。",
thinking_config=types.ThinkingConfig(include_thoughts=True),
),
)
for part in resp.candidates[0].content.parts:
if part.thought:
print("【思考摘要】", part.text)
elif part.text:
print("【回答】", part.text)Gemini 3 系列可通过 thinking_level(low / high)控制思考深度,Gemini 2.5 系列使用 thinking_budget(token 数,0 为关闭),详见 思考与推理参数。
图片生成
python
resp = client.models.generate_content(
model="gemini-3.1-flash-image-preview",
contents="一只在樱花树下看书的橘猫,水彩画风格",
config=types.GenerateContentConfig(
response_modalities=["TEXT", "IMAGE"],
image_config=types.ImageConfig(aspect_ratio="16:9"),
),
)
for part in resp.candidates[0].content.parts:
if part.inline_data:
with open("cat.png", "wb") as f:
f.write(part.inline_data.data)
elif part.text:
print(part.text)更多参数见 Gemini 原生格式 → 图片生成与编辑。