跳转到内容
exoticrouter.com ↗

Gemini

Google 的 generateContent 格式,适用于 Gemini 模型。模型写在 URL 里,密钥放在 Google 客户端惯用的位置,请求体会原样传给模型。

POST https://api.exoticrouter.com/v1beta/models/{model}:generateContent

POST https://api.exoticrouter.com/v1beta/models/{model}:streamGenerateContent

cURL
curl "https://api.exoticrouter.com/v1beta/models/google/gemini-4-neon:generateContent" \
-H "x-goog-api-key: $EXOTIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "Why is the sky blue?"}]}]
}'
Python
import os
from google import genai
from 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="Why is the sky blue?",
)
print(response.text)
TypeScript
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: "Why is the sky blue?",
});
console.log(response.text);
  • 模型是 URL 里 models/ 和冒号之间的部分。可以写完整 ID(带斜杠,如 google/gemini-4-neon),也可以只写模型名 gemini-4-neon。请求体里的 model 字段不会被读取。
  • 必须是支持这个接口的模型,也就是 Gemini 模型。其他模型请使用 Chat Completions。
  • 密钥从 x-goog-api-key 读取(Google 的 SDK 发送的就是它),也可以放在 URL 的 ?key= 里。
  • 请求体会原样传给模型:contents、systemInstruction、tools、generationConfig 等。
{
"candidates": [
{
"content": {
"role": "model",
"parts": [{ "text": "Sunlight is scattered by the air, and blue light is scattered the most." }]
},
"finishReason": "STOP"
}
],
"usageMetadata": { "promptTokenCount": 7, "candidatesTokenCount": 16, "totalTokenCount": 23 },
"modelVersion": "google/gemini-4-neon"
}

返回的是 Google 自己的响应格式。modelVersion 是模型的完整 ID。usageMetadata 是模型自己的统计;计费用的输入 token 数是网关自己的计数,可能与它不同。思考 token 按输出计费。

调用 :streamGenerateContent 可以边生成边返回。加上 ?alt=sse 会得到 server-sent events,每个 data: 行是一个响应数据块。不加的话,返回的是一个逐段到达的 JSON 数组。

cURL
curl -N "https://api.exoticrouter.com/v1beta/models/google/gemini-4-neon:streamGenerateContent?alt=sse" \
-H "x-goog-api-key: $EXOTIC_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "Write a haiku about glass."}]}]
}'
Python
for chunk in client.models.generate_content_stream(
model="gemini-4-neon",
contents="Write a haiku about glass.",
):
print(chunk.text, end="", flush=True)
TypeScript
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 ?? "");
}

建议使用 alt=sse。如果模型在流开始之后出错,SSE 流会以一个 error 事件结束;而 JSON 数组只会提前中断,唯一的迹象是它没有正常闭合。

错误采用 Google 的格式:

{
"error": {
"code": 429,
"message": "Rate limit exceeded for this API key. Please slow down.",
"status": "RESOURCE_EXHAUSTED"
}
}

status 与 HTTP 状态码对应:401 是 UNAUTHENTICATED,402 是 FAILED_PRECONDITION,404 是 NOT_FOUND,429 是 RESOURCE_EXHAUSTED,503 是 UNAVAILABLE。完整列表见错误。

  • Gemini SDK:把 Google 的 Gen AI 客户端指向网关。
  • Gemini CLI:使用这种格式的编程智能体。