Gemini 协议

原生支持 Google Vertex AI / Gemini API 协议。可直接使用 @google/genai SDK、Vertex SDK 或任何 Gemini 兼容客户端,请求体一字不动透传到上游。

端点

POST https://api.dsesnet.com/v1/models/{model}:generateContent
POST https://api.dsesnet.com/v1/models/{model}:streamGenerateContent?alt=sse

注意 model 写在 URL 路径里(不是 body)。:generateContent 是非流式,:streamGenerateContent?alt=sse 是 SSE 流式。

请求示例

{
  "contents": [
    {
      "role": "user",
      "parts": [{"text": "What is the capital of France?"}]
    }
  ],
  "generationConfig": {
    "maxOutputTokens": 100,
    "temperature": 0.7
  }
}

必需的 HTTP 头

Header说明
AuthorizationBearer gw-your-key网关 API Key
Content-Typeapplication/json请求体格式

请求体字段

字段类型必填说明
contentsarray对话内容数组,每条含 roleparts
generationConfigobject生成参数:maxOutputTokenstemperaturetopPtopK
systemInstructionobject系统指令(顶层字段,不在 contents 中)
toolsarray函数调用定义
safetySettingsarray安全过滤配置

响应示例

{
  "candidates": [
    {
      "content": {
        "role": "model",
        "parts": [{"text": "The capital of France is Paris."}]
      },
      "finishReason": "STOP"
    }
  ],
  "usageMetadata": {
    "promptTokenCount": 8,
    "candidatesTokenCount": 7,
    "thoughtsTokenCount": 0,
    "totalTokenCount": 15
  },
  "modelVersion": "gemini-2.5-flash"
}

响应中的 usageMetadata.thoughtsTokenCount 是思考 token 数(如使用 thinking 模型)。计费时 completion_tokens = candidatesTokenCount + thoughtsTokenCount

使用 curl

curl -X POST "https://api.dsesnet.com/v1/models/gemini-2.5-pro:generateContent" \
  -H "Authorization: Bearer gw-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role":"user","parts":[{"text":"Hello!"}]}],
    "generationConfig": {"maxOutputTokens": 100}
  }'

流式响应

把 URL 中的 :generateContent 改成 :streamGenerateContent?alt=sse,响应为 SSE 事件流:

curl -X POST "https://api.dsesnet.com/v1/models/gemini-2.5-pro:streamGenerateContent?alt=sse" \
  -H "Authorization: Bearer gw-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role":"user","parts":[{"text":"Tell me a story"}]}],
    "generationConfig": {"maxOutputTokens": 200}
  }'

使用 @google/genai SDK

import { GoogleGenAI } from '@google/genai'

// 指向网关,使用网关 Key 即可
const ai = new GoogleGenAI({
  apiKey: 'gw-your-key-here',
  httpOptions: {
    baseUrl: 'https://api.dsesnet.com',
  },
})

const response = await ai.models.generateContent({
  model: 'gemini-2.5-pro',
  contents: 'Hello!',
})

console.log(response.text)

系统指令

Vertex 协议把系统指令放在顶层 systemInstruction,不像 OpenAI 放在 messages 数组里:

{
  "systemInstruction": {
    "parts": [{"text": "You are a helpful assistant. Reply in Chinese."}]
  },
  "contents": [
    {"role": "user", "parts": [{"text": "What is 2+2?"}]}
  ],
  "generationConfig": {"maxOutputTokens": 100}
}

支持的模型

当前网关已接入 Vertex 协议的模型:

  • gemini-2.5-pro
  • gemini-2.5-flash
  • gemini-2.5-flash-lite
  • gemini-2.5-flash-image
  • gemini-3.0-flash-preview
  • gemini-3.0-pro-image-preview
  • gemini-3.1-flash-image-preview
  • gemini-3.1-flash-lite-preview
  • gemini-3.1-pro-preview
  • gemini-3.5-flash

完整列表和价格见 模型列表 页面。

三种协议对比

维度OpenAI ChatAnthropic MessagesVertex / Gemini
端点/v1/chat/completions/v1/messages/v1/models/{model}:generateContent
model 字段位置body.modelbody.modelURL 路径
流式开启stream: truestream: trueURL 改为 :streamGenerateContent?alt=sse
系统指令messages 数组首条顶层 system顶层 systemInstruction
消息根字段messages[]messages[]contents[] + parts[]
Token 字段prompt_tokens / completion_tokensinput_tokens / output_tokenspromptTokenCount / candidatesTokenCount + thoughtsTokenCount

三种协议共享同一套 API Key、余额和计费系统。选择与你的 SDK 匹配的协议即可。

说明

  • 请求体原样透传到上游,网关只解析响应里的 usageMetadata 提取 token 数用于计费,不修改请求内容。
  • 暂不支持 Google 真正的 aiplatform.googleapis.com 端点(GCP OAuth 流程不同,未来可加)。
  • 流式响应中只有最后一个 chunk 包含完整 usageMetadata(这是 Vertex 协议标准行为,不要在中间 chunk 期望取到 token 数)。