跳到主要内容
Tavily 是一个专门为 AI 代理 (LLM) 构建的搜索引擎,能够快速提供实时、准确、真实的结果。Tavily 提供两个关键端点,其中之一是搜索,它提供专为 LLM 和 RAG 定制的搜索结果。 本指南提供了 Tavily 工具 的快速入门概览。有关 Tavily 工具的完整详细说明,您可以在 API 参考 中找到更详细的文档。

概览

集成详情

设置

该集成位于 @langchain/tavily 包中,您可以按如下所示安装
npm install @langchain/tavily @langchain/core

凭据

在此处设置 API 密钥,并将其设置为名为 TAVILY_API_KEY 的环境变量。
process.env.TAVILY_API_KEY = "YOUR_API_KEY"
设置 LangSmith 以获得一流的可观察性也很有帮助(但不是必需的)
process.env.LANGSMITH_TRACING="true"
process.env.LANGSMITH_API_KEY="your-api-key"

实例化

您可以像这样导入并实例化 TavilySearch 工具
import { TavilySearch } from "@langchain/tavily";

const tool = new TavilySearch({
  maxResults: 5,
  topic: "general",
  // includeAnswer: false,
  // includeRawContent: false,
  // includeImages: false,
  // includeImageDescriptions: false,
  // searchDepth: "basic",
  // timeRange: "day",
  // includeDomains: [],
  // excludeDomains: [],
});

调用

直接使用参数调用

Tavily 搜索工具在调用期间接受以下参数
  • query(必填):自然语言搜索查询
  • 在调用期间还可以设置以下参数:includeImagessearchDepthtimeRangeincludeDomainsexcludeDomainsincludeImages
  • 出于可靠性和性能原因,某些影响响应大小的参数在调用期间不能修改:includeAnswerincludeRawContent。这些限制可防止意外的上下文窗口问题并确保结果一致。
await tool.invoke({
  query: "what is the current weather in SF?"
});

使用 ToolCall 调用

我们还可以使用模型生成的 ToolCall 调用该工具,在这种情况下将返回 @[ToolMessage]
// This is usually generated by a model, but we'll create a tool call directly for demo purposes.
const modelGeneratedToolCall = {
  args: {
    query: "what is the current weather in SF?"
  },
  id: "1",
  name: tool.name,
  type: "tool_call",
}

await tool.invoke(modelGeneratedToolCall)

链接

我们可以将工具绑定到 工具调用模型,然后调用它,从而在链中使用我们的工具。
// @lc-docs-hide-cell

import { ChatOpenAI } from "@langchain/openai"

const llm = new ChatOpenAI({
  model: "gpt-4o",
  temperature: 0,
})
import { HumanMessage } from "@langchain/core/messages";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { RunnableLambda } from "@langchain/core/runnables";

const prompt = ChatPromptTemplate.fromMessages(
  [
    ["system", "You are a helpful assistant."],
    ["placeholder", "{messages}"],
  ]
)

const llmWithTools = llm.bindTools([tool]);

const chain = prompt.pipe(llmWithTools);

const toolChain = RunnableLambda.from(
  async (userInput: string, config) => {
    const humanMessage = new HumanMessage(userInput,);
    const aiMsg = await chain.invoke({
      messages: [new HumanMessage(userInput)],
    }, config);
    const toolMsgs = await tool.batch(aiMsg.tool_calls, config);
    return chain.invoke({
      messages: [humanMessage, aiMsg, ...toolMsgs],
    }, config);
  }
);

const toolChainResult = await toolChain.invoke("what is the current weather in sf?");
const { tool_calls, content } = toolChainResult;

console.log("AIMessage", JSON.stringify({
  tool_calls,
  content,
}, null, 2));

代理

有关如何在代理中使用 LangChain 工具的指南,请参阅 LangGraph.js 文档。

API 参考

有关所有 Tavily 搜索 API 功能和配置的详细文档,请参阅 API 参考: docs.tavily.com/documentation/api-reference/endpoint/search 工具文档
以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.