> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-docs-zh.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Tavily 提取集成

> 通过LangChain JavaScript与Tavily提取工具集成。

[Tavily](https://tavily.com/) 是专为 AI 智能体（LLM）构建的搜索引擎，能够快速提供实时、准确、事实性的结果。Tavily 提供一个 [Extract](https://docs.tavily.com/documentation/api-reference/endpoint/extract) 端点，可用于提取一个或多个 URL 的清理、解析内容。

## 概述

### 集成细节

| 类 | 包 | [PY 支持](https://langchain-docs-zh.mintlify.site/langchain/oss/python/integrations/tools/tavily_extract.md) | 下载量 | 版本 |
| :- | :- | :-: | :-: | :-: |
| [`TavilyExtract`](https://reference.langchain.com/javascript/langchain-tavily/TavilyExtract) | [`@langchain/tavily`](https://www.npmjs.com/package/@langchain/tavily) | ✅ | ![NPM - 下载量](https://img.shields.io/npm/dm/@langchain/tavily?style=flat-square\&label=%20&) | ![NPM - 版本](https://img.shields.io/npm/v/@langchain/tavily?style=flat-square\&label=%20&) |

### 工具特性

| [返回工件](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md) | 原生异步 | 返回数据 | 定价 |
| :-: | :-: | :-: | :-: |
| ❌ | ✅ | 原始内容和图片 | 每月 1,000 个免费信用 |

## 设置

集成位于 `@langchain/tavily` 包中，您可以通过以下方式安装：

```bash npm theme={null}
npm install @langchain/tavily @langchain/core
```

```bash yarn theme={null}
yarn add @langchain/tavily @langchain/core
```

```bash pnpm theme={null}
pnpm add @langchain/tavily @langchain/core
```

### 凭证

[设置 Tavily API 密钥](https://app.tavily.com) 并将其设置为名为 `TAVILY_API_KEY` 的环境变量。

```typescript theme={null}
process.env.TAVILY_API_KEY = "YOUR_API_KEY"
```

设置 [LangSmith](https://smith.langchain.com)（最佳观测性，但非必需）也很有帮助：

```typescript theme={null}
process.env.LANGSMITH_TRACING="true"
process.env.LANGSMITH_API_KEY="your-api-key"
```

## 实例化

工具在实例化期间接受以下参数：

* `extractDepth` (可选，字符串)：提取深度，`"basic"` 或 `"advanced"`。默认为 `"basic"`。
* `includeImages` (可选，布尔值)：是否在提取中包含图片。默认为 `false`。
* `format` (可选，字符串)：内容格式。`"markdown"` 或 `"text"`。默认为 `"markdown"`。
* `includeFavicon` (可选，布尔值)：包含每个结果的 favicon URL。默认为 `false`。

有关可用参数的全面概述，请参阅 [Tavily Extract API 文档](https://docs.tavily.com/documentation/api-reference/endpoint/extract)。

```typescript theme={null}
import { TavilyExtract } from "@langchain/tavily";

const tool = new TavilyExtract({
  extractDepth: "basic",
  includeImages: false,
});
```

## 调用

### [使用参数直接调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md)

Tavily 提取工具在调用期间接受以下参数：

* `urls` (必需)：要从中提取内容的 URL 列表。
* 以下参数也可以在调用期间设置：`extractDepth`, `includeImages`。

注意：可选参数可用于智能体动态设置。如果您在实例化期间设置了一个参数，然后在调用工具时使用不同的值，工具将使用您在调用期间传递的值。

```typescript theme={null}
await tool.invoke({ urls: ["https://en.wikipedia.org/wiki/Lionel_Messi"] });
```

```text theme={null}
{
  results: [{
    url: 'https://en.wikipedia.org/wiki/Lionel_Messi',
    raw_content: 'Lionel Messi - Wikipedia\nJump to content\nMain menu\n... (truncated)',
    images: []
  }],
  failed_results: [],
  response_time: 0.02
}
```

### [使用 ToolCall 调用](https://langchain-docs-zh.mintlify.site/langchain/oss/langchain/tools.md)

我们也可以使用模型生成的 `ToolCall` 调用该工具，此时将返回一个 @\[`ToolMessage`]：

```typescript theme={null}
// This is usually generated by a model, but we'll create a tool call directly for demo purposes.
const modelGeneratedToolCall = {
  args: { urls: ["https://en.wikipedia.org/wiki/Lionel_Messi"] },
  id: "1",
  name: tool.name,
  type: "tool_call",
};

const toolMsg = await tool.invoke(modelGeneratedToolCall);

// The content is a JSON string of results
console.log(toolMsg.content.slice(0, 400));
```

```json theme={null}
{"results": [{"url": "https://en.wikipedia.org/wiki/Lionel_Messi", "raw_content": "Lionel Messi - Wikipedia\nJump to content\nMain menu\nMain menu\nmove to sidebar hide\nNavigation\n\nMain page\nContents\nCurrent events\nRandom article\nAbout Wikipedia\nContact us\n\nContribute\n\nHelp\nLearn to edit\nCommunity portal\nRecent changes\nUpload file\nSpecial pages\n... (truncated)"}], "failed_results": [], "response_time": 0.02}
```

## 在智能体中使用

我们可以通过将其传递给 `createAgent` 直接使用提取工具与 LangChain 智能体。智能体可以作为其工具调用的一部分动态设置 URL 列表和 `extractDepth`、`includeImages` 等切换。

```typescript theme={null}
// @lc-docs-hide-cell
import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "gpt-5.5",
  temperature: 0,
});
```

```typescript theme={null}
import { TavilyExtract } from "@langchain/tavily";
import { createAgent } from "langchain";

const tavilyExtractTool = new TavilyExtract();

const agent = createAgent({
  model: llm,
  tools: [tavilyExtractTool],
});

const userInput = "Summarize https://en.wikipedia.org/wiki/Albert_Einstein and https://en.wikipedia.org/wiki/Theoretical_physics.";

const stream = await agent.streamEvents(
  { messages: [["human", userInput]] },
  { version: "v3" },
);

for await (const snapshot of stream.values) {
  const lastMsg = snapshot.messages[snapshot.messages.length - 1];
  if (lastMsg.tool_calls?.length) {
    console.dir(lastMsg.tool_calls, { depth: null });
  } else if (lastMsg.content) {
    console.log(lastMsg.content);
  }
}
```

***

## API 参考

有关 Tavily Extract API 功能和配置的详细文档，请前往 API 参考：[docs.tavily.com/documentation/api-reference/endpoint/extract](https://docs.tavily.com/documentation/api-reference/endpoint/extract)

***

> 官方原文：[src/oss/javascript/integrations/tools/tavily\_extract.mdx](https://github.com/langchain-ai/docs/blob/main/src/oss/javascript/integrations/tools/tavily_extract.mdx) · 非官方翻译，译文由 AI 生成，以英文原文为准。


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