Spaces:
Running
Running
Download app/tools/web_tool.py from nvikou/bi-multiagent-hub: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/spaces/nvikou/bi-multiagent-hub/resolve/main/app/tools/web_tool.py
- Command line
-
hf download hf://spaces/nvikou/bi-multiagent-hub/app/tools/web_tool.py
-
curl -L -o web_tool.py https://huggingface.co/spaces/nvikou/bi-multiagent-hub/resolve/main/app/tools/web_tool.py
3.08 kB
| """Recherche web légère (DuckDuckGo), sans clé API tierce.""" | |
| from __future__ import annotations | |
| import logging | |
| import time | |
| from dataclasses import dataclass, field | |
| from app.cache import make_cache_key, web_cache | |
| from app.config import settings | |
| logger = logging.getLogger(__name__) | |
| class WebHit: | |
| title: str | |
| url: str | |
| snippet: str | |
| class WebSearchResult: | |
| query: str | |
| hits: list[WebHit] = field(default_factory=list) | |
| duration_ms: float = 0.0 | |
| error: str | None = None | |
| def search_web(query: str, top_k: int | None = None) -> WebSearchResult: | |
| k = top_k or settings.web_top_k | |
| cache_key = make_cache_key("web", query, k) | |
| cached = web_cache.get(cache_key) | |
| if cached is not None: | |
| return WebSearchResult( | |
| query=cached["query"], | |
| hits=[WebHit(**h) for h in cached["hits"]], | |
| duration_ms=cached["duration_ms"], | |
| error=cached.get("error"), | |
| ) | |
| start = time.perf_counter() | |
| if settings.effective_demo_mode: | |
| hits = [ | |
| WebHit( | |
| title="Contexte marché (démo)", | |
| url="https://example.com/demo-market", | |
| snippet=( | |
| "Résultat web simulé pour le portfolio — DEMO_MODE. " | |
| f"Requête : {query[:120]}" | |
| ), | |
| ) | |
| ] | |
| duration_ms = (time.perf_counter() - start) * 1000 | |
| payload = { | |
| "query": query, | |
| "hits": [h.__dict__ for h in hits], | |
| "duration_ms": duration_ms, | |
| "error": None, | |
| } | |
| web_cache.set(cache_key, payload) | |
| return WebSearchResult(query=query, hits=hits, duration_ms=duration_ms) | |
| try: | |
| from ddgs import DDGS | |
| except ImportError: # pragma: no cover | |
| try: | |
| from duckduckgo_search import DDGS # type: ignore | |
| except ImportError as exc: | |
| return WebSearchResult( | |
| query=query, | |
| hits=[], | |
| duration_ms=(time.perf_counter() - start) * 1000, | |
| error=f"Package de recherche web indisponible : {exc}", | |
| ) | |
| hits: list[WebHit] = [] | |
| error: str | None = None | |
| try: | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=k)) | |
| for item in results: | |
| hits.append( | |
| WebHit( | |
| title=item.get("title") or "", | |
| url=item.get("href") or item.get("link") or "", | |
| snippet=item.get("body") or item.get("snippet") or "", | |
| ) | |
| ) | |
| except Exception as exc: | |
| logger.warning("Recherche web échouée : %s", exc) | |
| error = str(exc) | |
| duration_ms = (time.perf_counter() - start) * 1000 | |
| payload = { | |
| "query": query, | |
| "hits": [h.__dict__ for h in hits], | |
| "duration_ms": duration_ms, | |
| "error": error, | |
| } | |
| web_cache.set(cache_key, payload) | |
| return WebSearchResult( | |
| query=query, hits=hits, duration_ms=duration_ms, error=error | |
| ) | |