Spaces:
Sleeping
Sleeping
Pycrolis
commited on
Commit
·
508a421
1
Parent(s):
ee92f11
feat(tool): add Wikipedia search tool
Browse files- ShrewdAgent.py +2 -0
- requirements.txt +2 -1
- tools/wikipedia_search.py +41 -0
ShrewdAgent.py
CHANGED
@@ -15,6 +15,7 @@ from pydantic import SecretStr
|
|
15 |
|
16 |
from tools.produce_classifier import produce_classifier
|
17 |
from tools.web_page_information_extractor import web_page_information_extractor
|
|
|
18 |
from tools.youtube_transcript import youtube_transcript
|
19 |
|
20 |
|
@@ -39,6 +40,7 @@ class ShrewdAgent:
|
|
39 |
def __init__(self):
|
40 |
self.tools = [
|
41 |
TavilySearch(),
|
|
|
42 |
web_page_information_extractor,
|
43 |
youtube_transcript,
|
44 |
produce_classifier,
|
|
|
15 |
|
16 |
from tools.produce_classifier import produce_classifier
|
17 |
from tools.web_page_information_extractor import web_page_information_extractor
|
18 |
+
from tools.wikipedia_search import wikipedia_search
|
19 |
from tools.youtube_transcript import youtube_transcript
|
20 |
|
21 |
|
|
|
40 |
def __init__(self):
|
41 |
self.tools = [
|
42 |
TavilySearch(),
|
43 |
+
wikipedia_search,
|
44 |
web_page_information_extractor,
|
45 |
youtube_transcript,
|
46 |
produce_classifier,
|
requirements.txt
CHANGED
@@ -9,4 +9,5 @@ pydantic~=2.11.4
|
|
9 |
html2text~=2025.4.15
|
10 |
beautifulsoup4~=4.13.4
|
11 |
readability-lxml~=0.8.4.1
|
12 |
-
youtube-transcript-api~=1.0.3
|
|
|
|
9 |
html2text~=2025.4.15
|
10 |
beautifulsoup4~=4.13.4
|
11 |
readability-lxml~=0.8.4.1
|
12 |
+
youtube-transcript-api~=1.0.3
|
13 |
+
wikipedia~=1.4.0
|
tools/wikipedia_search.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import wikipedia
|
2 |
+
from langchain_core.tools import tool
|
3 |
+
from loguru import logger
|
4 |
+
|
5 |
+
|
6 |
+
@tool("wikipedia_search_tool", parse_docstring=True)
|
7 |
+
def wikipedia_search(query: str) -> str:
|
8 |
+
"""
|
9 |
+
Searches Wikipedia for the given query.
|
10 |
+
|
11 |
+
Args:
|
12 |
+
query (str): The search query to look up on Wikipedia.
|
13 |
+
|
14 |
+
Returns:
|
15 |
+
str: A formatted string with the search results, page title and url.
|
16 |
+
"""
|
17 |
+
logger.info(f"use wikipedia_search_tool with param: {query}")
|
18 |
+
|
19 |
+
search_results = wikipedia.search(query, results=5)
|
20 |
+
|
21 |
+
if not search_results:
|
22 |
+
return "No results found for the query."
|
23 |
+
|
24 |
+
result_text = ""
|
25 |
+
try:
|
26 |
+
for i, title in enumerate(search_results, 1):
|
27 |
+
page = wikipedia.page(search_results[i - 1], auto_suggest=False)
|
28 |
+
result_text += f"{i}. [{title}]({page.url})\n"
|
29 |
+
|
30 |
+
return result_text
|
31 |
+
|
32 |
+
except wikipedia.DisambiguationError as e:
|
33 |
+
return f"Disambiguation page found. Possible matches:\n{'\n'.join(e.options)}"
|
34 |
+
except wikipedia.PageError as e:
|
35 |
+
return f"Page not found. Try another search term."
|
36 |
+
except Exception as e:
|
37 |
+
return f"An error occurred: {str(e)}"
|
38 |
+
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
print(wikipedia_search.invoke("Mercedes Sosa discography"))
|