o
    Zh                     @   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model_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dedefddZde	de	fddZede	dedee	 fddZde	dedefddZde	dee	 fddZde	dee fddZde	dee f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_maxbefore)modevaluesreturnc                 C   sD   zddl }|dd}|| ||d< W |S  ty!   tdw )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   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/wikipedia.pyvalidate_environment   s   

z(WikipediaAPIWrapper.validate_environmentqueryc                 C   sv   | j j|dt | jd}g }|d| j D ]}| | }r,| || }r,|| q|s1d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.runr(   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   sd   ||j |jd}| jr|j|j|j|j|j|j|j|j	dni }t
|jd | j i ||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linksr0   r1   r2   r3   r   contentr   )r&   r(   r)   Z	main_metaZadd_metadocr   r   r   _page_to_documentA   s4   z%WikipediaAPIWrapper._page_to_documentpagec              	   C   s8   z	| j j|ddW S  | j jj| j jjfy   Y d S w )NF)r,   Zauto_suggest)r   r:   
exceptionsZ	PageErrorZDisambiguationError)r&   r:   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   loadg   s   zWikipediaAPIWrapper.loadc                 c   sX    | j j|dt | jd}|d| j D ]}| | }r)| || }r)|V  qdS )r<   Nr   )r   r    r!   r   r"   r9   )r&   r   r'   r(   r)   r8   r   r   r   r>   q   s   zWikipediaAPIWrapper.lazy_loadN)__name__
__module____qualname____doc__r   __annotations__r   intr   strr   boolr   r	   classmethodr   r   r+   staticmethodr   r#   r   r9   r"   r   r?   r   r>   r   r   r   r   r
      s"   
 		
r
   )rC   loggingtypingr   r   r   r   r   Zlangchain_core.documentsr   Zpydanticr   r	   	getLoggerr@   loggerr!   r
   r   r   r   r   <module>   s    
