Tavily Search Agent Example
This example demonstrates how to use the Tavily Search tool with a LangGraph ReAct Agent.
Prerequisites
You need a Tavily API key and an OpenAI-compatible API key (e.g., DeepSeek, OpenAI).
Set the environment variables:
export TAVILY_API_KEY=your_tavily_key
export OPENAI_API_KEY=your_openai_key
# OR
export DEEPSEEK_API_KEY=your_deepseek_key
Usage
Run the example:
go run main.go
How it Works
- Initialize LLM: Connects to the LLM (defaulting to DeepSeek-V3).
- Initialize Tool: Creates the Tavily search tool.
- Create Agent: Uses
prebuilt.CreateReactAgent to build a graph-based agent that has access to the tool.
- Execute: The agent receives a query, decides to use the Tavily tool to find information, and then synthesizes the answer.
Code Overview
// Create the agent with LLM and Tools
agent, err := prebuilt.CreateReactAgent(llm, []tools.Tool{tavilyTool})
// Invoke the agent
response, err := agent.Invoke(ctx, inputs)