兼容 Gemini

可用

在已配置的情况下,使用兼容 Gemini 的模型、生成、流式传输和嵌入路由。

GET /v1beta/models

列出当前 API Key 可见的兼容 Gemini 模型。

Gemini 模型列表
curl https://www.token168.cc/v1beta/models \
  -H "Authorization: Bearer $TOKEN168_API_KEY"

POST /v1beta/models/{model}:generateContent

generateContent 接受 Gemini 原生的 content、safety、tool、system instruction、cache 和 generationConfig 字段。示例使用所选的最新模型文档示例 gemini-3.1-pro。

Gemini generateContent 生成
curl https://www.token168.cc/v1beta/models/gemini-3.1-pro:generateContent \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "Summarize the release notes.", "thought": false}]}],
    "safetySettings": [{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}],
    "generationConfig": {
      "temperature": 0.4,
      "topP": 0.9,
      "topK": 40,
      "maxOutputTokens": 800,
      "candidateCount": 1,
      "stopSequences": ["END"],
      "responseMimeType": "application/json",
      "responseSchema": {"type": "object", "properties": {"summary": {"type": "string"}}},
      "responseJsonSchema": {"type": "object"},
      "presencePenalty": 0,
      "frequencyPenalty": 0,
      "responseLogprobs": false,
      "logprobs": 0,
      "enableEnhancedCivicAnswers": false,
      "mediaResolution": "MEDIA_RESOLUTION_MEDIUM",
      "seed": 123,
      "responseModalities": ["TEXT"],
      "thinkingConfig": {"includeThoughts": true, "thinkingBudget": 1024, "thinkingLevel": "medium"},
      "speechConfig": {"voiceConfig": {"prebuiltVoiceConfig": {"voiceName": "Kore"}}},
      "imageConfig": {"aspectRatio": "1:1"}
    },
    "tools": [{"functionDeclarations": [{"name": "lookup", "parameters": {"type": "object", "properties": {}}}]}],
    "toolConfig": {"functionCallingConfig": {"mode": "AUTO", "allowedFunctionNames": ["lookup"]}, "retrievalConfig": {"languageCode": "en"}, "includeServerSideToolInvocations": false},
    "systemInstruction": {"role": "system", "parts": [{"text": "Return concise JSON."}]},
    "cachedContent": "cachedContents/example"
  }'

POST /v1beta/models/{model}:streamGenerateContent

流式传输使用相同的请求体,并配合流式 action 或 alt=sse 查询参数。

Gemini 流式传输
curl "https://www.token168.cc/v1beta/models/gemini-3.1-pro:streamGenerateContent?alt=sse" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "Stream one paragraph."}]}],
    "generationConfig": {"temperature": 0.4, "topP": 0.9, "topK": 40, "maxOutputTokens": 400, "candidateCount": 1}
  }'

POST /v1beta/models/{model}:embedContent

在已配置的情况下,嵌入类 Gemini 路径通过 Gemini 转发格式路由。

Gemini 嵌入
curl https://www.token168.cc/v1beta/models/gemini-3.1-pro:embedContent \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN168_API_KEY" \
  -d '{
    "content": {"parts": [{"text": "Token168 docs"}]},
    "taskType": "RETRIEVAL_DOCUMENT",
    "title": "Docs"
  }'