Gemini API 使用文档
此平台通过 OpenAI 兼容接口调用 Gemini 模型,使用 openai SDK 即可。
安装
- Python
- JavaScript
- curl
pip install openai
npm install openai
# curl 无需安装,系统自带
基础用法
- Python
- JavaScript
- curl
from openai import OpenAI
client = OpenAI(
api_key="your-api-key",
base_url="https://www.cheapertoken.work/v1",
)
response = client.chat.completions.create(
model="gemini-2.5-flash",
messages=[{"role": "user", "content": "你好,请介绍一下你自己"}],
)
print(response.choices[0].message.content)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "your-api-key",
baseURL: "https://www.cheapertoken.work/v1",
});
const response = await client.chat.completions.create({
model: "gemini-2.5-flash",
messages: [{ role: "user", content: "你好,请介绍一下你自己" }],
});
console.log(response.choices[0].message.content);
curl https://www.cheapertoken.work/v1/chat/completions \
-H "Authorization: Bearer your-api-key" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-2.5-flash",
"messages": [{"role": "user", "content": "你好,请介绍一下你自己"}]
}'
常用模型
| 模型 | 说明 |
|---|---|
gemini-2.5-flash | 速度快,适合大多数任务 |
gemini-2.5-pro | 最强能力,支持深度推理 |
gemini-3.5-flash | 新一代 Flash 模型 |