File size: 1,093 Bytes
1f88fc5
 
daca3ec
526ce64
1f88fc5
 
daca3ec
1f88fc5
526ce64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_community.retrievers import BM25Retriever
from langchain.tools import Tool
from langchain_community.tools import DuckDuckGoSearchRun
langchain_community.document_loaders.wikipedia.WikipediaLoader


search_tool = DuckDuckGoSearchRun()

def wiki_loader(query: str, lang: str='en', load_max_docs: int=2):
    """
    Fetches content from Wikipedia based on a given query.

    Parameters:
    - query (str): The search query for Wikipedia.
    - lang (str): The language of the Wikipedia to search in. Default is 'en'.
    - load_max_docs (int): The maximum number of documents to load. Default is 2.

    Returns:
    - list: A list of documents containing the fetched Wikipedia content.
    """
    try:
        # Initialize the WikipediaLoader with the given query, language, and max documents
        loader = WikipediaLoader(query=query, lang=lang, load_max_docs=load_max_docs)
        # Load the documents
        documents = loader.load()
        print(documents)
        return documents
    except Exception as e:
        print(f"An error occurred: {e}")
        return []