將 OpenClaw 連接到 Google Gemini
你是不是也遇過這種情況?好不容易寫好一個專案,結果發現 AI 的 API 費用貴得嚇人,或是對話稍微長一點,模型就開始忘東忘西。想要處理長文件或是複雜的歷史對話,成本和上下文限制總是開發者最頭痛的問題。
Google Gemini 的免費額度大方到讓人驚訝。當其他模型還在按 token 計費時,Gemini 直接給你 100 萬 token 的 context 視窗,還有非常實用的免費方案。如果你需要處理長文件或保存大量對話紀錄,Gemini 幾乎是目前的首選。
這篇教學會教你如何在 OpenClaw 中設定 Gemini。
需要準備的東西
Section titled “需要準備的東西”- Google 帳號
- 已安裝好的 OpenClaw
Quick Start (5 分鐘快速上手)
Section titled “Quick Start (5 分鐘快速上手)”第一步:取得你的 API Key
Section titled “第一步:取得你的 API Key”- 前往 Google AI Studio
- 使用你的 Google 帳號登入
- 點擊 “Get API Key” → “Create API Key”
- 複製該金鑰
第二步:設定 OpenClaw
Section titled “第二步:設定 OpenClaw”方法 A:環境變數
export GEMINI_API_KEY="your-api-key-here"方法 B:使用 CLI 設定
openclaw config set models.providers.gemini.apiKey "your-api-key-here"第三步:將 Gemini 設為你的模型
Section titled “第三步:將 Gemini 設為你的模型”編輯你的 OpenClaw 設定檔:
{ agents: { defaults: { model: { primary: "gemini/gemini-2.5-flash" } } }}設定完成,你現在可以開始使用 Gemini 了。
OAuth 驗證 (另一種選擇)
Section titled “OAuth 驗證 (另一種選擇)”如果你想要更安全、免金鑰的驗證方式,可以使用你的 Google 帳號進行 OAuth 驗證:
選項 1:透過安裝嚮導
- 執行
openclaw onboard - 選擇 “Google” 作為你的 AI 提供商
- 選擇 “OAuth Authentication”
- 在瀏覽器中完成 Google 登入流程
- OpenClaw 會安全地儲存你的驗證 token
選項 2:使用 CLI 插件驗證
如果你已經安裝了 OpenClaw,可以啟用 Gemini CLI 驗證插件:
# 啟用 Gemini CLI 驗證插件openclaw plugins enable google-gemini-cli-auth
# 進行驗證並設為預設提供商openclaw models auth login --provider google-gemini-cli --set-default這會開啟瀏覽器視窗進行 Google OAuth 登入。驗證後,你不需要管理 API key 就能使用 Gemini。
注意: OAuth 適合個人使用。如果是自動化任務或伺服器部署,建議使用 API key。
可用的 Gemini 模型
Section titled “可用的 Gemini 模型”| 模型 | 最適合 | Context 視窗 |
|---|---|---|
| gemini-2.5-flash | 快速回應 | 1M tokens |
| gemini-2.5-pro | 複雜推理 | 1M tokens |
| gemini-2.0-flash | 均衡表現 | 128K tokens |
模型選取範例
Section titled “模型選取範例”{ agents: { defaults: { model: { primary: "gemini/gemini-2.5-flash", fallback: ["gemini/gemini-2.0-flash"] } } }}關於 Gemini 的免費方案
Section titled “關於 Gemini 的免費方案”Google 的免費方案包含:
- 每分鐘 60 次請求 (Flash 模型)
- 每分鐘 2 次請求 (Pro 模型)
- 100 萬 token context 視窗
對大多數個人使用場景來說,免費方案已經非常夠用。
速率限制 (Rate Limits)
Section titled “速率限制 (Rate Limits)”如果你遇到速率限制,可以考慮:
- 切換到限制較寬鬆的 Flash 模型
- 在請求之間加入延遲
- 升級到 Gemini API 付費方案
完整的提供商設定
Section titled “完整的提供商設定”{ models: { providers: { gemini: { apiKey: "your-api-key", models: [ { id: "gemini-2.5-flash", name: "Gemini 2.5 Flash", reasoning: false, input: ["text", "image"], contextWindow: 1000000, maxTokens: 8192 } ] } } }}與其他提供商混合使用
Section titled “與其他提供商混合使用”你可以將 Gemini 與其他模型搭配:
{ agents: { defaults: { model: { primary: "gemini/gemini-2.5-flash", // 快速、免費 fallback: ["anthropic/claude-sonnet-4"] // 用於處理複雜任務 } } }}”API key not valid”
Section titled “”API key not valid””請再次檢查你的 API key:
- 前往 Google AI Studio
- 確認金鑰處於啟用狀態
- 確保複製時沒有多餘的空格
”Quota exceeded”
Section titled “”Quota exceeded””你已達到免費方案的限制。你可以:
- 等待配額重置 (每天重置)
- 切換到限制較高的 Flash 模型
- 升級為付費方案
”Model not found”
Section titled “”Model not found””驗證模型 ID 是否正確:
openclaw models list確保你使用的格式正確:gemini/model-name
還是搞不定? 我們的 AI Setup Assistant 學習了所有 OpenClaw 文件,可以即時幫你解決 Gemini 的設定問題。
Gemini vs 其他提供商
Section titled “Gemini vs 其他提供商”| 面向 | Gemini | Claude | OpenAI |
|---|---|---|---|
| 最適合 | 長文件、多模態 | 創意、細膩表現 | 程式碼、分析 |
| 免費方案 | ✅ 非常大方 | ❌ 無 | ❌ 無 |
| Context | 1M tokens | 200K tokens | 128K tokens |
| 寫程式 | 良好 | 極佳 | 極佳 |
我的建議: 使用 Gemini 處理文件分析,並將它當作一個節省成本的備援方案。至於需要最高輸出品質的任務,則保留給 Claude 或 GPT。
- 安裝 OpenClaw → — 完整安裝指南
- 使用 Ollama → — 免費的本地 AI
- 連接 Telegram → — 透過手機控制
- Docker 部署 → — 24/7 持續運作
需要協助嗎?加入 OpenClaw Discord 或查看 官方文件。
OpenClaw Expert
還是卡住了?
如果這篇文件沒有解決你的情境,直接問 OpenClaw Expert,拿到可執行步驟。