Spaces:
Sleeping
Sleeping
from langchain_community.tools import WikipediaQueryRun | |
from langchain_community.utilities import WikipediaAPIWrapper | |
class WikipediaSearcher: | |
""" | |
A simple wrapper class to query Wikipedia and retrieve summaries using LangChain's Wikipedia utilities. | |
""" | |
def __init__(self) -> None: | |
# Initialize the Wikipedia query runner with the API wrapper | |
self.wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper()) | |
def search(self, query: str) -> str: | |
""" | |
Search Wikipedia for a given query and return a summary or snippet. | |
Args: | |
query (str): The search query string. | |
Returns: | |
str: The Wikipedia summary or an error message. | |
""" | |
try: | |
result = self.wikipedia.run(query) | |
return result | |
except Exception as e: | |
return f"Error retrieving Wikipedia data: {e}" | |