Tavily Search
Tavily Search is a robust search API tailored specifically for LLM Agents. It seamlessly integrates with diverse data sources to ensure a superior, relevant search experience.
Setup
Set up an API key here and set it as an environment variable named TAVILY_API_KEY.
You'll also need to install the @langchain/community package:
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
Usage
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
import { ChatOpenAI } from "@langchain/openai";
import type { ChatPromptTemplate } from "@langchain/core/prompts";
import { pull } from "langchain/hub";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
// Define the tools the agent will have access to.
const tools = [new TavilySearchResults({ maxResults: 1 })];
// Get the prompt to use - you can modify this!
// If you want to see the prompt in full, you can at:
// https://smith.langchain.com/hub/hwchase17/openai-functions-agent
const prompt = await pull<ChatPromptTemplate>(
  "hwchase17/openai-functions-agent"
);
const llm = new ChatOpenAI({
  model: "gpt-3.5-turbo-1106",
  temperature: 0,
});
const agent = await createOpenAIFunctionsAgent({
  llm,
  tools,
  prompt,
});
const agentExecutor = new AgentExecutor({
  agent,
  tools,
});
const result = await agentExecutor.invoke({
  input: "what is the weather in wailea?",
});
console.log(result);
/*
  {
    input: 'what is the weather in wailea?',
    output: "The current weather in Wailea, HI is 64°F with clear skies. The high for today is 82°F and the low is 66°F. If you'd like more detailed information, you can visit [The Weather Channel](https://weather.com/weather/today/l/Wailea+HI?canonicalCityId=ffa9df9f7220c7e22cbcca3dc0a6c402d9c740c755955db833ea32a645b2bcab)."
  }
*/
API Reference:
- TavilySearchResults from @langchain/community/tools/tavily_search
- ChatOpenAI from @langchain/openai
- ChatPromptTemplate from @langchain/core/prompts
- pull from langchain/hub
- AgentExecutor from langchain/agents
- createOpenAIFunctionsAgent from langchain/agents
Related
- Tool conceptual guide
- Tool how-to guides