dlaima commited on
Commit
e27578f
·
verified ·
1 Parent(s): e42df9e

Update wikipedia_searcher.py

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