Spaces:
Sleeping
Sleeping
web_search update
Browse files
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
|
|
|
|
|
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
|
@@ -37,6 +39,67 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"Erreur pour le fuseau '{timezone}' : {str(e)}"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# ==== Instanciation des tools existants ====
|
| 42 |
|
|
|
|
| 1 |
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
+
from typing import Any, Optional
|
| 3 |
+
from smolagents.tools import Tool
|
| 4 |
import datetime
|
| 5 |
import pytz
|
| 6 |
import yaml
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
return f"Erreur pour le fuseau '{timezone}' : {str(e)}"
|
| 41 |
|
| 42 |
+
class DuckDuckGoSearchTool(Tool):
|
| 43 |
+
name = "web_search"
|
| 44 |
+
description = (
|
| 45 |
+
"Effectue une recherche web DuckDuckGo et renvoie un court résumé en français "
|
| 46 |
+
"des principaux résultats."
|
| 47 |
+
)
|
| 48 |
+
inputs = {
|
| 49 |
+
"query": {
|
| 50 |
+
"type": "string",
|
| 51 |
+
"description": "La requête de recherche à effectuer.",
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
output_type = "string"
|
| 55 |
+
|
| 56 |
+
def __init__(self, max_results: int = 5, **kwargs):
|
| 57 |
+
super().__init__()
|
| 58 |
+
self.max_results = max_results
|
| 59 |
+
try:
|
| 60 |
+
from duckduckgo_search import DDGS
|
| 61 |
+
except ImportError as e:
|
| 62 |
+
raise ImportError(
|
| 63 |
+
"Vous devez installer le paquet `duckduckgo-search` pour utiliser ce tool "
|
| 64 |
+
"(ex: `pip install duckduckgo-search`)."
|
| 65 |
+
) from e
|
| 66 |
+
self.ddgs = DDGS(**kwargs)
|
| 67 |
+
|
| 68 |
+
def forward(self, query: str) -> str:
|
| 69 |
+
# Récupération des résultats bruts
|
| 70 |
+
results = self.ddgs.text(query, max_results=self.max_results)
|
| 71 |
+
|
| 72 |
+
# Aucun résultat trouvé → message clair plutôt qu'exception
|
| 73 |
+
if not results:
|
| 74 |
+
return (
|
| 75 |
+
f'Aucun résultat trouvé pour la requête "{query}". '
|
| 76 |
+
"Essaie une formulation plus courte ou plus générale."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# On ne garde que les premiers résultats les plus utiles
|
| 80 |
+
top_results = results[:3]
|
| 81 |
+
|
| 82 |
+
# Construction d'un résumé lisible en français
|
| 83 |
+
lines = [f'Voici quelques résultats pour la requête "{query}" :', ""]
|
| 84 |
+
for idx, r in enumerate(top_results, start=1):
|
| 85 |
+
title = r.get("title", "").strip()
|
| 86 |
+
url = r.get("href", "").strip()
|
| 87 |
+
snippet = r.get("body", "").strip()
|
| 88 |
+
|
| 89 |
+
# On coupe les extraits trop longs
|
| 90 |
+
if len(snippet) > 220:
|
| 91 |
+
snippet = snippet[:217].rstrip() + "..."
|
| 92 |
+
|
| 93 |
+
lines.append(f"{idx}. {title or '(titre manquant)'}")
|
| 94 |
+
if url:
|
| 95 |
+
lines.append(f" Lien : {url}")
|
| 96 |
+
if snippet:
|
| 97 |
+
lines.append(f" Résumé : {snippet}")
|
| 98 |
+
lines.append("") # ligne vide entre les résultats
|
| 99 |
+
|
| 100 |
+
return "\n".join(lines).strip()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
|
| 104 |
# ==== Instanciation des tools existants ====
|
| 105 |
|