Gemini SDK
只要设置两样东西,Google 的 Gen AI SDK 就能配合网关使用:把网关地址设为 Base URL,把 API 版本设为 v1beta。它使用 Google 自己的格式,由 Gemini 模型提供服务。
pip install google-genainpm install @google/genai把客户端指向网关
Section titled “把客户端指向网关”import osfrom google import genaifrom google.genai import types
client = genai.Client( api_key=os.environ["EXOTIC_API_KEY"], http_options=types.HttpOptions( base_url="https://api.exoticrouter.com", api_version="v1beta", ),)
response = client.models.generate_content( model="gemini-4-neon", contents="Say hello in five words.",)print(response.text)import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.EXOTIC_API_KEY, apiVersion: "v1beta", httpOptions: { baseUrl: "https://api.exoticrouter.com" },});
const response = await ai.models.generateContent({ model: "gemini-4-neon", contents: "Say hello in five words.",});console.log(response.text);- Base URL 是不带路径的网关地址。API 版本和其余路径由 SDK 自己拼接。
- 密钥通过
x-goog-api-key发送,网关会从这里读取。
这个 API 提供的是 Gemini 模型。按 Google API 的习惯,只写模型名即可:gemini-4-neon。返回里的 modelVersion 是模型的完整 ID google/gemini-4-neon。
其他模型(Claude、GPT、DeepSeek 等)请使用 OpenAI SDK:所有文本模型都支持 Chat Completions,包括 Gemini 模型。
可以使用的调用
Section titled “可以使用的调用”| SDK 调用 | 是否提供 |
|---|---|
models.generate_content / models.generateContent |
是 |
models.generate_content_stream / models.generateContentStream |
是 |
models.count_tokens / models.countTokens |
是,由网关估算,免费 |
models.embed_content |
否 |
models.list |
否;请读取网关自己的模型列表 |
文件、缓存、微调、实时会话 |
否 |
请求体会按 SDK 构造的样子原样传给模型:系统指令、工具、思考和生成配置。
for chunk in client.models.generate_content_stream( model="gemini-4-neon", contents="Write a haiku about glass.",): print(chunk.text, end="", flush=True)const stream = await ai.models.generateContentStream({ model: "gemini-4-neon", contents: "Write a haiku about glass.",});for await (const chunk of stream) { process.stdout.write(chunk.text ?? "");}读取环境变量的工具
Section titled “读取环境变量的工具”Gemini CLI 以及其他基于 Google SDK 的工具会从环境变量里读取地址和密钥。完整的设置方法见 Gemini CLI。
export GOOGLE_GEMINI_BASE_URL="https://api.exoticrouter.com"export GEMINI_API_KEY="sk-er-..."$env:GOOGLE_GEMINI_BASE_URL = "https://api.exoticrouter.com"$env:GEMINI_API_KEY = "sk-er-..."