Spaces:
Sleeping
Sleeping
Update tools/WikipediaTool.py
Browse files- tools/WikipediaTool.py +16 -4
tools/WikipediaTool.py
CHANGED
|
@@ -10,7 +10,19 @@ class LocalWikipediaTool(Tool):
|
|
| 10 |
|
| 11 |
def forward(self, query: str) -> str:
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def forward(self, query: str) -> str:
|
| 12 |
try:
|
| 13 |
+
# search for related pages
|
| 14 |
+
results = wikipedia.search(query)
|
| 15 |
+
|
| 16 |
+
if not results:
|
| 17 |
+
return "No Wikipedia results found."
|
| 18 |
+
|
| 19 |
+
# pick first result
|
| 20 |
+
page_title = results[0]
|
| 21 |
+
|
| 22 |
+
# get a short summary (2–3 sentences)
|
| 23 |
+
summary = wikipedia.summary(page_title, sentences=3)
|
| 24 |
+
|
| 25 |
+
return f"Title: {page_title}\nSummary: {summary}"
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Wikipedia error: {e}"
|