Web 工具

适用范围

在以下情况使用本页面:

  • 您想要启用 web_search 或 web_fetch
  • 您需要 Brave 搜索 API 密钥设置
  • 您想使用 Perplexity Sonar 进行 web 搜索

Moltbot 提供两个轻量级 web 工具:

  • web_search — 通过 Brave 搜索 API(默认)或 Perplexity Sonar(直连或通过 OpenRouter)搜索 web。
  • web_fetch — HTTP 获取 + 可读内容提取(HTML → markdown/text)。

这些不是浏览器自动化。对于 JS 重的站点或登录,请使用 浏览器工具

工作原理

  • web_search 调用您配置的提供商并返回结果。
    • Brave(默认):返回结构化结果(标题、URL、片段)。
    • Perplexity:返回带有来自实时 web 搜索引用的 AI 综合答案。
  • 结果按查询缓存 15 分钟(可配置)。
  • web_fetch 执行纯 HTTP GET 并提取可读内容 (HTML → markdown/text)。它不会执行 JavaScript。
  • web_fetch 默认启用(除非明确禁用)。

选择搜索提供商

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

有关特定于提供商的详细信息,请参阅 Brave 搜索设置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 搜索 API 账户
  2. 在仪表板中,选择 Data for Search 计划(不是 “Data for AI”)并生成 API 密钥。
  3. 运行 moltbot configure --section web 将密钥存储在配置中(推荐),或在您的环境中设置 BRAVE_API_KEY

Brave 提供免费层和付费计划;请查看 Brave API 门户以了解 当前限制和定价。

在哪里设置密钥(推荐)

**推荐:**运行 moltbot configure --section web。它将密钥存储在 ~/.moltbot/moltbot.json 中的 tools.web.search.apiKey 下。

**环境替代方案:**在网关进程 环境中设置 BRAVE_API_KEY。对于网关安装,将其放在 ~/.moltbot/.env 中(或您的 服务环境)中。参见 环境变量

使用 Perplexity(直连或通过 OpenRouter)

Perplexity Sonar 模型具有内置的 web 搜索功能,并返回带有引用的 AI 综合答案。您可以通过 OpenRouter 使用它们(无需信用卡 - 支持 加密货币/预付)。

获取 OpenRouter API 密钥

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

设置 Perplexity 搜索

{
  tools: {
    web: {
      search: {
        enabled: true,
        provider: "perplexity",
        perplexity: {
          // API 密钥(如果设置了 OPENROUTER_API_KEY 或 PERPLEXITY_API_KEY,则可选)
          apiKey: "sk-or-v1-...",
          // 基础 URL(如果省略,则密钥感知默认)
          baseUrl: "https://openrouter.ai/api/v1",
          // 模型(默认为 perplexity/sonar-pro)
          model: "perplexity/sonar-pro"
        }
      }
    }
  }
}

**环境替代方案:**在网关 环境中设置 OPENROUTER_API_KEYPERPLEXITY_API_KEY。对于网关安装,将其放在 ~/.moltbot/.env 中。

如果没有设置基础 URL,Moltbot 根据 API 密钥来源选择默认值:

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

可用的 Perplexity 模型

模型描述最适合
perplexity/sonar带有 web 搜索的快速问答快速查找
perplexity/sonar-pro(默认)带有 web 搜索的多步推理复杂问题
perplexity/sonar-reasoning-pro思维链分析深度研究

web_search

使用您配置的提供商搜索 web。

要求

  • 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", // 如果设置了 BRAVE_API_KEY,则可选
        maxResults: 5,
        timeoutSeconds: 30,
        cacheTtlMinutes: 15
      }
    }
  }
}

工具参数

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

示例:

// 德语特定搜索
await web_search({
  query: "TV online schauen",
  count: 10,
  country: "DE",
  search_lang: "de"
});

// 法语搜索与法语 UI
await web_search({
  query: "actualités",
  country: "FR",
  search_lang: "fr",
  ui_lang: "fr"
});

// 最近的结果(过去一周)
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", // 如果设置了 FIRECRAWL_API_KEY,则可选
          baseUrl: "https://api.firecrawl.dev",
          onlyMainContent: true,
          maxAgeMs: 86400000, // ms(1 天)
          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 是尽力提取;某些站点将需要浏览器工具。
  • 有关密钥设置和服务详细信息,请参阅 Firecrawl
  • 响应被缓存(默认 15 分钟)以减少重复获取。
  • 如果您使用工具配置文件/允许列表,请添加 web_search/web_fetchgroup:web
  • 如果 Brave 密钥缺失,web_search 将返回一个简短的设置提示和文档链接。