This example demonstrates how to integrate Tavily's advanced web search API with VoltAgent to create an AI agent capable of real-time web search and content extraction.
- Web Search: Search the web for real-time information using Tavily's advanced search API
- Content Extraction: Extract detailed content from specific URLs
- AI-Powered Answers: Get comprehensive answers based on current web data
- Flexible Search Options: Configure search depth, domains, and result limits
- Tavily API Key: Get your API key from Tavily
- OpenAI API Key: For the AI model integration
-
Install dependencies:
pnpm install
-
Configure environment variables: Copy the example environment file and add your API keys:
cp .env.example .env
Then edit the
.envfile with your actual API keys:OPENAI_API_KEY=your_actual_openai_api_key TAVILY_API_KEY=your_actual_tavily_api_key
-
Run the example:
pnpm dev
The agent will be available at http://localhost:3141 and can handle various types of queries:
- "What's the latest news about AI?"
- "Find information about climate change"
- "Search for the best restaurants in Paris"
- "What's the current weather in New York?"
- "Extract content from this URL: https://example.com/article"
- "Get the full text from this news article: [URL]"
- Purpose: Search the web for real-time information
- Parameters:
query: Search query stringnumResults: Number of results to return (default: 5, max: 10)searchDepth: "basic" or "advanced" (default: "basic")includeDomains: Specific domains to searchexcludeDomains: Domains to excludemaxResults: Maximum number of results
- Purpose: Extract content from specific URLs
- Parameters:
url: URL to extract content fromincludeRawContent: Include raw HTML content (default: false)
Try these example queries with the agent:
- Current Events: "What are the latest developments in artificial intelligence?"
- Research: "Find recent studies about renewable energy"
- Local Information: "What are the best restaurants in San Francisco?"
- Technical Information: "What's the current status of TypeScript 5.0?"
- Content Analysis: "Extract and summarize this article: [URL]"
The agent is configured with:
- Model: GPT-4o-mini for cost-effective processing
- Port: 3141 (configurable)
- Logging: Pino logger with info level
- Tools: Tavily search and extract tools
This example uses:
- Tavily API: For web search and content extraction
- OpenAI API: For AI model processing
- VoltAgent: For agent orchestration and tool management
The tools include comprehensive error handling for:
- Missing API keys
- Network errors
- API rate limits
- Invalid URLs
- Empty search results
To modify or extend this example:
- Add new tools: Create additional tools in
src/tools.ts - Modify agent behavior: Update the agent instructions in
src/index.ts - Add new features: Extend the agent with additional capabilities
- API Key Issues: Ensure both
OPENAI_API_KEYandTAVILY_API_KEYare set - Network Errors: Check your internet connection and API endpoints
- Rate Limits: Tavily has usage limits; check your account status
- Port Conflicts: Change the port in the server configuration if needed