o
    ȳg5                     @   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,  )7P31P279P27P361P527P495P17P585P131P106P21P569P570P577P50P571P641P625P19P69P108P136P39P161P20P101P179P175P7937P57P607P509P800P449P580P582P276r   P112P740P159P452P102P1142P1387P1576P140P178P287P25P22P40P185P802P1416enzlangchain-wikidataz"https://www.wikidata.org/w/api.phpz0https://www.wikidata.org/w/rest.php/wikibase/v0/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_agentmediawiki_urlrA   z`Could not import mediawikiapi python package. Please install it with `pip install mediawikiapi`.)Client<   z
User-AgentT)timeoutbase_urlheadersfollow_redirectsrB   zxCould not import wikibase_rest_api_client python package. Please install it with `pip install wikibase-rest-api-client`.)
mediawikiapirM   mediawikiapi.configrN   WIKIDATA_USER_AGENTWIKIDATA_API_URLImportErrorwikibase_rest_api_clientrQ   WIKIDATA_REST_API_URL)clsrK   rM   rN   rQ   client r`   b/var/www/html/chatdoc2/venv/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)supported_propsrH   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`   ra   
<listcomp>   s    z8WikidataAPIWrapper._item_to_document.<locals>.<listcomp>
zhttps://www.wikidata.org/wiki/)titlesource)page_contentmeta))wikibase_rest_api_client.utilities.fluentrd   rB   rG   rH   get_itemloggerwarninglabelappenddescriptionaliasesjoin
statementsitemsr   rF   )selfrc   rd   fluent_clientresp	doc_linesproprK   r`   r`   ra   _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_LENGTHrA   searchrD   r   ru   r{   r   clipped_queryrz   docsitemdocr`   r`   ra   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.Nr   zResult z:
z(No good Wikidata Search Result was foundz

)	r   rA   r   rD   r   ru   rn   rx   rF   r   r`   r`   ra   run   s   zWikidataAPIWrapper.runN)__name__
__module____qualname____doc__r   __annotations__rD   intrE   boolrF   DEFAULT_PROPERTIESrG   r   strDEFAULT_LANG_CODErH   r   classmethodr   rb   r   r   r   r   r   r`   r`   r`   ra   r@   L   s   
 
 r@   )r   loggingtypingr   r   r   r   langchain_core.documentsr   pydanticr   r   	getLoggerr   rr   r   r   r   rY   rZ   r]   r@   r`   r`   r`   ra   <module>   s    
9