
    Ng                         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j        e          ZdZg dZdZd	Zd
ZdZ G 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                      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          ededefd                        Zdedee         fdZdedee         fdZdedef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                 ,   	 ddl m} ddlm}  | |t          t
                              |d<   n# t          $ r t          d          w xY w	 ddlm}  |dt          d	t          id
          }||d<   n# t          $ r t          d          w xY w|S )z7Validate that the python package exists in environment.r   )MediaWikiAPI)Config)
user_agentmediawiki_urlrC   z`Could not import mediawikiapi python package. Please install it with `pip install mediawikiapi`.)Client<   z
User-AgentT)timeoutbase_urlheadersfollow_redirectsrD   zxCould not import wikibase_rest_api_client python package. Please install it with `pip install wikibase-rest-api-client`.)
mediawikiapirP   mediawikiapi.configrQ   WIKIDATA_USER_AGENTWIKIDATA_API_URLImportErrorwikibase_rest_api_clientrT   WIKIDATA_REST_API_URL)clsrM   rP   rQ   rT   clients         b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/utilities/wikidata.pyvalidate_environmentz'WikidataAPIWrapper.validate_environment_   s   	111111222222$0L"5EUVVV% %F=!!  	 	 	E  		777777V.%':;!%	  F '-F?## 	 	 	Q  	
 s   /2 A&A7 7Bqidc                    ddl m}  || j        | j        | j                  }|                    |          }|s t                              d| d           d S g }|j        r|	                    d|j                    |j
        r|	                    d|j
                    |j        r0|	                    dd	                    |j                              |j                                        D ]9\  }}|r2|	                    |j         d
d	                    |                      :t          d                    |          d | j                 |d| d          S )Nr   )FluentWikibaseClient)supported_propsrJ   zCould not find item z in WikidatazLabel: zDescription: z	Aliases: z, z: 
zhttps://www.wikidata.org/wiki/)titlesource)page_contentmeta))wikibase_rest_api_client.utilities.fluentrg   rD   rI   rJ   get_itemloggerwarninglabelappenddescriptionaliasesjoin
statementsitemsr   rH   )selfre   rg   fluent_clientresp	doc_linesproprM   s           rc   _item_to_documentz$WikidataAPIWrapper._item_to_document   s   RRRRRR.B.B0C$)/
 /
 /
 %%c** 	NNC#CCCDDD4	: 	53tz33444 	A?T-=??@@@< 	DB4<)@)@BBCCC O1133 	G 	GLD& G  DJ!E!E$))F2C2C!E!EFFF))I..0L$2L0LM*P3*P*PQQ
 
 
 	
    queryc                     |dt                    }| j                            || j                  }g }|d| j                 D ].}|                     |          x}r|                    |           /|S )z[
        Run Wikidata search and get the item documents plus the meta information.
        Nresults)WIKIDATA_MAX_QUERY_LENGTHrC   searchrF   r~   rs   ry   r   clipped_queryrx   docsitemdocs          rc   loadzWikidataAPIWrapper.load   s    
 8889 ''t?Q'RR.D../ 	! 	!D,,T222s !C   r   c                 D   |dt                    }| j                            || j                  }g }|d| j                 D ]9}|                     |          x}r |                    d| d|j                    :|sd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   rC   r   rF   r~   rs   rl   rv   rH   r   s          rc   runzWikidataAPIWrapper.run   s     8889 ''t?Q'RR.D../ 	C 	CD,,T222s CAdAAs/?AABBB 	>=={{4  !=4#=!=>>r   N)__name__
__module____qualname____doc__r   __annotations__rF   intrG   boolrH   DEFAULT_PROPERTIESrI   r   strDEFAULT_LANG_CODErJ   r	   classmethodr   rd   r   r   r~   r   r    r   rc   rB   rB   L   sD          M3$)T)))!%3%%% 2NDI222!D#!!!_(###$ 3    [ $#@
S 
Xh-? 
 
 
 
8# $x.    ? ? ? ? ? ? ? ?r   rB   )r   loggingtypingr   r   r   r   langchain_core.documentsr   pydanticr   r	   	getLoggerr   rp   r   r   r   r\   r]   r`   rB   r   r   rc   <module>r      s       , , , , , , , , , , , , - - - - - - / / / / / / / /		8	$	$ 8 8 8 r  * 7 J j? j? j? j? j? j? j? j? j? j?r   