dlaima commited on
Commit
e427836
·
verified ·
1 Parent(s): dc7f623

Update wikipedia_searcher.py

Browse files
Files changed (1) hide show
  1. wikipedia_searcher.py +14 -21
wikipedia_searcher.py CHANGED
@@ -1,30 +1,23 @@
1
- from langchain_community.tools import WikipediaQueryRun
 
2
  from langchain_community.utilities import WikipediaAPIWrapper
3
 
4
- class WikipediaSearcher:
5
- """
6
- A simple wrapper class to query Wikipedia and retrieve summaries using LangChain's Wikipedia utilities.
7
- """
8
 
9
- def __init__(self) -> None:
10
- # Initialize the Wikipedia query runner with the API wrapper
11
- self.wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
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
- result = self.wikipedia.run(query)
25
- return result
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
+