Spaces:
Runtime error
Runtime error
Create tools.py
Browse filestools that agent can use for search and respond at user query
tools.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from duckduckgo_search import DDGS
|
| 6 |
+
from smolagents import HfApiModel, tool
|
| 7 |
+
|
| 8 |
+
MEMORY_FILE = "memory.json"
|
| 9 |
+
|
| 10 |
+
def load_memory():
|
| 11 |
+
if os.path.exists(MEMORY_FILE):
|
| 12 |
+
try:
|
| 13 |
+
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
|
| 14 |
+
content = f.read().strip()
|
| 15 |
+
if not content:
|
| 16 |
+
return {}
|
| 17 |
+
return json.loads(content)
|
| 18 |
+
except json.JSONDecodeError:
|
| 19 |
+
return {}
|
| 20 |
+
return {}
|
| 21 |
+
|
| 22 |
+
def save_memory(memory):
|
| 23 |
+
with open(MEMORY_FILE, "w", encoding="utf-8") as f:
|
| 24 |
+
json.dump(memory, f, ensure_ascii=False, indent=2)
|
| 25 |
+
|
| 26 |
+
# Modello di sintesi testo-based
|
| 27 |
+
summarizer_model = HfApiModel(
|
| 28 |
+
model_id="google/flan-t5-base",
|
| 29 |
+
token=os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
def summarize_text(text: str, max_length: int = 150) -> str:
|
| 33 |
+
prompt = f"Riassumi brevemente il seguente testo:\n\n{text}\n\nRiassunto:"
|
| 34 |
+
summary = summarizer_model.generate(prompt=prompt, max_new_tokens=max_length)
|
| 35 |
+
return summary.strip()
|
| 36 |
+
|
| 37 |
+
@tool
|
| 38 |
+
def duckduckgo_search(query: str, max_results: int = 3) -> str:
|
| 39 |
+
results_texts = []
|
| 40 |
+
retry_attempts = 2
|
| 41 |
+
delay_seconds = 5
|
| 42 |
+
|
| 43 |
+
for attempt in range(retry_attempts):
|
| 44 |
+
try:
|
| 45 |
+
with DDGS() as ddgs:
|
| 46 |
+
results_gen = ddgs.text(query, max_results=max_results)
|
| 47 |
+
for i, result in enumerate(results_gen):
|
| 48 |
+
if i >= max_results:
|
| 49 |
+
break
|
| 50 |
+
snippet = result.get('body') or result.get('text') or ''
|
| 51 |
+
results_texts.append(snippet)
|
| 52 |
+
if results_texts:
|
| 53 |
+
return "\n".join(results_texts)
|
| 54 |
+
else:
|
| 55 |
+
return "Nessun risultato DuckDuckGo."
|
| 56 |
+
except Exception as e:
|
| 57 |
+
if "Ratelimit" in str(e) or "429" in str(e):
|
| 58 |
+
if attempt < retry_attempts - 1:
|
| 59 |
+
time.sleep(delay_seconds)
|
| 60 |
+
delay_seconds *= 2 # Backoff esponenziale
|
| 61 |
+
continue
|
| 62 |
+
else:
|
| 63 |
+
return "Errore: superato limite richieste DuckDuckGo. Riprova tra qualche minuto."
|
| 64 |
+
else:
|
| 65 |
+
return f"Errore durante la ricerca DuckDuckGo: {str(e)}"
|
| 66 |
+
return "Errore sconosciuto durante la ricerca DuckDuckGo."
|
| 67 |
+
|
| 68 |
+
@tool
|
| 69 |
+
def wikipedia_search(query: str) -> str:
|
| 70 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query.replace(' ', '_')}"
|
| 71 |
+
headers = {"User-Agent": "MyAgent/1.0 ([email protected])"} # Sostituisci con il tuo indirizzo email
|
| 72 |
+
try:
|
| 73 |
+
resp = requests.get(url, headers=headers, timeout=10)
|
| 74 |
+
if resp.status_code == 200:
|
| 75 |
+
data = resp.json()
|
| 76 |
+
return data.get("extract", "Nessun sommario disponibile.")
|
| 77 |
+
elif resp.status_code == 404:
|
| 78 |
+
return "Pagina Wikipedia non trovata."
|
| 79 |
+
else:
|
| 80 |
+
return f"Errore nella richiesta Wikipedia: {resp.status_code}"
|
| 81 |
+
except requests.RequestException as e:
|
| 82 |
+
return f"Errore nella richiesta Wikipedia: {str(e)}"
|