Gemini 兼容接口
用官方 google-genai SDK 调用 Asale 网关的 generateContent。改掉 base URL 和密钥,请求体原样不动。
Gemini 协议是 google-genai 和 gemini-cli 说的那一种。与另外两种不同,它把模型名和流式开关都放在路径里而不是请求体里——手写对接时最容易踩的就是这一点。
密钥、模型、计费和错误码请先看总览;本页只讲 Gemini 特有的部分。
连接参数
| Base URL | https://gw.asale.ai |
| 鉴权头 | x-goog-api-key: sk-asale-... |
| SDK 环境变量 | GEMINI_API_KEY、GOOGLE_GEMINI_BASE_URL |
base URL 只写到域名。Google 客户端会自己拼 /v1beta/models/…,填 https://gw.asale.ai/v1beta 会让路径重复而 404。gemini-cli 读取的 GOOGLE_GEMINI_BASE_URL 同样只要域名。
Authorization: Bearer 和 x-api-key 也接受。放在 ?key= 查询串里的密钥不接受,请放进请求头。
接口清单
| 方法 | 路径 | 说明 |
|---|---|---|
| POST | /v1beta/models/{model}:generateContent | 主接口。 |
| POST | /v1beta/models/{model}:streamGenerateContent | 流式——方法名本身就是开关。 |
| GET | /v1beta/models | 平台此刻能匹配的模型。 |
调用
curl "https://gw.asale.ai/v1beta/models/gemini-2.5-pro:generateContent" \
-H "x-goog-api-key: $ASALE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"role": "user", "parts": [{"text": "Hello"}]}]
}'
# pip install google-genai
import os
from google import genai
from google.genai import types
client = genai.Client(
api_key=os.environ["ASALE_API_KEY"],
http_options=types.HttpOptions(base_url="https://gw.asale.ai"),
)
resp = client.models.generate_content(
model="gemini-2.5-pro",
contents="Hello",
)
print(resp.text)
// npm i @google/genai
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({
apiKey: process.env.ASALE_API_KEY,
httpOptions: { baseUrl: "https://gw.asale.ai" },
});
const resp = await ai.models.generateContent({
model: "gemini-2.5-pro",
contents: "Hello",
});
console.log(resp.text);
流式
把 :generateContent 换成 :streamGenerateContent。方法后缀就是流式开关——Gemini 的请求体里没有 stream 字段——响应是携带 GenerateContentResponse 分片的 SSE。
for chunk in client.models.generate_content_stream(
model="gemini-2.5-pro",
contents="数到五",
):
print(chunk.text, end="", flush=True)
生成参数与工具
generationConfig——maxOutputTokens、temperature、topP、stopSequences以及思考预算都会透传。systemInstruction作为系统提示透传。tools/functionCall/functionResponse按原生结构透传。
实用提示
- 路径里的模型名优先。若路径和请求体写了不同的模型,以路径为准——那才是你实际请求的地址。
- 请设置
maxOutputTokens。余额冻结额度按它计算,响应结束后差额退回。 - 模型名是平台的,不是 Google 的型号表。用
GET /v1beta/models取清单,市场可以看哪些有供给。 - 请求体上限 16 MiB。