# README
Function Documentation for Tavily Search Package
Overview
This package provides a concurrent search implementation using the Tavily API. It supports multiple search modes and uses Go's errgroup package for efficient concurrent execution.
Core Components
searchTavily.go Functions
DeepSearch
func DeepSearch(searchQuery string) (string, error)
Performs an expanded search operation by breaking down the query into multiple sub-queries and aggregating results.
Parameters:
searchQuery
(string): The original search query to be expanded and searched
Returns:
string
: Combined search results from all sub-querieserror
: Error if the search fails
Error cases:
- Empty query provided
- Query expansion failure
- All searches failed
- No results found
SearchInternet
func SearchInternet(searchQuery string) (string, error)
Performs a single search query against the Tavily API.
Parameters:
searchQuery
(string): The search query to execute
Returns:
string
: Search results in JSON formaterror
: Error if the search fails
Error cases:
- Empty query provided
- Search execution failed
- No results returned
- Empty results returned
SearchAtentInternet
func SearchAtentInternet(searchQueries []string, searchFunc func(string, int) (string, error), envVar string)
Manages concurrent execution of multiple search queries using errgroup.Group.
Parameters:
searchQueries
([]string): Array of search queries to executesearchFunc
(func(string, int) (string, error)): Function to perform individual searchesenvVar
(string): Environment variable name for max results configuration
Returns:
[]SearchResult
: Array of search results with timing and error information
Concurrency Features:
- Uses errgroup.Group for goroutine management
- Mutex-based synchronization for result collection
- Context-based timeout (30 seconds)
- Automatic cleanup and cancellation
tavilyclient.go Functions
TavilySearch
func TavilySearch(searchQuery string, maxResults int) (string, error)
Performs a general internet search using the Tavily API.
Parameters:
searchQuery
(string): The search query to executemaxResults
(int): Maximum number of results to return
Returns:
string
: JSON-formatted search resultserror
: Error if the search fails
Configuration:
- Topic: General
- IncludeAnswer: true
- IncludeImages: false
- IncludeRawContent: false
TavilyMaxSearch
func TavilyMaxSearch(searchQuery string) (string, error)
Performs a deep search with advanced depth configuration.
Configuration:
- Topic: General
- SearchDepth: advance
- IncludeAnswer: true
- IncludeRawContent: true
- MaxResults: From MAXDeepRESULTS environment variable
TavilyNewsSearch
func TavilyNewsSearch(searchQuery string) (string, error)
Performs a basic news search.
Configuration:
- Topic: News
- SearchDepth: basic
- IncludeAnswer: true
- MaxResults: From MAXRESULTS environment variable
TavilyRawNewsSearch
func TavilyRawNewsSearch(searchQuery string) (string, error)
Performs an advanced news search with expanded results.
Configuration:
- Topic: News
- SearchDepth: advance
- IncludeAnswer: true
- MaxResults: From MAXDeepRESULTS environment variable
- Days: 5
API Key and Configuration Management
func getAPIKey() string
func getMaxResults(envVar string) int
API Key Management:
- Checks environment variable TAVILY_API_KEY
- Falls back to default API key if environment variable is not set
Max Results Configuration:
- Reads from specified environment variable (MAXRESULTS or MAXDeepRESULTS)
- Falls back to default value of 3 if not set
Data Structures
SearchResult
type SearchResult struct {
Query string `json:"query"`
Results string `json:"results"`
WorkerID int `json:"worker_id"`
TimeSpent time.Duration `json:"time_spent"`
Error error `json:"error"`
}
Structure for holding search results and metadata.
Error Handling Patterns
- Input Validation
if searchQuery == "" {
return "", fmt.Errorf("empty search query provided")
}
- Error Wrapping
if err != nil {
return "", fmt.Errorf("search failed: %v", err)
}
- Concurrent Error Handling
if err := g.Wait(); err != nil {
logging.WriteLogs(fmt.Sprintf("Search group error: %v", err))
}
Logging
The package implements comprehensive logging throughout:
- Search initialization and completion
- Worker status updates and timing
- Error conditions
- API interactions
Dependencies
- golang.org/x/sync/errgroup
- github.com/hekmon/tavily
- Internal packages:
- anthropicfunc/logging
- anthropicfunc/util
Configuration
Environment Variables
- TAVILY_API_KEY: API key for Tavily service
- MAXRESULTS: Maximum results for regular searches
- MAXDeepRESULTS: Maximum results for deep searches
Default Settings
- Default API key provided as fallback
- Default max results: 3
- Search timeout: 30 seconds
- News search default days: 5
Concurrency Settings
- Worker count matches query count
- Mutex-protected result collection
- Context-based timeout management