Spaces:
Sleeping
Sleeping
Update wikipedia_searcher.py
Browse files- wikipedia_searcher.py +14 -21
wikipedia_searcher.py
CHANGED
|
@@ -1,30 +1,23 @@
|
|
| 1 |
-
from
|
|
|
|
| 2 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 3 |
|
| 4 |
-
class WikipediaSearcher:
|
| 5 |
-
""
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def search(self, query: str) -> str:
|
| 14 |
-
"""
|
| 15 |
-
Search Wikipedia for a given query and return a summary or snippet.
|
| 16 |
-
|
| 17 |
-
Args:
|
| 18 |
-
query (str): The search query string.
|
| 19 |
-
|
| 20 |
-
Returns:
|
| 21 |
-
str: The Wikipedia summary or an error message.
|
| 22 |
-
"""
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
except Exception as e:
|
| 27 |
return f"Error retrieving Wikipedia data: {e}"
|
| 28 |
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
from langchain_community.tools import WikipediaQueryRun
|
| 3 |
from langchain_community.utilities import WikipediaAPIWrapper
|
| 4 |
|
| 5 |
+
class WikipediaSearcher(Tool):
|
| 6 |
+
name = "wikipedia_search"
|
| 7 |
+
description = "Search Wikipedia for factual answers to questions."
|
|
|
|
| 8 |
|
| 9 |
+
def run(self, input_data: dict) -> str:
|
| 10 |
+
query = input_data.get("query", "")
|
| 11 |
+
if not query:
|
| 12 |
+
return "No query provided for Wikipedia search."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
+
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 15 |
+
return wikipedia.run(query)
|
| 16 |
except Exception as e:
|
| 17 |
return f"Error retrieving Wikipedia data: {e}"
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|