U
    h                     @   sf   d Z ddlZddlmZmZmZmZmZ ddlm	Z	 ddl
mZmZ eeZdZG dd deZdS )	zUtil that calls Wikipedia.    N)AnyDictIteratorListOptional)Document)	BaseModelroot_validatori,  c                   @   s   e Zd ZU dZeed< dZeed< dZe	ed< dZ
eed< d	Zeed
< eddeedddZe	e	dddZee	eee	 dddZe	eedddZe	ee	 dddZe	ee dddZe	ee dddZdS ) WikipediaAPIWrapperaM  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   top_k_resultsenlangFload_all_available_metai  doc_content_chars_maxT)pre)valuesreturnc                 C   sL   z*ddl }|dd}|| ||d< W n tk
rF   tdY nX |S )z7Validate that the python package exists in environment.r   Nr   r   r   zZCould not import wikipedia python package. Please install it with `pip install wikipedia`.)	wikipediagetZset_langImportError)clsr   r   r    r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/utilities/wikipedia.pyvalidate_environment   s    

z(WikipediaAPIWrapper.validate_environment)queryr   c                 C   sv   | j j|dt | jd}g }|d| j D ],}| | }r,| || }r,|| q,|sbdS d|d| j S )z,Run Wikipedia search and get page summaries.Nresultsz)No good Wikipedia Search Result was foundz

)	r   searchWIKIPEDIA_MAX_QUERY_LENGTHr   _fetch_page_formatted_page_summaryappendjoinr   )selfr   page_titlesZ	summaries
page_title	wiki_pagesummaryr   r   r   run.   s    
 zWikipediaAPIWrapper.run)r'   r(   r   c                 C   s   d|  d|j  S )NzPage: z

Summary: )r)   )r'   r(   r   r   r   r"   <   s    z+WikipediaAPIWrapper._formatted_page_summaryc              	   C   s`   ||j |jd}| jr<|j|j|j|j|j|j|j|j	dni }t
|jd | j ||d}|S )N)titler)   source)
categoriespage_urlZ
image_urlsZrelated_titles	parent_id
referencesrevision_idsections)Zpage_contentmetadata)r)   urlr   r-   Zimageslinksr/   r0   r1   r2   r   contentr   )r%   r'   r(   Z	main_metaZadd_metadocr   r   r   _page_to_document@   s0    z%WikipediaAPIWrapper._page_to_document)pager   c              	   C   s>   z| j j|ddW S  | j jj| j jjfk
r8   Y d S X d S )NF)r+   Zauto_suggest)r   r9   
exceptionsZ	PageErrorZDisambiguationError)r%   r9   r   r   r   r!   ]   s    zWikipediaAPIWrapper._fetch_pagec                 C   s   t | |S )
        Run Wikipedia search and get the article text plus the meta information.
        See

        Returns: a list of documents.

        )list	lazy_load)r%   r   r   r   r   loadf   s    zWikipediaAPIWrapper.loadc                 c   sV   | j j|dt | jd}|d| j D ](}| | }r(| || }r(|V  q(dS )r;   Nr   )r   r   r    r   r!   r8   )r%   r   r&   r'   r(   r7   r   r   r   r=   p   s    
 zWikipediaAPIWrapper.lazy_loadN)__name__
__module____qualname____doc__r   __annotations__r   intr   strr   boolr   r	   r   r   r*   staticmethodr   r"   r   r8   r!   r   r>   r   r=   r   r   r   r   r
      s   
		
r
   )rB   loggingtypingr   r   r   r   r   Zlangchain_core.documentsr   Zlangchain_core.pydantic_v1r   r	   	getLoggerr?   loggerr    r
   r   r   r   r   <module>   s   
