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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Unified Web Search Service
Supports multiple search providers:
- Tavily (AI-optimized, RAG-ready)
- Serper (Google search)
- Brave Search (Privacy-focused)
- You.com (AI-ready)
"""

import aiohttp
import asyncio
import time
from typing import List, Dict, Optional, Literal
from abc import ABC, abstractmethod
from dataclasses import dataclass
from enum import Enum
import json

from app.config import settings


class SearchProvider(Enum):
    TAVILY = "tavily"
    SERPER = "serper"
    BRAVE = "brave"
    YOUCOM = "youcom"


@dataclass
class SearchResult:
    title: str
    url: str
    snippet: str
    score: float = 0.8
    provider: str = "unknown"


@dataclass
class SearchConfig:
    provider: SearchProvider
    api_key: str
    max_results: int = 5
    timeout: int = 15
    retry_attempts: int = 3
    retry_delay: float = 2.0
    cache_ttl: int = 300  # 5 minutes


class BaseSearchProvider(ABC):
    """Abstract base class for search providers"""

    def __init__(self, config: SearchConfig):
        self.config = config
        self._cache: Dict[str, tuple] = {}

    @abstractmethod
    async def search(self, query: str) -> List[SearchResult]:
        pass

    @abstractmethod
    def _format_results(self, raw_data) -> List[SearchResult]:
        pass

    def _get_cache(self, query: str) -> Optional[List[SearchResult]]:
        if query in self._cache:
            data, timestamp = self._cache[query]
            if time.time() - timestamp < self.config.cache_ttl:
                return data
            del self._cache[query]
        return None

    def _set_cache(self, query: str, results: List[SearchResult]):
        self._cache[query] = (results, time.time())

        # Clean old cache entries
        if len(self._cache) > 100:
            oldest = sorted(self._cache.keys(), key=lambda k: self._cache[k][1])[:10]
            for k in oldest:
                del self._cache[k]

    async def _make_request(
        self, url: str, params: Dict = None, headers: Dict = None, method: str = "GET"
    ) -> Dict:
        """Make HTTP request with retry logic"""

        for attempt in range(self.config.retry_attempts):
            try:
                timeout = aiohttp.ClientTimeout(total=self.config.timeout)

                async with aiohttp.ClientSession(timeout=timeout) as session:
                    if method == "GET":
                        async with session.get(
                            url, params=params, headers=headers
                        ) as response:
                            if response.status == 429:
                                await asyncio.sleep(
                                    self.config.retry_delay * (attempt + 1)
                                )
                                continue
                            if response.status != 200:
                                return {}
                            return await response.json()
                    else:
                        async with session.post(
                            url, json=params, headers=headers
                        ) as response:
                            if response.status == 429:
                                await asyncio.sleep(
                                    self.config.retry_delay * (attempt + 1)
                                )
                                continue
                            if response.status != 200:
                                return {}
                            return await response.json()

            except Exception as e:
                if attempt < self.config.retry_attempts - 1:
                    await asyncio.sleep(self.config.retry_delay)
                continue

        return {}


class TavilySearchProvider(BaseSearchProvider):
    """Tavily AI Search - Optimized for RAG and AI applications"""

    def __init__(self, api_key: str, max_results: int = 5):
        config = SearchConfig(
            provider=SearchProvider.TAVILY, api_key=api_key, max_results=max_results
        )
        super().__init__(config)
        self.base_url = "https://api.tavily.com/search"

    async def search(self, query: str) -> List[SearchResult]:
        # Check cache first
        cached = self._get_cache(query)
        if cached:
            return cached

        payload = {
            "api_key": self.config.api_key,
            "query": query,
            "search_depth": "advanced",
            "max_results": self.config.max_results,
            "include_answer": True,
            "include_raw_content": False,
            "include_images": False,
        }

        data = await self._make_request(self.base_url, params=payload, method="POST")

        results = self._format_results(data)
        self._set_cache(query, results)
        return results

    def _format_results(self, data: Dict) -> List[SearchResult]:
        results = []

        search_results = data.get("results", [])

        for i, result in enumerate(search_results[: self.config.max_results]):
            # Scale scores to be comparable with PDF cosine similarity (0.3-0.7)
            # Tavily already provides relevance scores, so we scale them
            tavily_score = result.get("score", 0.7)
            scaled_score = 0.3 + (tavily_score * 0.4)  # Maps 0-1 to 0.3-0.7

            results.append(
                SearchResult(
                    title=result.get("title", ""),
                    url=result.get("url", ""),
                    snippet=result.get("content", ""),
                    score=scaled_score - (i * 0.05),
                    provider="tavily",
                )
            )

        return results


class SerperSearchProvider(BaseSearchProvider):
    """Serper.dev - Google Search API"""

    def __init__(self, api_key: str, max_results: int = 5):
        config = SearchConfig(
            provider=SearchProvider.SERPER, api_key=api_key, max_results=max_results
        )
        super().__init__(config)
        self.base_url = "https://serpapi.com/search"

    async def search(self, query: str) -> List[SearchResult]:
        cached = self._get_cache(query)
        if cached:
            return cached

        params = {
            "engine": "google",
            "q": query,
            "api_key": self.config.api_key,
            "num": self.config.max_results,
        }

        data = await self._make_request(self.base_url, params=params)

        results = self._format_results(data)
        self._set_cache(query, results)
        return results

    def _format_results(self, data: Dict) -> List[SearchResult]:
        results = []

        organic_results = data.get("organic_results", [])

        # Scale scores to be comparable with PDF cosine similarity (0.3-0.7)
        for i, result in enumerate(organic_results[: self.config.max_results]):
            base_score = 0.65 - (i * 0.05)  # Start at 0.65, decrease by 0.05
            results.append(
                SearchResult(
                    title=result.get("title", ""),
                    url=result.get("link", ""),
                    snippet=result.get("snippet", ""),
                    score=base_score,
                    provider="serper",
                )
            )

        return results


class BraveSearchProvider(BaseSearchProvider):
    """Brave Search API - Privacy-focused"""

    def __init__(self, api_key: str, max_results: int = 5):
        config = SearchConfig(
            provider=SearchProvider.BRAVE, api_key=api_key, max_results=max_results
        )
        super().__init__(config)
        self.base_url = "https://api.search.brave.com/res/v1/web/search"

    async def search(self, query: str) -> List[SearchResult]:
        cached = self._get_cache(query)
        if cached:
            return cached

        headers = {
            "Accept": "application/json",
            "X-Subscription-Token": self.config.api_key,
        }

        params = {"q": query, "count": self.config.max_results}

        data = await self._make_request(self.base_url, params=params, headers=headers)

        results = self._format_results(data)
        self._set_cache(query, results)
        return results

    def _format_results(self, data: Dict) -> List[SearchResult]:
        results = []

        web_results = data.get("web", {}).get("results", [])

        # Scale scores to be comparable with PDF cosine similarity (0.3-0.7)
        for i, result in enumerate(web_results[: self.config.max_results]):
            base_score = 0.65 - (i * 0.05)  # Start at 0.65, decrease by 0.05
            results.append(
                SearchResult(
                    title=result.get("title", ""),
                    url=result.get("url", ""),
                    snippet=result.get("description", ""),
                    score=base_score,
                    provider="brave",
                )
            )

        return results


class YouComSearchProvider(BaseSearchProvider):
    """You.com - AI-Optimized Search"""

    def __init__(self, api_key: str, max_results: int = 5):
        config = SearchConfig(
            provider=SearchProvider.YOUCOM, api_key=api_key, max_results=max_results
        )
        super().__init__(config)
        self.base_url = "https://api.you.com/search"

    async def search(self, query: str) -> List[SearchResult]:
        cached = self._get_cache(query)
        if cached:
            return cached

        headers = {"Authorization": f"Bearer {self.config.api_key}"}

        params = {"query": query, "num": self.config.max_results}

        data = await self._make_request(self.base_url, params=params, headers=headers)

        results = self._format_results(data)
        self._set_cache(query, results)
        return results

    def _format_results(self, data: Dict) -> List[SearchResult]:
        results = []

        search_results = data.get("results", [])

        # Scale scores to be comparable with PDF cosine similarity (0.3-0.7)
        for i, result in enumerate(search_results[: self.config.max_results]):
            base_score = 0.65 - (i * 0.05)  # Start at 0.65, decrease by 0.05
            results.append(
                SearchResult(
                    title=result.get("title", ""),
                    url=result.get("url", ""),
                    snippet=result.get("snippet", ""),
                    score=base_score,
                    provider="youcom",
                )
            )

        return results


class WebSearchService:
    """
    Unified web search service with provider selection
    """

    def __init__(self):
        # Default to Tavily (RAG-optimized)
        self.default_provider = SearchProvider.TAVILY
        self._providers: Dict[SearchProvider, BaseSearchProvider] = {}
        self._initialize_providers()

    def _initialize_providers(self):
        """Initialize available search providers"""

        # Tavily (Recommended for RAG)
        tavily_key = getattr(settings, "TAVILY_API_KEY", None) or getattr(
            settings, "SERPER_API_KEY", None
        )
        if tavily_key:
            self._providers[SearchProvider.TAVILY] = TavilySearchProvider(
                tavily_key, max_results=5
            )

        # Serper
        serper_key = settings.SERPER_API_KEY
        if serper_key:
            self._providers[SearchProvider.SERPER] = SerperSearchProvider(
                serper_key, max_results=5
            )

    def set_provider(self, provider: SearchProvider):
        """Change the active search provider"""
        if provider in self._providers:
            self.default_provider = provider

    async def search(
        self,
        query: str,
        max_results: int = 5,
        provider: Optional[SearchProvider] = None,
    ) -> List[Dict]:
        """
        Search using specified or default provider

        Args:
            query: Search query
            max_results: Maximum number of results
            provider: Specific provider to use (optional)

        Returns:
            List of search results
        """
        search_provider = provider or self.default_provider

        if search_provider not in self._providers:
            # Fallback to any available provider
            if self._providers:
                search_provider = list(self._providers.keys())[0]
            else:
                return []

        provider = self._providers[search_provider]
        results = await provider.search(query)

        # Convert to dict format
        return [
            {
                "title": r.title,
                "url": r.url,
                "snippet": r.snippet,
                "score": r.score,
            }
            for r in results
        ]

    def get_available_providers(self) -> List[str]:
        """Get list of available providers"""
        return [p.value for p in self._providers.keys()]

    def get_current_provider(self) -> str:
        """Get current provider"""
        return self.default_provider.value


# Factory function to create service
def create_web_search_service(provider: str = "tavily") -> WebSearchService:
    """Create web search service with specified provider"""
    service = WebSearchService()

    provider_map = {
        "tavily": SearchProvider.TAVILY,
        "serper": SearchProvider.SERPER,
        "brave": SearchProvider.BRAVE,
        "youcom": SearchProvider.YOUCOM,
    }

    if provider.lower() in provider_map:
        service.set_provider(provider_map[provider.lower()])

    return service


# Default instance
web_search_service = WebSearchService()