Source code for langchain_community.utilities.wikipedia
"""Util that calls Wikipedia."""importloggingfromtypingimportAny,Dict,Iterator,List,Optionalfromlangchain_core.documentsimportDocumentfrompydanticimportBaseModel,model_validatorlogger=logging.getLogger(__name__)WIKIPEDIA_MAX_QUERY_LENGTH=300
[docs]classWikipediaAPIWrapper(BaseModel):"""Wrapper around WikipediaAPI. To use, you should have the ``wikipedia`` python package installed. This wrapper will use the Wikipedia API to conduct searches and fetch page summaries. By default, it will return the page summaries of the top-k results. It limits the Document content by doc_content_chars_max. """wiki_client:Any#: :meta private:top_k_results:int=3lang:str="en"load_all_available_meta:bool=Falsedoc_content_chars_max:int=4000@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:Dict)->Any:"""Validate that the python package exists in environment."""try:importwikipedialang=values.get("lang","en")wikipedia.set_lang(lang)values["wiki_client"]=wikipediaexceptImportError:raiseImportError("Could not import wikipedia python package. ""Please install it with `pip install wikipedia`.")returnvalues
[docs]defrun(self,query:str)->str:"""Run Wikipedia search and get page summaries."""page_titles=self.wiki_client.search(query[:WIKIPEDIA_MAX_QUERY_LENGTH],results=self.top_k_results)summaries=[]forpage_titleinpage_titles[:self.top_k_results]:ifwiki_page:=self._fetch_page(page_title):ifsummary:=self._formatted_page_summary(page_title,wiki_page):summaries.append(summary)ifnotsummaries:return"No good Wikipedia Search Result was found"return"\n\n".join(summaries)[:self.doc_content_chars_max]
[docs]defload(self,query:str)->List[Document]:""" Run Wikipedia search and get the article text plus the meta information. See Returns: a list of documents. """returnlist(self.lazy_load(query))
[docs]deflazy_load(self,query:str)->Iterator[Document]:""" Run Wikipedia search and get the article text plus the meta information. See Returns: a list of documents. """page_titles=self.wiki_client.search(query[:WIKIPEDIA_MAX_QUERY_LENGTH],results=self.top_k_results)forpage_titleinpage_titles[:self.top_k_results]:ifwiki_page:=self._fetch_page(page_title):ifdoc:=self._page_to_document(page_title,wiki_page):yielddoc