> ## Documentation Index
> Fetch the complete documentation index at: https://help.decodo.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Decodo provides web scraping and proxy infrastructure. For agent-specific guidance - which endpoint to call, how to authenticate, and the usage rules - read https://help.decodo.com/docs/for-agents.md.
> Add .md to any page URL on this site to get its Markdown source.
> Search these docs live over MCP at https://help.decodo.com/mcp.

# Vercel AI SDK

> @decodo/ai-sdk 软件包：为 Vercel AI SDK 提供 Decodo 抓取、搜索、站点地图和爬取工具，让任何模型都能读取实时网络数据。

`@decodo/ai-sdk` 是为 [**Vercel AI SDK**](https://ai-sdk.dev) 模型提供实时网络数据的 npm 软件包。它添加的工具可以将页面抓取为 Markdown、搜索网络、列出站点的 URL，以及爬取站点。所有工具都运行在 [**Web Data API**](https://help.decodo.com/docs/cn/web-data-api-introduction.md) 之上，并可配合任意 AI SDK 模型提供方使用。

> [!NOTE]
>
> 源代码、示例和最新版本请查看 [**ai-sdk 仓库**](https://github.com/Decodo/ai-sdk)。

## 功能

* **网页抓取**：将单个页面获取为 Markdown，并可选择先渲染 JavaScript
* **搜索**：搜索网络并获取排序后的结果
* **站点地图**：列出站点的 URL，而不获取其内容
* **爬取**：启动爬取、轮询其状态、以 Markdown 读取页面，或取消爬取
* **AI SDK 集成**：`decodoTools()` 返回标准的 AI SDK 工具，可整体传给 `generateText`，也可逐个选用
* **模型可处理的错误**：失败的调用会以工具错误的形式返回给模型，`generateText` 会继续运行

## 要求

* Node.js 18 或更高版本
* `ai` v6 或 v7，它是一个 peer dependency
* Decodo [**Web Data API 计划**](https://dashboard.decodo.com/web-data/pricing)。[**控制面板**](https://dashboard.decodo.com/)提供免费试用。

## 安装

```bash theme={null}
npm install @decodo/ai-sdk ai @ai-sdk/anthropic
```

本页示例使用 `@ai-sdk/anthropic` 并读取 `ANTHROPIC_API_KEY`。任意 AI SDK 模型提供方均可使用。

## 身份验证

在 [**Web Data API 演练场**](https://dashboard.decodo.com/web-data/playground)中，从您的 Web Data API 订阅复制 API 密钥，并将其存储在 `DECODO_API_KEY` 环境变量中：

```bash theme={null}
export DECODO_API_KEY=your_api_key
```

您也可以直接传入密钥：

```typescript theme={null}
decodoTools({ apiKey: 'your_api_key' });
```

当 API 密钥缺失，或其中包含换行符、空格等无法放入请求头的字符时，`decodoTools()` 会立即抛出异常。

## 用法

将全部工具传给 `generateText`：

```typescript theme={null}
import { anthropic } from '@ai-sdk/anthropic';
import { decodoTools } from '@decodo/ai-sdk';
import { generateText, stepCountIs } from 'ai';

const { text } = await generateText({
  model: anthropic('claude-sonnet-5-5'),
  tools: decodoTools({ apiKey: process.env.DECODO_API_KEY }),
  stopWhen: stepCountIs(5),
  prompt: 'Scrape https://ip.decodo.com and tell me the country of the IP in one sentence.',
});

console.log(text);
```

若只想向模型提供部分工具，可以解构它们。例如，一次爬取需要 `crawl_start`、`crawl_status` 和 `crawl_results`：

```typescript theme={null}
const { crawl_start, crawl_status, crawl_results } = decodoTools();
const { text } = await generateText({
  model: anthropic('claude-sonnet-5-5'),
  tools: { crawl_start, crawl_status, crawl_results },
  stopWhen: stepCountIs(30),
  prompt: 'Crawl 5 pages of https://decodo.com and summarise what the company sells.',
});
```

模型通过再次调用 `crawl_status` 来轮询，每次调用都会消耗一个步骤，因此涉及爬取的提示词应设置更高的 `stepCountIs`。当爬取处于 `pending` 状态时，`crawl_status` 会等待 5 秒再返回。这会放慢轮询速度，避免长时间的爬取耗尽步骤。

## 可用工具

| 工具 | 模型获得的内容 |
| - | - |
| `scrape` | 以 Markdown 返回的单个页面，并带有 `truncated`。超过 45,000 个字符的内容会被截断，此时 `totalLength` 会说明页面的实际长度 |
| `search` | 排序后的自然搜索结果，以及搜索引擎返回时附带的答案框、热门报道、其他人也在问和相关搜索 |
| `map` | 站点的 URL，默认 10 个，最多 500 个 |
| `crawl_start` | 立即返回 `crawl_id`。默认爬取 10 个页面，最多 100 个 |
| `crawl_status` | 状态和页面计数。当 `status` 不再是 `pending` 时，爬取即已完成 |
| `crawl_results` | 以 Markdown 返回已爬取的页面，每次调用约 45,000 个字符，并通过 `nextCursor` 获取其余内容 |
| `crawl_cancel` | 停止爬取。此前已爬取的页面仍可读取 |

单次调用容纳不下的爬取页面会被拆分到多次调用中，每一部分都带有标明字符范围的 `contentPart`。每个页面还会返回其 `url`、`depth`、`status` 和 `content_error`，响应中也包含 `total`，便于模型判断还有多少内容未读取。

### 输入参数

输入参数是严格的：未列出的字段会被拒绝，而不是被忽略。

| 工具 | 输入参数 |
| - | - |
| `scrape` | `url`（必填）、`geo`、`locale`，以及用于先渲染 JavaScript 的 `headless: 'html'` |
| `search` | `query`（必填）、`geo`、`locale`、`page` |
| `map` | `url`（必填）、`limit`、`sitemap`、`domain_filter`、`select_paths`、`exclude_paths`、`geo`、`locale` |
| `crawl_start` | `map` 的全部输入参数，另加 `max_depth` |
| `crawl_status`、`crawl_cancel` | `crawl_id` |
| `crawl_results` | `crawl_id`、`cursor` |

`limit`、`max_depth`、`select_paths` 和 `exclude_paths` 可以让爬取避开站点中您不需要的部分，因此值得在提示词中加以设置。

## 错误处理

失败的调用会以模型可读取并据此行动的工具错误形式返回，`generateText` 会继续运行：

* API 密钥被拒绝时，错误信息会指明 `DECODO_API_KEY`。
* 速率限制和 `5xx` 响应会提示模型稍后重试。
* 超时会提示模型稍后重试，但 `scrape` 和 `crawl_start` 除外。这两类请求可能已经执行，因此会告知模型除非被要求，否则不要重复发送。
* 校验错误会提示模型修改输入参数。
* 无法抓取的页面会被视为错误，即使 API 返回了 `200`。

这些工具背后的完整 API 参数列表，请参阅[**参数**](https://help.decodo.com/docs/cn/web-scraping-api-parameters-overview.md)。

***

## 相关内容

* [**TypeScript SDK**](https://help.decodo.com/docs/cn/typescript-sdk.md)：这些工具所基于的 SDK。
* [**LangChain（TypeScript）**](https://help.decodo.com/docs/cn/langchain.md)：面向 Node.js 中 LangChain 智能体的 Decodo 工具。
* [**MCP 服务器**](https://help.decodo.com/docs/cn/mcp.md)：将 AI 智能体和兼容 MCP 的工具连接到 Decodo。
* [GitHub 仓库](https://github.com/Decodo/ai-sdk)：查看源代码、示例和最新版本。

***

> [card] [支持](https://direct.lc.chat/12092754)
>
> 需要帮助或只是想打个招呼？我们的支持团队全天候为您服务。 \
> 您也可以随时通过电子邮件 [support@decodo.com](mailto:support@decodo.com) 联系我们。

> [card] [反馈](mailto:feedback@decodo.com)
>
> 找不到您要找的内容？请求一篇文章！ \
> 有反馈意见？分享您对我们如何改进的想法。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.