Zeroclaw 接入 Defapi 的低成本 Gemini & Claude API

AI Tech Scout

本教程将指导您如何在 Zeroclaw 中通过 Defapi 平台接入各种大型语言模型。使用 Defapi,您可以以极具性价比的成本,获得完全兼容官方接口的体验。

简介

Zeroclaw 是一个基于 Rust 的自主 AI Agent 运行时环境,支持通过 Telegram、Discord、Slack 等渠道与用户互动。而 Defapi 则是一个提供高性价比模型 API 的平台,其价格仅为官方价格的一半,支持全球稳定访问,并全面兼容各大模型厂商的 API 协议(如 v1/chat/completionsv1/messagesv1beta/models/ 等)。

具体操作

通过 Defapi 接入模型非常简便,以下是具体的配置步骤。

1. 获取 API Key

首先,访问 Defapi 官网并注册账号。在您需要的模型页面(例如 Claude 或 Gemini 模型)获取专属的 API Key。
例如:Defapi Claude 页面Defapi Gemini 页面

2. 配置环境变量

在系统的终端或环境中,配置对应的环境变量,填入您的 API Key:

针对 Claude 模型:

export ANTHROPIC_API_KEY="dk-xxxxxxxxxxxxxxxx"

针对 Gemini 模型:

export GEMINI_API_KEY="dk-xxxxxxxxxxxxxxxx"

3. 修改配置文件

编辑 Zeroclaw 的主配置文件(通常位于 ~/.zeroclaw/config.toml),根据所选模型调整配置。

使用 Claude 模型(以 Claude Sonnet 4.5 为例):

default_provider = "anthropic-custom:https://api.defapi.org"
api_key = "dk-xxxxxxxxxxxxxxxx"
default_model = "anthropic/claude-sonnet-4.5"
default_temperature = 0.7

使用 Gemini 模型(以 Gemini 3 Flash 为例):

default_provider = "custom:https://api.defapi.org"
api_key = "dk-xxxxxxxxxxxxxxxx"
default_model = "gemini-3-flash"
default_temperature = 0.7

提示:Defapi 的接口与 Anthropic 官方的 v1/messages 接口及 Google 的 v1beta/models/*:generateContent 接口完美兼容,您可以无缝切换而无需修改任何底层代码。

验证 Zeroclaw 是否正常工作

配置完成后,您可以通过以下方式验证 Zeroclaw 是否成功接入了 Defapi。

方法一:直接发消息测试

在终端中启动 Zeroclaw,并发送一条测试消息:

zeroclaw agent -m "Hello, can you hear me?"

如果模型能够正常且迅速地返回回复,则说明配置已成功。

方法二:使用诊断命令

Zeroclaw 内置了诊断命令供您检查运行状态:

# 查看系统状态
zeroclaw status

# 运行系统诊断
zeroclaw doctor

# 列出所有可用的提供商和模型列表
zeroclaw providers

方法三:配置通道测试

您可以配置社交渠道(如 Telegram)进行真实的交互测试:

[channels_config.telegram]
bot_token = "123456:your-telegram-token"
allowed_users = ["*"]

5 种常见的用例

结合 Defapi 的低成本优势,Zeroclaw 可以广泛应用于以下场景:

用例一:代码审查助手

自动审查代码库,指出潜在的错误、安全漏洞及优化建议。

[agents.code_reviewer]
provider = "anthropic-custom:https://api.defapi.org"
model = "anthropic/claude-sonnet-4.5"
system_prompt = "You are an expert code reviewer. Review the code for bugs, security issues, and best practices."
agentic = false
max_iterations = 1

用例二:智能客服机器人

全天候为用户提供快速的问答支持与友好回复。

[agents.support]
provider = "anthropic-custom:https://api.defapi.org"
model = "anthropic/claude-haiku-4.5"
system_prompt = "You are a helpful customer support agent. Be concise and friendly."
max_iterations = 3

用例三:自动化研究助手

能够自主搜索网络并整理总结结果,适合深度调研与信息收集。

[agents.researcher]
provider = "custom:https://api.defapi.org"
model = "gemini-3-flash"
system_prompt = "You are a research assistant. Search the web and summarize findings."
agentic = true
max_depth = 2
allowed_tools = ["web_search", "http_request", "file_read"]
max_iterations = 8

用例四:数据分析师

连接本地文件与 Shell 工具,帮助分析数据并提供深刻见解。

[agents.analyst]
provider = "anthropic-custom:https://api.defapi.org"
model = "anthropic/claude-sonnet-4.5"
system_prompt = "You are a data analyst. Analyze the provided data and provide insights."
agentic = true
allowed_tools = ["shell", "file_read"]

用例五:多模态内容生成

结合多种输入源(如图像、文档),快速生成具有创意的营销文案或多媒体内容。

[agents.content_creator]
provider = "custom:https://api.defapi.org"
model = "gemini-3-flash"
system_prompt = "You are a creative content creator. Generate engaging content based on user requests."
agentic = true
allowed_tools = ["web_search", "file_read"]
max_iterations = 3
Zeroclaw 接入 Defapi 的低成本 Gemini & Claude API