
    7|h#                         d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlZddlmZmZ ddlmZ ddlmZmZ  ej*                  e      Z G d de      Z G d	 d
e      Zy)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                       e Zd ZU dZ edd      Zeed<    edd      Zeed<    edd      Z	e
e   ed	<    edd
      Ze
e   ed<   y)	NUASchemaa0  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).textN)__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   r        d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/tools/nuclia/tool.pyr   r      ss     AFC  5B   OD(3-   ND(3- r   r   c                       e Zd ZU dZdZeed<   dZeed<   eZ	e
e   ed<   i Zeeef   ed<   i Zeeef   ed<   dd	ed
df fdZ	 ddededee   dee   dee   d
efdZ	 	 	 d dededee   dee   dee   d
efdZdeded
efdZdeded
efdZdeded
efdZded
efdZd!dZded
eedf   fdZdee   dee   d
dfdZ xZS )"NucliaUnderstandingAPIz8Tool 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                    t         j                  j                  dd      }d| d| j                  d<   t         j                  j                  d      }|st	        d      || j                  d<   || j                  d	<   t
        |           y )
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   r0   zNucliaUnderstandingAPI.__init__E   sx    zz~~mZ8$,TF2F"GYjjnn-.JKK&)DLL#$-[!r   r   r   r   r   run_managerc                     |dk(  r;| j                  ||       |r| j                  ||      S |r| j                  ||      S y|dk(  r| j                  |      S y)zUse the tool.pushpull )_check_params	_pushFile	_pushText_pull)r1   r   r   r   r   r5   s         r   _runzNucliaUnderstandingAPI._runP   sd     VtT*~~b$//~~b$//  v::b>!r   c                    K   | j                  ||       |r| j                  ||       |r| j                  ||       d}	 | j                  |      }|r	 |S t	        j
                  d       d{    47 w)zUse the tool asynchronously.N   )r:   r;   r<   r=   asynciosleep)r1   r   r   r   r   r5   datas          r   _arunzNucliaUnderstandingAPI._arunc   sx      	4&NN2t$NN2t$::b>D --###	  $s   A,A6.A4/A6c                 X    d|ddid| j                   d   id}| j                  ||      S )Nr   r   )bodyformatml_textr$   )	textfieldprocessing_options)r#   
_pushField)r1   r   r   fields       r   r<   z NucliaUnderstandingAPI._pushTexty   s:     41"=>#,dll;.G"H
 r5))r   content_pathc           	         t        |d      5 }t        j                  | j                  d   dz   t	        j
                  |      d   xs dd| j                  d   z   d|j                         	      }|j                  d
k7  r<t        j                  d| d|j                   d|j                          	 d d d        yd|j                   id| j                  d   id}| j                  ||      cd d d        S # 1 sw Y   y xY w)Nrbr(   z/processing/uploadr   zapplication/octet-streamBearer r*   zcontent-typex-stf-nuakey)headersrC      zError uploading z:  r9   filerH   r$   )	filefieldrJ   )openrequestspostr#   	mimetypes
guess_typereadstatus_codeloggerinfor   rK   )r1   r   rM   source_fileresponserL   s         r   r;   z NucliaUnderstandingAPI._pushFile   s	   ,% 	2}}Y'*>>$-$8$8$Fq$I %21$-Y0G$G
 !%%'H ##s*&|nB++,Ahmm_> 	2 	2$ #)X]]O!=+4dll;6O*P r51+	2 	2 	2s   B&C8<2C88DrL   c                    t         j                  d| d       t        j                  | j                  d   dz   dd| j                  d   z   d|	      }|j
                  d
k7  r=t         j                  d| d|j
                   d|j                          t        d      |j                         d   }t         j                  d| d|        |dd| j                  |<   |S )NzPushing z	 in queuer(   z/processing/pushzapplication/jsonrP   r*   rQ   )rS   jsonrT   zError pushing field :rU   zError pushing fielduuidzField z pushed in queue, uuid: pending)rf   status)
r_   r`   rY   rZ   r#   r^   r   r.   rd   r"   )r1   r   rL   rb   rf   s        r   rK   z!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                     | j                          | j                  j                  |d       }|st        j	                  | d       y|d   dk(  rt        j	                  d|d    d       y|d   S )	Nz not in queuer9   rh   rg   zWaiting for rf   z to be processedrC   )_pull_queuer"   r-   r_   r`   )r1   r   results      r   r=   zNucliaUnderstandingAPI._pull   sr    ""2t,KK2$m,-H*KK,vf~&66FGH&>!r   c                    	 ddl m} 	 ddlm} t        j                  | j                  d   dz   dd	| j                  d
   z   i      j                         }|d   dk(  rt        j                  d       y |d   dk(  r|d   } |       }|j                  t        j                  |             |j                  }t        j                  d| d       | j                  |      }|st        j                  d|        y d| j                   |   d<    ||dd      }	|	| j                   |   d<   y y # t        $ r}t        d      |d }~ww xY w# t        $ r}t        d      |d }~ww xY w)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/pullrR   rP   r*   )rS   rh   emptyzQueue emptyokpayloadzPulled z from queuezNo matching id for doneT)preserving_proto_field_nameincluding_default_value_fieldsrC   )nucliadb_protos.writer_pb2rm   ImportErrorgoogle.protobuf.json_formatrn   rY   r-   r#   rd   r_   r`   ParseFromStringbase64	b64decoderf   _find_matching_idr"   )
r1   rm   ern   resrq   pbrf   matching_idrC   s
             r   rj   z"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.   D. E .	E7EE	E%E  E%rf   c                 `    | j                   j                         D ]  \  }}|d   |k(  s|c S  y )Nrf   )r"   items)r1   rf   r   rk   s       r   r{   z(NucliaUnderstandingAPI._find_matching_id   s8    ----/ 	JBf~%		 r   c                 B    |s|st        d      |r|rt        d      y y )NzFile path or text is requiredz1Cannot process both file and text on a single run)r.   )r1   r   r   s      r   r:   z$NucliaUnderstandingAPI._check_params   s+    D<==DPQQ 4r   )F)N)NNN)r%   N) r   r   r   r   r    r   r   r   r   r!   r   r   r"   r   r   r#   boolr0   r   r	   r>   r   rD   r<   r;   rK   r=   rj   r   r{   r:   __classcell__)r4   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   rA   ry   loggingr[   r+   typingr   r   r   r   r   rY   langchain_core.callbacksr   r	   langchain_core.toolsr
   pydanticr   r   	getLoggerr   r_   r   r   r   r   r   <module>r      s_        	 3 3  * %			8	$	 8tRX tRr   