Web 工具

OpenClaw 内置了两个轻量级的网络工具:

  • web_search — 通过 Brave Search API(默认)或 Perplexity Sonar(直连或通过 OpenRouter)搜索网络。
  • web_fetch — HTTP 抓取 + 可读内容提取(HTML → markdown/文本)。

这些不是浏览器自动化工具。如果要处理 JavaScript 密集型网站或登录操作,请使用 Browser 工具

工作原理

  • web_search 调用你配置的服务商并返回结果。
    • Brave(默认):返回结构化结果(标题、URL、摘要)。
    • Perplexity:返回 AI 合成的答案,并附带实时网络搜索的引用。
  • 结果会按查询缓存 15 分钟(可配置)。
  • web_fetch 执行普通的 HTTP GET 请求并提取可读内容(HTML → markdown/文本)。它不会执行 JavaScript。
  • web_fetch 默认启用(除非明确禁用)。

选择搜索服务商

服务商优点缺点API 密钥
Brave(默认)快速、结构化结果、有免费额度传统搜索结果BRAVE_API_KEY
PerplexityAI 合成答案、引用、实时搜索需要 Perplexity 或 OpenRouter 访问权限OPENROUTER_API_KEYPERPLEXITY_API_KEY

查看 Brave Search 设置Perplexity Sonar 了解服务商的具体细节。

在配置中设置服务商:

{
  tools: {
    web: {
      search: {
        provider: "brave", // or "perplexity"
      },
    },
  },
}

示例:切换到 Perplexity Sonar(直连 API):

{
  tools: {
    web: {
      search: {
        provider: "perplexity",
        perplexity: {
          apiKey: "pplx-...",
          baseUrl: "https://api.perplexity.ai",
          model: "perplexity/sonar-pro",
        },
      },
    },
  },
}

获取 Brave API 密钥

  1. https://brave.com/search/api/ 创建 Brave Search API 账户
  2. 在控制面板中,选择 Data for Search 方案(不是 “Data for AI”)并生成 API 密钥。
  3. 运行 openclaw configure --section web 将密钥存储到配置中(推荐),或在环境变量中设置 BRAVE_API_KEY

Brave 提供免费额度和付费方案;查看 Brave API 门户了解当前的限制和价格。

密钥存储位置(推荐)

推荐方式: 运行 openclaw configure --section web。它会将密钥存储到 ~/.openclaw/openclaw.jsontools.web.search.apiKey 下。

环境变量方式: 在 Gateway 进程环境中设置 BRAVE_API_KEY。如果是 gateway 安装,将它放在 ~/.openclaw/.env(或你的服务环境)中。参见环境变量

使用 Perplexity(直连或通过 OpenRouter)

Perplexity Sonar 模型内置了网络搜索能力,会返回 AI 合成的答案并附带引用。你可以通过 OpenRouter 使用它们(无需信用卡 - 支持加密货币/预付费)。

获取 OpenRouter API 密钥

  1. https://openrouter.ai/ 创建账户
  2. 充值(支持加密货币、预付费或信用卡)
  3. 在账户设置中生成 API 密钥

设置 Perplexity 搜索

{
  tools: {
    web: {
      search: {
        enabled: true,
        provider: "perplexity",
        perplexity: {
          // API key (optional if OPENROUTER_API_KEY or PERPLEXITY_API_KEY is set)
          apiKey: "sk-or-v1-...",
          // Base URL (key-aware default if omitted)
          baseUrl: "https://openrouter.ai/api/v1",
          // Model (defaults to perplexity/sonar-pro)
          model: "perplexity/sonar-pro",
        },
      },
    },
  },
}

环境变量方式: 在 Gateway 环境中设置 OPENROUTER_API_KEYPERPLEXITY_API_KEY。如果是 gateway 安装,将它放在 ~/.openclaw/.env 中。

如果没有设置 base URL,OpenClaw 会根据 API 密钥来源选择默认值:

  • PERPLEXITY_API_KEYpplx-...https://api.perplexity.ai
  • OPENROUTER_API_KEYsk-or-...https://openrouter.ai/api/v1
  • 未知密钥格式 → OpenRouter(安全回退)

可用的 Perplexity 模型

模型描述适用场景
perplexity/sonar快速问答 + 网络搜索快速查询
perplexity/sonar-pro(默认)多步推理 + 网络搜索复杂问题
perplexity/sonar-reasoning-pro思维链分析深度研究

使用你配置的服务商搜索网络。

要求

  • tools.web.search.enabled 不能为 false(默认:启用)
  • 你选择的服务商的 API 密钥:
    • BraveBRAVE_API_KEYtools.web.search.apiKey
    • PerplexityOPENROUTER_API_KEYPERPLEXITY_API_KEYtools.web.search.perplexity.apiKey

配置

{
  tools: {
    web: {
      search: {
        enabled: true,
        apiKey: "BRAVE_API_KEY_HERE", // optional if BRAVE_API_KEY is set
        maxResults: 5,
        timeoutSeconds: 30,
        cacheTtlMinutes: 15,
      },
    },
  },
}

工具参数

  • query(必需)
  • count(1–10;默认从配置读取)
  • country(可选):2 字母国家代码,用于特定地区的结果(例如 “DE”、“US”、“ALL”)。如果省略,Brave 会选择默认地区。
  • search_lang(可选):搜索结果的 ISO 语言代码(例如 “de”、“en”、“fr”)
  • ui_lang(可选):界面元素的 ISO 语言代码
  • freshness(可选,仅 Brave):按发现时间过滤(pdpwpmpyYYYY-MM-DDtoYYYY-MM-DD

示例:

// German-specific search
await web_search({
  query: "TV online schauen",
  count: 10,
  country: "DE",
  search_lang: "de",
});

// French search with French UI
await web_search({
  query: "actualités",
  country: "FR",
  search_lang: "fr",
  ui_lang: "fr",
});

// Recent results (past week)
await web_search({
  query: "TMBG interview",
  freshness: "pw",
});

web_fetch

抓取 URL 并提取可读内容。

要求

  • tools.web.fetch.enabled 不能为 false(默认:启用)
  • 可选的 Firecrawl 回退:设置 tools.web.fetch.firecrawl.apiKeyFIRECRAWL_API_KEY

配置

{
  tools: {
    web: {
      fetch: {
        enabled: true,
        maxChars: 50000,
        timeoutSeconds: 30,
        cacheTtlMinutes: 15,
        maxRedirects: 3,
        userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
        readability: true,
        firecrawl: {
          enabled: true,
          apiKey: "FIRECRAWL_API_KEY_HERE", // optional if FIRECRAWL_API_KEY is set
          baseUrl: "https://api.firecrawl.dev",
          onlyMainContent: true,
          maxAgeMs: 86400000, // ms (1 day)
          timeoutSeconds: 60,
        },
      },
    },
  },
}

工具参数

  • url(必需,仅 http/https)
  • extractModemarkdown | text
  • maxChars(截断长页面)

注意事项:

  • web_fetch 首先使用 Readability(主内容提取),然后使用 Firecrawl(如果已配置)。如果两者都失败,工具会返回错误。
  • Firecrawl 请求使用反机器人模式,并默认缓存结果。
  • web_fetch 默认发送类似 Chrome 的 User-Agent 和 Accept-Language;如需要可以覆盖 userAgent
  • web_fetch 会阻止私有/内部主机名,并重新检查重定向(用 maxRedirects 限制)。
  • web_fetch 是尽力而为的提取;某些网站需要使用 browser 工具。
  • 查看 Firecrawl 了解密钥设置和服务详情。
  • 响应会被缓存(默认 15 分钟)以减少重复抓取。
  • 如果你使用工具配置文件/白名单,添加 web_search/web_fetchgroup:web
  • 如果缺少 Brave 密钥,web_search 会返回简短的设置提示和文档链接。