背景
- NotebookLM 播客太惊艳了,一直想集成到 zenfeed,来一波 RSS+播客 (已实现【RSS + 播客】基于 L 站热帖生成播客)
- 但所需的 TTS 价格居高不下,当时还发帖问了波 有无便宜的大模型语音合成 API ,遂搁置
- 天无绝人之路,大善人推出 gemini-flash-tts,音质还行,主要可以嫖免费额度 Gemini TTS 的速率限制是多少
- 但问题又来了,一直用的 Gemini Balance 居然只支持单人语音(貌似是 OpenAI 格式,非 gemini 多人格式),结合自己另外的一些小需求,干脆自己撸了一个…(ps 如果这里以及下面提到的差异不是你的痛点,更推荐坛里的 gb)
介绍
- !!!降低封禁风险!!!: 通过 Cloudflare AI Gateway 路由请求,有效降低 API 密钥(尤其是 Gemini)被封禁的概率。大家用 cf ai gateway 能减少 gemini key G 掉的概率吗
- !!!智能的错误处理(同时你只管加 KEY,不需要担心存量 KEY 的死活)!!!:
- 模型级限流: 精准识别并暂时屏蔽达到速率限制的特定模型。特别地,针对 Google AI Studio,能智能区分分钟级和天级配额,进行差异化冷却(例如,触发天级配额后冷却 24 小时)。冷却后自动换 key 重试
- 自动熔断: 永久禁用被提供商封禁(
403错误)的密钥,减少无效重试。
- 广泛的兼容性: 支持 Cloudflare AI Gateway 兼容的所有 API 提供商,不止轮询 Gemini,轮询 OpenAI 也是支持的
部署
CF + 部署脚本,就很快
0. 检查环境
1. 创建 AI Gateway
创建一个新的 AI Gateway,并将其命名为 one-balance
2. 一键部署!
git clone https://github.com/glidea/one-balance.git
cd one-balance
pnpm install
# Mac/Linux
AUTH_KEY=your-super-secret-auth-key pnpm run deploycf
# Windows (PowerShell)
$env:AUTH_KEY = "your-super-secret-auth-key"; pnpm run deploycf
脚本将引导你登录 wrangler (如果尚未登录),自动创建所需的 D1 数据库(AI Gateway 不支持通过wrangler创建所以得手动),并部署 Worker。部署成功后,会得到一个 Worker 的 URL,例如 https://one-balance.<your-subdomain>.workers.dev
使用
1. 配置待轮询 KEYS
访问 https://<your-worker-url>
最佳实践:
尽量避免和他人共享 Key,这样系统无法感知全局的调用信息,可能会增加 429 概率
2. 访问 API
curl "https://<your-worker-url>/api/compat/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-super-secret-auth-key" \
-d '{
"model": "google-ai-studio/gemini-2.5-pro",
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}'
注意model需要加provider前缀,详细参见:
- Provider Native · Cloudflare AI Gateway docs
- Unified API (OpenAI compat) · Cloudflare AI Gateway docs
以上方式在流式回复时,存在中文乱码的问题(AI Gateway 侧),通过调用具体 Provider 接口绕过,如 google-ai-studio
curl "https://<your-worker-url>/api/google-ai-studio/v1/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
-H 'content-type: application/json' \
-H 'x-goog-api-key: your-super-secret-auth-key' \
-d '{
"contents": [
{
"role":"user",
"parts": [
{"text":"你是谁?"}
]
}
]
}'
其它 provider 参考:Provider Native · Cloudflare AI Gateway docs
Cherry Studio
Gemini 格式










