Tavily integration for LangChain.js
npm install @langchain/tavily@langchain/tavily
This package provides integrations for the Tavily search engine within LangChain.js. Tavily is a search engine built specifically for AI agents (LLMs), delivering real-time, accurate, and factual results at speed.
This package exposes four tools:
- TavilySearch: Performs a search optimized for LLMs and RAG.
- TavilyExtract: Extracts raw content from a list of URLs.
- TavilyCrawl: Initiates a structured web crawl starting from a specified base URL.
- TavilyMap: Generates a site map starting from a specified base URL.
- TavilyResearch: Creates structured research tasks and optionally streams research output.
- TavilyGetResearch: Retrieves research results by request_id for previously created research tasks.
``bash`
npm install @langchain/tavily
You need a Tavily API key to use these tools. You can get one here. Set it as an environment variable:
`typescript`
process.env.TAVILY_API_KEY = "YOUR_API_KEY";
`typescript
import { TavilySearch } from "@langchain/tavily";
const tool = new TavilySearch({
maxResults: 5,
// You can set other constructor parameters here, e.g.:
// topic: "general",
// includeAnswer: false,
// includeRawContent: false,
// includeImages: false,
// searchDepth: "basic",
});
// Invoke with a query
const results = await tool.invoke({
query: "what is the current weather in SF?",
});
console.log(results);
`
`typescript
import { TavilyExtract } from "@langchain/tavily";
const tool = new TavilyExtract({
// Constructor parameters:
// extractDepth: "basic",
// includeImages: false,
});
// Invoke with a list of URLs
const results = await tool.invoke({
urls: ["https://en.wikipedia.org/wiki/Lionel_Messi"],
});
console.log(results);
`
`typescript
import { TavilyResearch } from "@langchain/tavily";
const tool = new TavilyResearch({
// Optional constructor defaults:
// model: "auto",
// stream: false,
// citationFormat: "numbered",
// apiBaseUrl: "https://api.tavily.com",
});
// Invoke with a research task
const result = await tool.invoke({
input: "Research the latest developments in AI",
model: "mini",
citationFormat: "apa",
});
console.log(result);
`
`typescript
import { TavilyGetResearch } from "@langchain/tavily";
const tool = new TavilyGetResearch({
// Optional constructor parameters:
// apiBaseUrl: "https://api.tavily.com",
});
// Invoke with a request_id returned from TavilyResearch
const result = await tool.invoke({
requestId: "your-request-id-here",
});
console.log(result);
`
`typescript
import { TavilyCrawl } from "@langchain/tavily";
const tool = new TavilyCrawl({
// Constructor parameters:
// extractDepth: "basic",
// format: "markdown",
// maxDepth: 3,
// maxBreadth: 50,
// limit: 100,
// includeImages: false,
// allowExternal: false,
});
// Invoke with a URL and optional parameters
const results = await tool.invoke({
url: "https://docs.tavily.com/",
instructions: "Find information about the LangChain integration.",
});
console.log(results);
`
`typescript
import { TavilyMap } from "@langchain/tavily";
const tool = new TavilyMap({
// Constructor parameters:
// maxDepth: 3,
// maxBreadth: 50,
// limit: 100,
// allowExternal: false,
});
// Invoke with a URL and optional parameters
const results = await tool.invoke({
url: "https://docs.tavily.com/",
});
console.log(results);
``
For more detailed information, check out the documentation pages:
- TavilySearch: http://js.langchain.com/docs/integrations/tools/tavily_search/
- TavilyExtract: http://js.langchain.com/docs/integrations/tools/tavily_extract/
- TavilyCrawl: http://js.langchain.com/docs/integrations/tools/tavily_crawl/
- TavilyMap: http://js.langchain.com/docs/integrations/tools/tavily_map/
This package is licensed under the MIT License. See the LICENSE file for details.