
    dh                         S r SSKrSSKrSSKrSSKrSSKrSSKJrJrJ	r	J
r
Jr  SSKrSSKJrJr  SSKJr  SSKJrJr  \R*                  " \5      r " S S\5      r " S	 S
\5      rg)zTool for the Nuclia Understanding API.

Installation:

```bash
    pip install --upgrade protobuf
    pip install nucliadb-protos
```
    N)AnyDictOptionalTypeUnion)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTool)	BaseModelFieldc                       \ rS rSr% Sr\" SSS9r\\S'   \" SSS9r	\\S'   \" SS	S9r
\\   \S
'   \" SSS9r\\   \S'   Srg)	NUASchema   a  Input for Nuclia Understanding API.

Attributes:
    action: Action to perform. Either `push` or `pull`.
    id: ID of the file to push or pull.
    path: Path to the file to push (needed only for `push` action).
    text: Text content to process (needed only for `push` action).
.z+Action to perform. Either `push` or `pull`.)descriptionactionzID of the file to push or pull.idz9Path to the file to push (needed only for `push` action).pathz8Text content to process (needed only for `push` action).text N)__name__
__module____qualname____firstlineno____doc__r   r   str__annotations__r   r   r   r   __static_attributes__r       ]/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/tools/nuclia/tool.pyr   r      ss     AFC  5B   OD(3-   ND(3- r   r   c                     ^  \ rS rSr% SrSr\\S'   Sr\\S'   \	r
\\   \S'   0 r\\\4   \S'   0 r\\\4   \S	'   S S
\SS4U 4S jjjr S!S\S\S\\   S\\   S\\   S\4S jjr   S"S\S\S\\   S\\   S\\   S\4S jjrS\S\S\4S jrS\S\S\4S jrS\S\S\4S jrS\S\4S jrS#S jrS\S\\S4   4S jrS\\   S\\   SS4S jrSr U =r!$ )$NucliaUnderstandingAPI9   z8Tool to process files with the Nuclia Understanding API.nuclia_understanding_apinamezvA wrapper around Nuclia Understanding API endpoints. Useful for when you need to extract text from any kind of files. r   args_schema_results_config	enable_mlreturnNc                 "  > [         R                  R                  SS5      nSU S3U R                  S'   [         R                  R                  S5      nU(       d  [	        S5      eX0R                  S'   XR                  S	'   [
        TU ]  5         g )
NNUCLIA_ZONEzeurope-1zhttps://z.nuclia.cloud/api/v1BACKENDNUCLIA_NUA_KEYz+NUCLIA_NUA_KEY environment variable not setNUA_KEYr(   )osenvirongetr'   
ValueErrorsuper__init__)selfr(   zonekey	__class__s       r   r4   NucliaUnderstandingAPI.__init__E   st    zz~~mZ8$,TF2F"GYjjnn-.JKK&)LL#$-[!r   r   r   r   r   run_managerc                     US:X  aC  U R                  X45        U(       a  U R                  X#5      $ U(       a  U R                  X$5      $  gUS:X  a  U R                  U5      $ g)zUse the tool.pushpull )_check_params	_pushFile	_pushText_pull)r5   r   r   r   r   r:   s         r   _runNucliaUnderstandingAPI._runP   sc     Vt*~~b//~~b//   v::b>!r   c                   #    U R                  X45        U(       a  U R                  X#5        U(       a  U R                  X$5        Sn U R                  U5      nU(       a   U$ [        R
                  " S5      I Sh  vN   M;   N7f)zUse the tool asynchronously.N   )r?   r@   rA   rB   asynciosleep)r5   r   r   r   r   r:   datas          r   _arunNucliaUnderstandingAPI._arunc   sq      	4&NN2$NN2$::b>D --###	  $s   A9B;B<Bc                 V    SUSS.0SU R                   S   0S.nU R                  X5      $ )Nr   r   )bodyformatml_textr(   )	textfieldprocessing_options)r'   
_pushField)r5   r   r   fields       r   rA    NucliaUnderstandingAPI._pushTexty   s8     41"=>#,dll;.G"H
 r))r   content_pathc           	         [        US5       n[        R                  " U R                  S   S-   [        R
                  " U5      S   =(       d    SSU R                  S   -   S.UR                  5       S	9nUR                  S
:w  a<  [        R                  SU SUR                   SUR                   35         S S S 5        gSUR                   0SU R                  S   0S.nU R                  X5      sS S S 5        $ ! , (       d  f       g = f)Nrbr,   z/processing/uploadr   zapplication/octet-streamBearer r.   zcontent-typex-stf-nuakey)headersrI      zError uploading z:  r>   filerO   r(   )	filefieldrQ   )openrequestspostr'   	mimetypes
guess_typereadstatus_codeloggerinfor   rR   )r5   r   rU   source_fileresponserS   s         r   r@    NucliaUnderstandingAPI._pushFile   s    ,%}}Y'*>>$-$8$8$Fq$I %21$-Y0G$G
 !%%'H ##s*&|nB++,Ahmm_>  &%$ #)X]]O!=+4dll;6O*P r1+ &%%s   B+C<1C<<
D
rS   c                    [         R                  SU S35        [        R                  " U R                  S   S-   SSU R                  S   -   S.US	9nUR
                  S
:w  a=  [         R                  SU SUR
                   SUR                   35        [        S5      eUR                  5       S   n[         R                  SU SU 35        USS.U R                  U'   U$ )NzPushing z	 in queuer,   z/processing/pushzapplication/jsonrX   r.   rY   )r[   jsonr\   zError pushing field :r]   zError pushing fielduuidzField z pushed in queue, uuid: pending)ro   status)
rg   rh   ra   rb   r'   rf   r   r2   rm   r&   )r5   r   rS   rj   ro   s        r   rR   !NucliaUnderstandingAPI._pushField   s    hrd),-==LL#&88 2 )DLL,C C 
 3&KK&rd!H,@,@+A8==/R 233==?6*DKK&$<TFCD)- CDMM"Kr   c                     U R                  5         U R                  R                  US 5      nU(       d  [        R	                  U S35        gUS   S:X  a  [        R	                  SUS    S35        gUS   $ )	Nz not in queuer>   rq   rp   zWaiting for ro   z to be processedrI   )_pull_queuer&   r1   rg   rh   )r5   r   results      r   rB   NucliaUnderstandingAPI._pull   sr    ""2t,KK2$m,-H*KK,vf~&66FGH&>!r   c                     SSK Jn   SSKJn  [
        R                  " U R                  S   S-   SS	U R                  S
   -   0S9R                  5       nUS   S:X  a  [        R                  S5        g US   S:X  a  US   nU" 5       nUR                  [        R                  " U5      5        UR                  n[        R                  SU S35        U R                  U5      nU(       d  [        R                  SU 35        g SU R                   U   S'   U" USSS9n	XR                   U   S'   g g ! [         a  n[        S5      UeS nAff = f! [         a  n[        S5      UeS nAff = f)Nr   )BrokerMessagezOnucliadb-protos is not installed. Run `pip install nucliadb-protos` to install.)MessageToJsonzMUnable to import google.protobuf, please install with `pip install protobuf`.r,   z/processing/pullrZ   rX   r.   )r[   rq   emptyzQueue emptyokpayloadzPulled z from queuezNo matching id for doneT)preserving_proto_field_nameincluding_default_value_fieldsrI   )nucliadb_protos.writer_pb2rx   ImportErrorgoogle.protobuf.json_formatry   ra   r1   r'   rm   rg   rh   ParseFromStringbase64	b64decodero   _find_matching_idr&   )
r5   rx   ery   resr|   pbro   matching_idrI   s
             r   rt   "NucliaUnderstandingAPI._pull_queue   s|   	@	A llLL#&88	DLL,C C

 $& 	 x=G#KK&]d")nGBv//8977DKK'${34006K1$897=k*84$0437
 6:k*62! #+  	@ 	  	* 	s.   D0 E 0
E:EE
E)E$$E)ro   c                 d    U R                   R                  5        H  u  p#US   U:X  d  M  Us  $    g )Nro   )r&   items)r5   ro   r   ru   s       r   r   (NucliaUnderstandingAPI._find_matching_id   s1    ----/JBf~%	 0 r   c                 j    U(       d  U(       d  [        S5      eU(       a  U(       a  [        S5      eg g )NzFile path or text is requiredz1Cannot process both file and text on a single run)r2   )r5   r   r   s      r   r?   $NucliaUnderstandingAPI._check_params   s+    D<==DPQQ 4r   r   )F)N)NNN)r)   N)"r   r   r   r   r   r$   r   r   r   r   r%   r   r   r&   r   r   r'   boolr4   r   r	   rC   r   rJ   rA   r@   rR   rB   rt   r   r   r?   r   __classcell__)r8   s   @r   r!   r!   9   s   B*D#*	L   $-Ki,!Hd38n! GT#s(^ 	$ 	4 	 	" <@  sm	
 sm 78 
. #"@D  sm	
 sm <= 
,*C *s *s *2C 2s 2s 20S   *
" 
" 
"(:Tc eCI.> R(3- Rx} R R Rr   r!   )r   rG   r   loggingrc   r/   typingr   r   r   r   r   ra   langchain_core.callbacksr   r	   langchain_core.toolsr
   pydanticr   r   	getLoggerr   rg   r   r!   r   r   r   <module>r      s]        	 3 3  * %			8	$	 8tRX tRr   