Spaces:
Sleeping
Sleeping
Update tools/wikipedia_search.py
Browse files- tools/wikipedia_search.py +12 -52
tools/wikipedia_search.py
CHANGED
|
@@ -1,33 +1,21 @@
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
def wikipedia_search(query: str) -> str:
|
| 4 |
-
"""
|
| 5 |
-
Search Wikipedia and return the full page content (up to 200,000 chars) of the best matching article.
|
| 6 |
-
If the page contains a 'Discography' section, it will try to keep it intact.
|
| 7 |
-
"""
|
| 8 |
try:
|
| 9 |
base_url = "https://en.wikipedia.org/w/api.php"
|
| 10 |
headers = {"User-Agent": "langgraph-gaia-agent/1.0"}
|
| 11 |
|
| 12 |
-
# 1.
|
| 13 |
direct_title = query.replace(" ", "_")
|
| 14 |
-
check_params = {
|
| 15 |
-
"action": "query",
|
| 16 |
-
"titles": direct_title,
|
| 17 |
-
"format": "json"
|
| 18 |
-
}
|
| 19 |
r = requests.get(base_url, params=check_params, headers=headers, timeout=10)
|
| 20 |
data = r.json()
|
| 21 |
pages = data.get("query", {}).get("pages", {})
|
| 22 |
if any(pid != "-1" for pid in pages):
|
| 23 |
# 精确页面存在
|
| 24 |
extract_params = {
|
| 25 |
-
"action": "query",
|
| 26 |
-
"
|
| 27 |
-
"explaintext": True,
|
| 28 |
-
"redirects": 1,
|
| 29 |
-
"titles": direct_title,
|
| 30 |
-
"format": "json",
|
| 31 |
}
|
| 32 |
r = requests.get(base_url, params=extract_params, headers=headers, timeout=10)
|
| 33 |
data = r.json()
|
|
@@ -35,28 +23,12 @@ def wikipedia_search(query: str) -> str:
|
|
| 35 |
extract = page.get("extract", "")
|
| 36 |
title = page.get("title", direct_title)
|
| 37 |
url = "https://en.wikipedia.org/wiki/" + title.replace(" ", "_")
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if len(extract) > max_len:
|
| 41 |
-
# 尝试保留 Discography 章节
|
| 42 |
-
discog_idx = extract.lower().find("discography")
|
| 43 |
-
if discog_idx != -1:
|
| 44 |
-
# 保留从 Discography 前 5000 到文末(或后 150000)
|
| 45 |
-
start = max(0, discog_idx - 5000)
|
| 46 |
-
end = min(len(extract), discog_idx + 150000)
|
| 47 |
-
extract = extract[start:end] + "\n...[truncated from Wikipedia full page, but Discography section preserved]"
|
| 48 |
-
else:
|
| 49 |
-
extract = extract[:max_len] + "\n...[truncated]"
|
| 50 |
return f"# {title}\nURL: {url}\n\n{extract}"
|
| 51 |
|
| 52 |
-
# 2.
|
| 53 |
-
search_params = {
|
| 54 |
-
"action": "query",
|
| 55 |
-
"list": "search",
|
| 56 |
-
"srsearch": query,
|
| 57 |
-
"srlimit": 1,
|
| 58 |
-
"format": "json",
|
| 59 |
-
}
|
| 60 |
r = requests.get(base_url, params=search_params, headers=headers, timeout=10)
|
| 61 |
data = r.json()
|
| 62 |
hits = data.get("query", {}).get("search", [])
|
|
@@ -64,29 +36,17 @@ def wikipedia_search(query: str) -> str:
|
|
| 64 |
return f"No Wikipedia article found for '{query}'."
|
| 65 |
title = hits[0]["title"]
|
| 66 |
|
| 67 |
-
# 3. 获取完整页面内容
|
| 68 |
extract_params = {
|
| 69 |
-
"action": "query",
|
| 70 |
-
"
|
| 71 |
-
"explaintext": True,
|
| 72 |
-
"redirects": 1,
|
| 73 |
-
"titles": title,
|
| 74 |
-
"format": "json",
|
| 75 |
}
|
| 76 |
r = requests.get(base_url, params=extract_params, headers=headers, timeout=10)
|
| 77 |
data = r.json()
|
| 78 |
for page in data.get("query", {}).get("pages", {}).values():
|
| 79 |
extract = page.get("extract", "")
|
| 80 |
url = "https://en.wikipedia.org/wiki/" + title.replace(" ", "_")
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
discog_idx = extract.lower().find("discography")
|
| 84 |
-
if discog_idx != -1:
|
| 85 |
-
start = max(0, discog_idx - 5000)
|
| 86 |
-
end = min(len(extract), discog_idx + 150000)
|
| 87 |
-
extract = extract[start:end] + "\n...[truncated]"
|
| 88 |
-
else:
|
| 89 |
-
extract = extract[:max_len] + "\n...[truncated]"
|
| 90 |
return f"# {title}\nURL: {url}\n\n{extract}"
|
| 91 |
|
| 92 |
return f"Could not retrieve content for '{query}'."
|
|
|
|
| 1 |
import requests
|
| 2 |
|
| 3 |
def wikipedia_search(query: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
try:
|
| 5 |
base_url = "https://en.wikipedia.org/w/api.php"
|
| 6 |
headers = {"User-Agent": "langgraph-gaia-agent/1.0"}
|
| 7 |
|
| 8 |
+
# 1. 尝试直接作为页面标题访问
|
| 9 |
direct_title = query.replace(" ", "_")
|
| 10 |
+
check_params = {"action": "query", "titles": direct_title, "format": "json"}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
r = requests.get(base_url, params=check_params, headers=headers, timeout=10)
|
| 12 |
data = r.json()
|
| 13 |
pages = data.get("query", {}).get("pages", {})
|
| 14 |
if any(pid != "-1" for pid in pages):
|
| 15 |
# 精确页面存在
|
| 16 |
extract_params = {
|
| 17 |
+
"action": "query", "prop": "extracts", "explaintext": True,
|
| 18 |
+
"redirects": 1, "titles": direct_title, "format": "json"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
}
|
| 20 |
r = requests.get(base_url, params=extract_params, headers=headers, timeout=10)
|
| 21 |
data = r.json()
|
|
|
|
| 23 |
extract = page.get("extract", "")
|
| 24 |
title = page.get("title", direct_title)
|
| 25 |
url = "https://en.wikipedia.org/wiki/" + title.replace(" ", "_")
|
| 26 |
+
if len(extract) > 8000:
|
| 27 |
+
extract = extract[:8000] + "\n...[truncated]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
return f"# {title}\nURL: {url}\n\n{extract}"
|
| 29 |
|
| 30 |
+
# 2. 搜索
|
| 31 |
+
search_params = {"action": "query", "list": "search", "srsearch": query, "srlimit": 1, "format": "json"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
r = requests.get(base_url, params=search_params, headers=headers, timeout=10)
|
| 33 |
data = r.json()
|
| 34 |
hits = data.get("query", {}).get("search", [])
|
|
|
|
| 36 |
return f"No Wikipedia article found for '{query}'."
|
| 37 |
title = hits[0]["title"]
|
| 38 |
|
|
|
|
| 39 |
extract_params = {
|
| 40 |
+
"action": "query", "prop": "extracts", "explaintext": True,
|
| 41 |
+
"redirects": 1, "titles": title, "format": "json"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
r = requests.get(base_url, params=extract_params, headers=headers, timeout=10)
|
| 44 |
data = r.json()
|
| 45 |
for page in data.get("query", {}).get("pages", {}).values():
|
| 46 |
extract = page.get("extract", "")
|
| 47 |
url = "https://en.wikipedia.org/wiki/" + title.replace(" ", "_")
|
| 48 |
+
if len(extract) > 8000:
|
| 49 |
+
extract = extract[:8000] + "\n...[truncated]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return f"# {title}\nURL: {url}\n\n{extract}"
|
| 51 |
|
| 52 |
return f"Could not retrieve content for '{query}'."
|