o
    Zh5                     @   sz   d Z ddlZddl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ZdZd	Zd
ZdZG dd de
ZdS )zUtil that calls Wikidata.    N)AnyDictListOptional)Document)	BaseModelmodel_validatori,  )7ZP31ZP279ZP27ZP361ZP527ZP495ZP17ZP585ZP131ZP106ZP21ZP569ZP570ZP577ZP50ZP571ZP641ZP625ZP19P69ZP108ZP136ZP39ZP161ZP20ZP101ZP179ZP175ZP7937ZP57ZP607ZP509ZP800ZP449ZP580ZP582ZP276r	   ZP112ZP740ZP159ZP452ZP102ZP1142ZP1387ZP1576ZP140ZP178ZP287ZP25ZP22ZP40ZP185ZP802ZP1416enzlangchain-wikidataz"https://www.wikidata.org/w/api.phpz0https://www.wikidata.org/w/rest.php/wikibase/v1/c                   @   s   e Zd ZU dZeed< eed< dZeed< dZe	ed< dZ
eed	< eZee ed
< eZeed< ed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fddZdS )WikidataAPIWrapperaw  Wrapper around the Wikidata API.

    To use, you should have the ``wikibase-rest-api-client`` and
    ``mediawikiapi `` python packages installed.
    This wrapper will use the Wikibase APIs to conduct searches and
    fetch item content. By default, it will return the item content
    of the top-k results.
    It limits the Document content by doc_content_chars_max.
    wikidata_mwwikidata_rest   top_k_resultsFload_all_available_metai  doc_content_chars_maxwikidata_propslangbefore)modevaluesreturnc                 C   s   zddl m} ddlm} ||ttd|d< W n ty#   tdw zddlm} |dt	d	tid
d}||d< W |S  tyF   tdw )z7Validate that the python package exists in environment.r   )MediaWikiAPI)Config)
user_agentZmediawiki_urlr   z`Could not import mediawikiapi python package. Please install it with `pip install mediawikiapi`.)Client<   z
User-AgentT)timeoutbase_urlheadersZfollow_redirectsr   zxCould not import wikibase_rest_api_client python package. Please install it with `pip install wikibase-rest-api-client`.)
Zmediawikiapir   Zmediawikiapi.configr   WIKIDATA_USER_AGENTWIKIDATA_API_URLImportErrorZwikibase_rest_api_clientr   WIKIDATA_REST_API_URL)clsr   r   r   r   client r&   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/utilities/wikidata.pyvalidate_environment_   s4   

z'WikidataAPIWrapper.validate_environmentqidc              	   C   s   ddl m} || j| j| jd}||}|s"td| d d S g }|jr0|	d|j  |j
r<|	d|j
  |jrK|	dd	|j  |j D ]\}}|ri|	|j d
d	dd |D   qPtd|d | j |d| ddS )Nr   )FluentWikibaseClient)Zsupported_propsr   zCould not find item z in WikidatazLabel: zDescription: z	Aliases: z, z: c                 S   s   g | ]}|j pd qS )unknown)value).0vr&   r&   r'   
<listcomp>   s    z8WikidataAPIWrapper._item_to_document.<locals>.<listcomp>
zhttps://www.wikidata.org/wiki/)titlesource)page_contentmeta)Z)wikibase_rest_api_client.utilities.fluentr*   r   r   r   Zget_itemloggerwarninglabelappenddescriptionaliasesjoinZ
statementsitemsr   r   )selfr)   r*   Zfluent_clientrespZ	doc_linespropr   r&   r&   r'   _item_to_document   s2   
z$WikidataAPIWrapper._item_to_documentqueryc                 C   sR   |dt  }| jj|| jd}g }|d| j D ]}| | }r&|| q|S )z[
        Run Wikidata search and get the item documents plus the meta information.
        Nresults)WIKIDATA_MAX_QUERY_LENGTHr   searchr   r@   r8   r=   rA   Zclipped_queryr<   docsitemdocr&   r&   r'   load   s   
zWikidataAPIWrapper.loadc                 C   sx   |dt  }| jj|| jd}g }|d| j D ]}| | }r-|d| d|j  q|s2dS d|d| j S )z+Run Wikidata search and get item summaries.NrB   zResult z:
z(No good Wikidata Search Result was foundz

)	rD   r   rE   r   r@   r8   r3   r;   r   rF   r&   r&   r'   run   s   zWikidataAPIWrapper.runN)__name__
__module____qualname____doc__r   __annotations__r   intr   boolr   DEFAULT_PROPERTIESr   r   strDEFAULT_LANG_CODEr   r   classmethodr   r(   r   r   r@   rJ   rK   r&   r&   r&   r'   r   L   s   
 
 r   )rO   loggingtypingr   r   r   r   Zlangchain_core.documentsr   Zpydanticr   r   	getLoggerrL   r5   rD   rS   rU   r    r!   r#   r   r&   r&   r&   r'   <module>   s    
9