
    Ng'                         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dS )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<   dS )	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        a/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/tools/nuclia/tool.pyr   r      s           %A  FC    e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_configF	enable_mlreturnNc                 ,   t           j                            dd          }d| d| j        d<   t           j                            d          }|st	          d          || j        d<   || j        d	<   t                                                       d S )
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   s    z~~mZ88"GT"G"G"GYjnn-.. 	*JKKK&)DL#$-[!r   r   r   r   r   run_managerc                     |dk    rG|                      ||           |r|                     ||          S |r|                     ||          S n|dk    r|                     |          S dS )zUse the tool.pushpull )_check_params	_pushFile	_pushText_pull)r1   r   r   r   r   r5   s         r   _runzNucliaUnderstandingAPI._runP   s     VtT*** 0~~b$/// 0~~b$///0v::b>>!rr   c                   K   |                      ||           |r|                     ||           |r|                     ||           d}	 |                     |          }|rnt	          j        d           d{V  3|S )zUse the tool asynchronously.NT   )r:   r;   r<   r=   asynciosleep)r1   r   r   r   r   r5   datas          r   _arunzNucliaUnderstandingAPI._arunc   s       	4&&& 	%NN2t$$$ 	%NN2t$$$	$::b>>D -#########		$
 r   c                 \    d|ddid| j         d         id}|                     ||          S )Nr   r   )bodyformatml_textr$   )	textfieldprocessing_options)r#   
_pushField)r1   r   r   fields       r   r<   z NucliaUnderstandingAPI._pushTexty   sC     41"="=>#,dl;.G"H
 
 r5)))r   content_pathc           	         t          |d          5 }t          j        | j        d         dz   t	          j        |          d         pdd| j        d         z   d|                                	          }|j        d
k    r;t          	                    d| d|j         d|j
                    	 d d d            dS d|j
         id| j        d         id}|                     ||          cd d d            S # 1 swxY w Y   d S )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$F$Fq$I %21$-Y0G$G 
 !%%''  H #s**>| > >+> >.6m> >   	2 	2 	2 	2 	2 	2 	2 	2$ #)X]*<!=+4dl;6O*P  r511+	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   BC6:/C66C:=C:rL   c                    t                               d| d           t          j        | j        d         dz   dd| j        d         z   d|	          }|j        d
k    r<t                               d| d|j         d|j                    t          d          |                                d         }t                               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   ,r,,,---=L#&88 2 )DL,C C  
 
 
 3&&KKVrVV0DVVx}VV   2333==??6*DKKCCCTCCDDD)- C CDM"Kr   c                    |                                   | j                            |d           }|st                              | d           dS |d         dk    r&t                              d|d          d           dS |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   s    ""2t,, 	"KK2,,,---2H**KKGvf~GGGHHH2&>!r   c                 *   	 ddl m} n"# t          $ r}t          d          |d }~ww xY w	 ddlm} n"# t          $ r}t          d          |d }~ww xY wt          j        | j        d         dz   dd	| j        d
         z   i                                          }|d         dk    rt          
                    d           d S |d         dk    r|d         } |            }|                    t          j        |                     |j        }t          
                    d| d           |                     |          }|st          
                    d|            d S d| j        |         d<    ||dd          }	|	| j        |         d<   d S d S )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AAAAAA 	 	 	*  	 lL#&88	DL,C C
 
 

 $&& 	 x=G##KK&&&&&]d"")nGBv/889997DKK3$3334440066K 	:8$88999997=k*84$}0437  
 6:k*6222! #"s$   	 
(#(3 
AAArf   c                 d    | j                                         D ]\  }}|d         |k    r|c S d S )Nrf   )r"   items)r1   rf   r   rk   s       r   r{   z(NucliaUnderstandingAPI._find_matching_id   sE    ---// 	 	JBf~%%			 &tr   c                 V    |s|st          d          |r|rt          d          d S d S )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   sX     	>D 	><=== 	RD 	RPQQQ	R 	R 	R 	Rr   )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B*D#***	L     $-Ki,,,!Hd38n!!! GT#s(^   	 	$ 	4 	 	 	 	 	 	" <@   sm	
 sm 78 
   . #"@D   sm	
 sm <= 
   ,*C *s *s * * * *2C 2s 2s 2 2 2 20S      *
" 
" 
" 
" 
" 
"(: (: (: (:Tc eCI.>    R(3- Rx} R R R R R R R 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      sL           				 3 3 3 3 3 3 3 3 3 3 3 3 3 3         * ) ) ) ) ) % % % % % % % %		8	$	$    	   8tR tR tR tR tRX tR tR tR tR tRr   