Spaces:
Running
Running
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -6,6 +6,14 @@ retrieval. They are intentionally simple and return concise text.
|
|
| 6 |
"""
|
| 7 |
from typing import Optional
|
| 8 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
| 11 |
def duckduckgo_search(query: str, region: str = "wt-wt") -> str:
|
|
@@ -38,6 +46,19 @@ def duckduckgo_search(query: str, region: str = "wt-wt") -> str:
|
|
| 38 |
return f"(duckduckgo error) {e}"
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def wikipedia_search(query: str) -> str:
|
| 42 |
"""Search Wikipedia for a query and return the page summary for the top hit.
|
| 43 |
|
|
@@ -47,17 +68,54 @@ def wikipedia_search(query: str) -> str:
|
|
| 47 |
if not query:
|
| 48 |
return "(no query provided)"
|
| 49 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
search_params = {"action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": 1}
|
| 51 |
-
r = requests.get("https://en.wikipedia.org/w/api.php", params=search_params, timeout=10)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
r.raise_for_status()
|
| 53 |
sr = r.json().get("query", {}).get("search", [])
|
| 54 |
if not sr:
|
| 55 |
return "(no wiki result)"
|
| 56 |
title = sr[0].get("title")
|
| 57 |
# fetch summary
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
summary_resp.raise_for_status()
|
| 60 |
summary = summary_resp.json()
|
| 61 |
return summary.get("extract") or summary.get("description") or "(no summary)"
|
| 62 |
except Exception as e:
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
from typing import Optional
|
| 8 |
import requests
|
| 9 |
+
import logging
|
| 10 |
+
|
| 11 |
+
logger = logging.getLogger(__name__)
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
from smolagents import tool
|
| 15 |
+
except Exception:
|
| 16 |
+
tool = None
|
| 17 |
|
| 18 |
|
| 19 |
def duckduckgo_search(query: str, region: str = "wt-wt") -> str:
|
|
|
|
| 46 |
return f"(duckduckgo error) {e}"
|
| 47 |
|
| 48 |
|
| 49 |
+
# Export a smolagents-wrapped tool if available so the agent's interpreter
|
| 50 |
+
# can call `duckduckgo_search` directly when executing parsed code.
|
| 51 |
+
if tool is not None:
|
| 52 |
+
try:
|
| 53 |
+
@tool
|
| 54 |
+
def duckduckgo_search_tool(query: str, region: str = "wt-wt") -> str:
|
| 55 |
+
return duckduckgo_search(query, region)
|
| 56 |
+
except Exception:
|
| 57 |
+
duckduckgo_search_tool = duckduckgo_search
|
| 58 |
+
else:
|
| 59 |
+
duckduckgo_search_tool = duckduckgo_search
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def wikipedia_search(query: str) -> str:
|
| 63 |
"""Search Wikipedia for a query and return the page summary for the top hit.
|
| 64 |
|
|
|
|
| 68 |
if not query:
|
| 69 |
return "(no query provided)"
|
| 70 |
try:
|
| 71 |
+
# Use browser-like headers to reduce chance of 403/blocks
|
| 72 |
+
headers = {
|
| 73 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0 Safari/537.36",
|
| 74 |
+
"Accept": "application/json, text/javascript, */*; q=0.01",
|
| 75 |
+
}
|
| 76 |
search_params = {"action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": 1}
|
| 77 |
+
r = requests.get("https://en.wikipedia.org/w/api.php", params=search_params, timeout=10, headers=headers)
|
| 78 |
+
# If API returns 403 or other non-200, fallback to DuckDuckGo
|
| 79 |
+
if r.status_code != 200:
|
| 80 |
+
logger.warning("wikipedia_search API returned status %s, falling back to DuckDuckGo", r.status_code)
|
| 81 |
+
return duckduckgo_search(query)
|
| 82 |
r.raise_for_status()
|
| 83 |
sr = r.json().get("query", {}).get("search", [])
|
| 84 |
if not sr:
|
| 85 |
return "(no wiki result)"
|
| 86 |
title = sr[0].get("title")
|
| 87 |
# fetch summary
|
| 88 |
+
summary_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{requests.utils.requote_uri(title)}"
|
| 89 |
+
summary_resp = requests.get(summary_url, timeout=10, headers=headers)
|
| 90 |
+
if summary_resp.status_code != 200:
|
| 91 |
+
logger.warning("wikipedia summary endpoint returned status %s for %s, falling back to DuckDuckGo", summary_resp.status_code, title)
|
| 92 |
+
return duckduckgo_search(query)
|
| 93 |
summary_resp.raise_for_status()
|
| 94 |
summary = summary_resp.json()
|
| 95 |
return summary.get("extract") or summary.get("description") or "(no summary)"
|
| 96 |
except Exception as e:
|
| 97 |
+
logger.warning("wikipedia_search failed: %s; falling back to DuckDuckGo", e)
|
| 98 |
+
try:
|
| 99 |
+
return duckduckgo_search(query)
|
| 100 |
+
except Exception:
|
| 101 |
+
return f"(wikipedia error) {e}"
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# Export a smolagents-wrapped tool if available so the agent's interpreter
|
| 105 |
+
# can call `wikipedia_search` directly when executing parsed code.
|
| 106 |
+
if tool is not None:
|
| 107 |
+
try:
|
| 108 |
+
@tool
|
| 109 |
+
def wikipedia_search_tool(query: str) -> str:
|
| 110 |
+
return wikipedia_search(query)
|
| 111 |
+
except Exception:
|
| 112 |
+
wikipedia_search_tool = wikipedia_search
|
| 113 |
+
else:
|
| 114 |
+
wikipedia_search_tool = wikipedia_search
|
| 115 |
+
|
| 116 |
+
__all__ = [
|
| 117 |
+
"duckduckgo_search",
|
| 118 |
+
"duckduckgo_search_tool",
|
| 119 |
+
"wikipedia_search",
|
| 120 |
+
"wikipedia_search_tool",
|
| 121 |
+
]
|