File size: 912 Bytes
fb58427
 
 
 
593012b
 
 
 
 
 
fb58427
 
 
593012b
 
 
 
 
 
 
 
 
ba3b3ff
 
 
 
 
fb58427
 
 
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
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}"