Gemini 接口

最后更新

Gemini 兼容接口沿用 Google Gemini API 的 REST 路径和请求体,可以用 Google GenAI SDK 或直接发 HTTP 请求调用目录里的模型。

接入

  • REST 地址前缀是 https://modelportal.ai/v1beta;Google GenAI SDK 的 base_url 填 https://modelportal.ai,SDK 会自己拼 /v1beta。
  • 密钥放在 x-goog-api-key 请求头、查询参数 key 或 Authorization: Bearer 中任意一处;带了几处时必须相同。
  • 模型 ID 写在路径里,斜杠可以直接写,也可以写成 %2F。
curl "https://modelportal.ai/v1beta/models/MODEL_ID:generateContent" \
  -H "x-goog-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"contents": [{"role": "user", "parts": [{"text": "你好"}]}]}'
from google import genai
from google.genai import types

client = genai.Client(
    api_key="YOUR_API_KEY",
    http_options=types.HttpOptions(base_url="https://modelportal.ai"),
)
reply = client.models.generate_content(model="MODEL_ID", contents="你好")
print(reply.text)

与 OpenAI 格式不同的地方

  • 流式用 :streamGenerateContent?alt=sse;不带 alt=sse 时返回 JSON 数组。
  • generation_config、system_instruction 以及其中的 max_output_tokens、candidate_count 会换成驼峰写法;同一字段两种写法同时出现时拒绝。
  • 不填 maxOutputTokens 时,平台按模型的输出上限和你的可用余额补一个值。
  • 路由偏好用请求头 X-ModelPortal-Provider-Preferences 传,见供应商选择。
  • 错误体是 {"error": {"code", "message", "status"}},status 只由 HTTP 状态决定。

接口与字段见 Gemini 兼容接口参考。