|
| 1 | +--- |
| 2 | +sidebar_label: Tavily Crawl |
| 3 | +title: TavilyCrawl |
| 4 | +--- |
| 5 | + |
| 6 | +export const quartoRawHtml = [ |
| 7 | + ` |
| 8 | +<table> |
| 9 | +<colgroup> |
| 10 | +<col style="width: 22%" /> |
| 11 | +<col style="width: 22%" /> |
| 12 | +<col style="width: 27%" /> |
| 13 | +<col style="width: 27%" /> |
| 14 | +</colgroup> |
| 15 | +<thead> |
| 16 | +<tr> |
| 17 | +<th style="text-align: left;">Class</th> |
| 18 | +<th style="text-align: left;">Package</th> |
| 19 | +<th style="text-align: center;"><a href="https://python.langchain.com/docs/integrations/tools/tavily_search/">PY support</a></th> |
| 20 | +<th style="text-align: center;">Package latest</th> |
| 21 | +</tr> |
| 22 | +</thead> |
| 23 | +<tbody> |
| 24 | +<tr> |
| 25 | +<td style="text-align: left;"><a href="https://api.js.langchain.com/classes/_langchain_tavily.TavilyCrawl.html">TavilyMap</a></td> |
| 26 | +<td style="text-align: left;"><a href="https://www.npmjs.com/package/@langchain/tavily"><code>@langchain/tavily</code></a></td> |
| 27 | +<td style="text-align: center;">✅</td> |
| 28 | +<td style="text-align: center;"><img src="https://img.shields.io/npm/v/@langchain/tavily?style=flat-square&label=%20&.png" alt="NPM - Version" /></td> |
| 29 | +</tr> |
| 30 | +</tbody> |
| 31 | +</table> |
| 32 | +`, |
| 33 | +]; |
| 34 | + |
| 35 | +[Tavily](https://tavily.com/) is a search engine built specifically for |
| 36 | +AI agents (LLMs), delivering real-time, accurate, and factual results at |
| 37 | +speed. Tavily offers four key endpoints, one of which being Crawl, which |
| 38 | +provides a graph-based website traversal tool that can explore hundreds |
| 39 | +of paths in parallel with built-in extraction and intelligent discovery. |
| 40 | + |
| 41 | +This guide provides a quick overview for getting started with the Tavily |
| 42 | +[tool](../../../docs/integrations/tools/). For a complete breakdown of |
| 43 | +the Tavily tool, you can find more detailed documentation in the [API |
| 44 | +reference](https://v03.api.js.langchain.com/modules/_langchain_tavily.html). |
| 45 | + |
| 46 | +## Overview |
| 47 | + |
| 48 | +### Integration details |
| 49 | + |
| 50 | +<div dangerouslySetInnerHTML={{ __html: quartoRawHtml[0] }} /> |
| 51 | + |
| 52 | +## Setup |
| 53 | + |
| 54 | +The integration lives in the `@langchain/tavily` package, which you can |
| 55 | +install as shown below: |
| 56 | + |
| 57 | +```mdx-code-block |
| 58 | +import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx"; |
| 59 | +import Npm2Yarn from "@theme/Npm2Yarn"; |
| 60 | +
|
| 61 | +<IntegrationInstallTooltip></IntegrationInstallTooltip> |
| 62 | +
|
| 63 | +<Npm2Yarn> |
| 64 | + @langchain/tavily @langchain/core |
| 65 | +</Npm2Yarn> |
| 66 | +``` |
| 67 | + |
| 68 | +### Credentials |
| 69 | + |
| 70 | +Set up an API key [here](https://app.tavily.com) and set it as an |
| 71 | +environment variable named `TAVILY_API_KEY`. |
| 72 | + |
| 73 | +```typescript |
| 74 | +process.env.TAVILY_API_KEY = "YOUR_API_KEY"; |
| 75 | +``` |
| 76 | + |
| 77 | +It’s also helpful (but not needed) to set up |
| 78 | +[LangSmith](https://smith.langchain.com/) for best-in-class |
| 79 | +observability: |
| 80 | + |
| 81 | +```typescript |
| 82 | +process.env.LANGSMITH_TRACING = "true"; |
| 83 | +process.env.LANGSMITH_API_KEY = "your-api-key"; |
| 84 | +``` |
| 85 | + |
| 86 | +## Instantiation |
| 87 | + |
| 88 | +You can import and instantiate an instance of the `TavilyCrawl` tool |
| 89 | +like this: |
| 90 | + |
| 91 | +```typescript |
| 92 | +import { TavilyCrawl } from "@langchain/tavily"; |
| 93 | + |
| 94 | +const tool = new TavilyCrawl({ |
| 95 | + maxDepth: 3, |
| 96 | + maxBreadth: 50, |
| 97 | + // extractDepth: "basic", |
| 98 | + // format: "markdown", |
| 99 | + // limit: 100, |
| 100 | + // includeImages: false, |
| 101 | + // allowExternal: false, |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +## Invocation |
| 106 | + |
| 107 | +### [Invoke directly with args](../../../docs/concepts/tools) |
| 108 | + |
| 109 | +The Tavily crawl tool accepts the following arguments during invocation: |
| 110 | + |
| 111 | +- `url` (required): A natural language search query |
| 112 | + |
| 113 | +- The following arguments can also be set during invocation : |
| 114 | + `instructions`, `selectPaths` , `selectDomains`, `excludePaths`, |
| 115 | + `excludeDomains`, `allowExternal`, `categories`. |
| 116 | + |
| 117 | +```typescript |
| 118 | +await tool.invoke({ |
| 119 | + url: "https://docs.tavily.com", |
| 120 | +}); |
| 121 | +``` |
| 122 | + |
| 123 | +### [Invoke with ToolCall](../../../docs/concepts/tools) |
| 124 | + |
| 125 | +We can also invoke the tool with a model-generated `ToolCall`, in which |
| 126 | +case a `ToolMessage` will be returned: |
| 127 | + |
| 128 | +```typescript |
| 129 | +// This is usually generated by a model, but we'll create a tool call directly for demo purposes. |
| 130 | +const modelGeneratedToolCall = { |
| 131 | + args: { |
| 132 | + url: "https://docs.tavily.com", |
| 133 | + }, |
| 134 | + id: "1", |
| 135 | + name: tool.name, |
| 136 | + type: "tool_call", |
| 137 | +}; |
| 138 | + |
| 139 | +await tool.invoke(modelGeneratedToolCall); |
| 140 | +``` |
| 141 | + |
| 142 | +## Chaining |
| 143 | + |
| 144 | +We can use our tool in a chain by first binding it to a [tool-calling |
| 145 | +model](../../../docs/how_to/tool_calling/) and then calling it: |
| 146 | + |
| 147 | +```mdx-code-block |
| 148 | +import ChatModelTabs from "@theme/ChatModelTabs"; |
| 149 | +
|
| 150 | +<ChatModelTabs customVarName="llm" /> |
| 151 | +``` |
| 152 | + |
| 153 | +```typescript |
| 154 | +import { HumanMessage } from "@langchain/core/messages"; |
| 155 | +import { ChatPromptTemplate } from "@langchain/core/prompts"; |
| 156 | +import { RunnableLambda } from "@langchain/core/runnables"; |
| 157 | + |
| 158 | +const prompt = ChatPromptTemplate.fromMessages([ |
| 159 | + ["system", "You are a helpful assistant."], |
| 160 | + ["placeholder", "{messages}"], |
| 161 | +]); |
| 162 | + |
| 163 | +const llmWithTools = llm.bindTools([tool]); |
| 164 | + |
| 165 | +const chain = prompt.pipe(llmWithTools); |
| 166 | + |
| 167 | +const toolChain = RunnableLambda.from(async (userInput: string, config) => { |
| 168 | + const humanMessage = new HumanMessage(userInput); |
| 169 | + const aiMsg = await chain.invoke( |
| 170 | + { |
| 171 | + messages: [new HumanMessage(userInput)], |
| 172 | + }, |
| 173 | + config |
| 174 | + ); |
| 175 | + const toolMsgs = await tool.batch(aiMsg.tool_calls, config); |
| 176 | + return chain.invoke( |
| 177 | + { |
| 178 | + messages: [humanMessage, aiMsg, ...toolMsgs], |
| 179 | + }, |
| 180 | + config |
| 181 | + ); |
| 182 | +}); |
| 183 | + |
| 184 | +const toolChainResult = await toolChain.invoke("https://docs.tavily.com"); |
| 185 | +``` |
| 186 | + |
| 187 | +```typescript |
| 188 | +const { tool_calls, content } = toolChainResult; |
| 189 | + |
| 190 | +console.log( |
| 191 | + "AIMessage", |
| 192 | + JSON.stringify( |
| 193 | + { |
| 194 | + tool_calls, |
| 195 | + content, |
| 196 | + }, |
| 197 | + null, |
| 198 | + 2 |
| 199 | + ) |
| 200 | +); |
| 201 | +``` |
| 202 | + |
| 203 | +## Agents |
| 204 | + |
| 205 | +For guides on how to use LangChain tools in agents, see the |
| 206 | +[LangGraph.js](https://langchain-ai.github.io/langgraphjs/how-tos/#tool-calling) |
| 207 | +docs. |
| 208 | + |
| 209 | +## API reference |
| 210 | + |
| 211 | +For detailed documentation of all Tavily Crawl API features and |
| 212 | +configurations head to the API reference: |
| 213 | + |
| 214 | +https://docs.tavily.com/documentation/api-reference/endpoint/crawl |
| 215 | + |
| 216 | +## Related |
| 217 | + |
| 218 | +- Tool [conceptual |
| 219 | + guide](https://js.langchain.com/docs/concepts/tools/) |
| 220 | + |
| 221 | +- Tool [how-to guides](https://js.langchain.com/docs/how_to/#tools) |
0 commit comments