File size: 894 Bytes
e27578f
fb58427
 
e27578f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba3b3ff
e27578f
ba3b3ff
 
fb58427
 
 
e427836
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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:
            return self.wikipedia.run(query)
        except Exception as e:
            return f"Error retrieving Wikipedia data: {e}"