File size: 7,519 Bytes
1041734 e7b4937 1041734 8e48c56 1041734 8e48c56 1041734 8e48c56 1041734 8e48c56 1041734 8e48c56 1041734 8e48c56 1041734 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
"""
Web Search Tool - Tavily and Exa implementations
Author: @mangubee
Date: 2026-01-02
Provides web search functionality with:
- Tavily as primary search (free tier: 1000 req/month)
- Exa as fallback (paid tier)
- Retry logic with exponential backoff
- Structured error handling
"""
import logging
from typing import Dict, List, Optional
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
retry_if_exception_type,
)
from src.config.settings import Settings
# ============================================================================
# CONFIG
# ============================================================================
MAX_RETRIES = 3
RETRY_MIN_WAIT = 1 # seconds
RETRY_MAX_WAIT = 10 # seconds
DEFAULT_MAX_RESULTS = 5
# ============================================================================
# Logging Setup
# ============================================================================
logger = logging.getLogger(__name__)
# ============================================================================
# Tavily Search Implementation
# ============================================================================
@retry(
stop=stop_after_attempt(MAX_RETRIES),
wait=wait_exponential(multiplier=1, min=RETRY_MIN_WAIT, max=RETRY_MAX_WAIT),
retry=retry_if_exception_type((ConnectionError, TimeoutError)),
reraise=True,
)
def tavily_search(query: str, max_results: int = DEFAULT_MAX_RESULTS) -> Dict:
"""
Search using Tavily API with retry logic.
Args:
query: Search query string
max_results: Maximum number of results to return (default: 5)
Returns:
Dict with structure: {
"results": [{"title": str, "url": str, "snippet": str}, ...],
"source": "tavily",
"query": str,
"count": int
}
Raises:
ValueError: If API key not configured
ConnectionError: If API connection fails after retries
Exception: For other API errors
"""
try:
from tavily import TavilyClient
settings = Settings()
api_key = settings.tavily_api_key
if not api_key:
raise ValueError("TAVILY_API_KEY not configured in settings")
logger.info(f"Tavily search: query='{query}', max_results={max_results}")
client = TavilyClient(api_key=api_key)
response = client.search(query=query, max_results=max_results)
# Extract and structure results
results = []
for item in response.get("results", []):
results.append(
{
"title": item.get("title", ""),
"url": item.get("url", ""),
"snippet": item.get("content", ""),
}
)
logger.info(f"Tavily search successful: {len(results)} results")
return {
"results": results,
"source": "tavily",
"query": query,
"count": len(results),
}
except ValueError as e:
logger.error(f"Tavily configuration error: {e}")
raise
except (ConnectionError, TimeoutError) as e:
logger.warning(f"Tavily connection error (will retry): {e}")
raise
except Exception as e:
logger.error(f"Tavily search error: {e}")
raise Exception(f"Tavily search failed: {str(e)}")
# ============================================================================
# Exa Search Implementation
# ============================================================================
@retry(
stop=stop_after_attempt(MAX_RETRIES),
wait=wait_exponential(multiplier=1, min=RETRY_MIN_WAIT, max=RETRY_MAX_WAIT),
retry=retry_if_exception_type((ConnectionError, TimeoutError)),
reraise=True,
)
def exa_search(query: str, max_results: int = DEFAULT_MAX_RESULTS) -> Dict:
"""
Search using Exa API with retry logic.
Args:
query: Search query string
max_results: Maximum number of results to return (default: 5)
Returns:
Dict with structure: {
"results": [{"title": str, "url": str, "snippet": str}, ...],
"source": "exa",
"query": str,
"count": int
}
Raises:
ValueError: If API key not configured
ConnectionError: If API connection fails after retries
Exception: For other API errors
"""
try:
from exa_py import Exa
settings = Settings()
api_key = settings.exa_api_key
if not api_key:
raise ValueError("EXA_API_KEY not configured in settings")
logger.info(f"Exa search: query='{query}', max_results={max_results}")
client = Exa(api_key=api_key)
response = client.search(
query=query, num_results=max_results, use_autoprompt=True
)
# Extract and structure results
results = []
for item in response.results:
results.append(
{
"title": item.title if hasattr(item, "title") else "",
"url": item.url if hasattr(item, "url") else "",
"snippet": item.text if hasattr(item, "text") else "",
}
)
logger.info(f"Exa search successful: {len(results)} results")
return {
"results": results,
"source": "exa",
"query": query,
"count": len(results),
}
except ValueError as e:
logger.error(f"Exa configuration error: {e}")
raise
except (ConnectionError, TimeoutError) as e:
logger.warning(f"Exa connection error (will retry): {e}")
raise
except Exception as e:
logger.error(f"Exa search error: {e}")
raise Exception(f"Exa search failed: {str(e)}")
# ============================================================================
# Unified Search with Fallback
# ============================================================================
def search(query: str, max_results: int = DEFAULT_MAX_RESULTS) -> Dict:
"""
Unified search function with automatic fallback.
Tries Tavily first (free tier), falls back to Exa if Tavily fails.
Args:
query: Search query string
max_results: Maximum number of results to return (default: 5)
Returns:
Dict with search results from either Tavily or Exa
Raises:
Exception: If both Tavily and Exa searches fail
"""
settings = Settings()
default_tool = settings.default_search_tool
# Try default tool first
if default_tool == "tavily":
try:
return tavily_search(query, max_results)
except Exception as e:
logger.warning(f"Tavily failed, falling back to Exa: {e}")
try:
return exa_search(query, max_results)
except Exception as exa_error:
logger.error(f"Both Tavily and Exa failed")
raise Exception(f"Search failed - Tavily: {e}, Exa: {exa_error}")
else:
# Default is Exa
try:
return exa_search(query, max_results)
except Exception as e:
logger.warning(f"Exa failed, falling back to Tavily: {e}")
try:
return tavily_search(query, max_results)
except Exception as tavily_error:
logger.error(f"Both Exa and Tavily failed")
raise Exception(f"Search failed - Exa: {e}, Tavily: {tavily_error}")
|