Spaces:
Sleeping
Sleeping
Rename tools.py to wikipedia_searcher.py
Browse files- tools.py +0 -61
- wikipedia_searcher.py +12 -0
tools.py
DELETED
@@ -1,61 +0,0 @@
|
|
1 |
-
from smolagents import Tool
|
2 |
-
import requests
|
3 |
-
|
4 |
-
def fetch_wikipedia_article(query: str) -> str:
|
5 |
-
url = "https://en.wikipedia.org/w/api.php"
|
6 |
-
params = {
|
7 |
-
"action": "query",
|
8 |
-
"prop": "extracts",
|
9 |
-
"explaintext": True,
|
10 |
-
"format": "json",
|
11 |
-
"titles": query,
|
12 |
-
}
|
13 |
-
response = requests.get(url, params=params)
|
14 |
-
data = response.json()
|
15 |
-
pages = data.get("query", {}).get("pages", {})
|
16 |
-
page = next(iter(pages.values()))
|
17 |
-
return page.get("extract", "No content found for this query.")
|
18 |
-
|
19 |
-
def search_wikipedia(query: str) -> str:
|
20 |
-
url = "https://en.wikipedia.org/w/api.php"
|
21 |
-
params = {
|
22 |
-
"action": "query",
|
23 |
-
"list": "search",
|
24 |
-
"srsearch": query,
|
25 |
-
"format": "json",
|
26 |
-
}
|
27 |
-
response = requests.get(url, params=params)
|
28 |
-
data = response.json()
|
29 |
-
results = data.get("query", {}).get("search", [])
|
30 |
-
if results:
|
31 |
-
return "\n".join([r['title'] for r in results])
|
32 |
-
return "No search results found."
|
33 |
-
|
34 |
-
class WikipediaTool(Tool):
|
35 |
-
name = "WikipediaTool"
|
36 |
-
description = "Fetches plain text from a Wikipedia article. Input should be the article title as a string."
|
37 |
-
inputs = {"query": "string"}
|
38 |
-
output_type = "string"
|
39 |
-
|
40 |
-
def __call__(self, inputs) -> str:
|
41 |
-
if isinstance(inputs, str):
|
42 |
-
inputs = {"query": inputs}
|
43 |
-
elif isinstance(inputs, dict) and "query" not in inputs:
|
44 |
-
inputs = {"query": next(iter(inputs.values()))}
|
45 |
-
return fetch_wikipedia_article(inputs["query"])
|
46 |
-
|
47 |
-
class WikipediaSearchTool(Tool):
|
48 |
-
name = "WikipediaSearchTool"
|
49 |
-
description = "Searches Wikipedia for article titles. Input should be a question or topic as a string."
|
50 |
-
inputs = {"query": "string"}
|
51 |
-
output_type = "string"
|
52 |
-
|
53 |
-
def __call__(self, inputs) -> str:
|
54 |
-
if isinstance(inputs, str):
|
55 |
-
inputs = {"query": inputs}
|
56 |
-
elif isinstance(inputs, dict) and "query" not in inputs:
|
57 |
-
inputs = {"query": next(iter(inputs.values()))}
|
58 |
-
return search_wikipedia(inputs["query"])
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wikipedia_searcher.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.tools import WikipediaQueryRun
|
2 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
3 |
+
|
4 |
+
class WikipediaSearcher:
|
5 |
+
def __init__(self):
|
6 |
+
self.wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
7 |
+
|
8 |
+
def search(self, query: str) -> str:
|
9 |
+
return self.wikipedia.run(query)
|
10 |
+
|
11 |
+
|
12 |
+
|