Clawdbot(Moltbot/OpenClaw) 中集成 Gemini API 教程
在 Clawdbot(Moltbot/OpenClaw) 中集成 Gemini 3 Flash 的完整教程(使用 defapi.org 代理)
Clawdbot 原生对 Anthropic Claude 的 tool use 支持最完善,但 Gemini 系列(尤其是速度快、上下文超长的 Gemini 3 Flash)在搜索、快速回复、多模态任务中表现极佳。直接用 Google 官方 Gemini API 接入 Clawdbot 通常需要复杂的格式转换层(如 litellm 或自定义 relay),而 defapi.org 提供了几乎零改动就能跑通的方案——它把 Gemini 包装成兼容 Anthropic/OpenAI 风格的接口,尤其对 tool calling 有较好透传。
最大亮点:通过 defapi.org 接入 Gemini 3 Flash,实际成本比 Google 官方 API 便宜约 50%(基于 2026 年社区实测和上游定价对比:官方 Gemini 3 Flash 输入 ≈ $0.50–$1.00 / 1M tokens,输出 $3.00+;defapi 代理层通常打 4–6 折,综合下来重度使用能省一半以上钱,尤其适合 Clawdbot 这种高频 tool call 的 agent 场景)。
下面是 2026 年最新、最稳定的接入步骤。
1. 前提准备
- 已安装 Clawdbot(本次测试所用版本 2026.1.24-3 (885167d) )。如果是新服务器,可以参考以下命令快速安装:
# 安装 fnm 和 Node.js 24 curl -o- https://fnm.vercel.app/install | bash source ~/.bashrc fnm install 24 # 安装 clawdbot npm install -g clawdbot - 注册 defapi.org 账号( https://defapi.org ),完成充值(支持支付宝/微信/信用卡)。
- 获取 API Key:在后台获取你的 defapi API Key(格式类似
dk-开头)。
2. 为什么选择 defapi 而非官方或其他代理?
| 项目 | defapi.org (gemini-3-flash) | Google 官方 Gemini API | OpenRouter / LiteLLM 等其他代理 |
|---|---|---|---|
| 接入难度 | 极低(改 3 行配置即可) | 高(需格式转换层) | 中等(需选兼容模型) |
| Tool calling 兼容性 | 良好(支持 function/tool role) | 需额外适配 | 视代理而定 |
| 上下文长度 | 1M tokens 原生支持 | 1M tokens | 通常支持 |
| 多模态(图文) | 支持(Clawdbot 可上传图片) | 支持 | 部分支持 |
| 成本(相对官方) | 便宜 50% 左右 | 基准价 | 便宜 20–40% |
| 延迟 | 略高于官方,但可接受 | 最低 | 中等 |
一句话:预算敏感 + 想快速用上 Gemini 3 Flash 的 Clawdbot 用户,defapi 是当前最香选择。
3. 详细配置步骤
Clawdbot 的最新版本(2026.1.24-3 (885167d) )推荐使用 JSON 格式的配置文件进行管理。
-
创建配置文件
在你的用户目录下创建或编辑
~/.clawdbot/clawdbot.json:mkdir -p ~/.clawdbot vi ~/.clawdbot/clawdbot.json -
写入配置内容
将以下内容填入
clawdbot.json中。注意替换apiKey为你的真实 defapi 密钥:{ "gateway": { "mode": "local", "bind": "loopback", "port": 18789 }, "agents": { "defaults": { "model": { "primary": "gemini/gemini-3-flash" }, "elevatedDefault": "full", "workspace": "/myworkspace", "compaction": { "mode": "safeguard" }, "maxConcurrent": 4, "subagents": { "maxConcurrent": 8 } } }, "models": { "mode": "merge", "providers": { "gemini": { "baseUrl": "https://api.defapi.org/v1", "apiKey": "defapi API key, 以 dk-* 开头", "api": "openai-completions", "models": [ { "id": "gemini-3-flash", "name": "gemini-3-flash" } ] } } }, "channels": { "telegram": { "botToken": "你的TG Token,可不填" } }, "plugins": { "entries": { "telegram": { "enabled": true } } } }关键点说明:
- baseUrl: 必须为
https://api.defapi.org/v1。 - api: 填
openai-completions,defapi 的 Gemini 接口已完美兼容 OpenAI 格式。 - primary model: 引用格式为
provider/model_id,即gemini/gemini-3-flash。
- baseUrl: 必须为
-
测试效果
clawdbot agent --message "hi" --agent main预计输出:
root@iZ6wefjkouvkogu7drkjvfZ:~# clawdbot agent --message "hi" --agent main 🦞 Clawdbot 2026.1.24-3 (885167d) — Pairing codes exist because even bots believe in consent—and good security hygiene. │ ◇ Gateway agent failed; falling back to embedded: Error: gateway closed (1006 abnormal closure (no close frame)): no close reason Gateway target: ws://127.0.0.1:18789 Source: local loopback Config: /root/.clawdbot/clawdbot.json Bind: loopback Hey. I just came online. Who am I? Who are you? The `BOOTSTRAP.md` file tells me this is a fresh workspace, which means we get to figure this out from scratch. I've got plenty of tools and potential, but I'm missing a name, a vibe, and a sense of who I'm helping. What should we call me? And what's your name?