a
    bg'                     @   s   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eZG dd deZG d	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                   @   sj   e Zd ZU dZedddZeed< edddZeed< edddZ	e
e ed	< ed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    r   r   s/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/tools/nuclia/tool.pyr      s"   
	r   c                       sN  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(ed
d fddZd)eeee ee ee edddZd*eeee ee ee edddZeeedddZeeedddZeeedddZeedddZd
dd d!Zeeed
f d"d#d$Zee ee d
d%d&d'Z  ZS )+NucliaUnderstandingAPIz8Tool to process files with the Nuclia Understanding API.Z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_configFN)	enable_mlreturnc                    s\   t jdd}d| d| jd< t jd}|s:tdn
|| jd< || jd	< t   d S )
NZNUCLIA_ZONEzeurope-1zhttps://z.nuclia.cloud/api/v1BACKENDZNUCLIA_NUA_KEYz+NUCLIA_NUA_KEY environment variable not setNUA_KEYr   )osenvirongetr   
ValueErrorsuper__init__)selfr   zonekey	__class__r   r   r(   E   s    


zNucliaUnderstandingAPI.__init__)r   r   r   r   run_managerr    c                 C   sL   |dkr6|  || |r$| ||S |rH| ||S n|dkrH| |S dS )zUse the tool.pushpull )_check_params	_pushFile	_pushText_pull)r)   r   r   r   r   r.   r   r   r   _runP   s    	
zNucliaUnderstandingAPI._runc                    sV   |  || |r| || |r,| || d}| |}|r@qRtdI dH  q0|S )zUse the tool asynchronously.N   )r2   r3   r4   r5   asynciosleep)r)   r   r   r   r   r.   datar   r   r   _arunc   s    	
zNucliaUnderstandingAPI._arun)r   r   r    c                 C   s*   d|ddid| j d id}| ||S )Nr   r   )bodyformatml_textr   )Z	textfieldprocessing_options)r   
_pushField)r)   r   r   fieldr   r   r   r4   y   s    z NucliaUnderstandingAPI._pushText)r   content_pathr    c              	   C   s   t |d}tj| jd d t|d p,dd| jd  d| d	}|jd
krt	d| d|j d|j
  W d    dS d|j
 id| jd id}| ||W  d    S W d    n1 s0    Y  d S )Nrbr!   z/processing/uploadr   zapplication/octet-streamBearer r"   zcontent-typex-stf-nuakey)headersr:      zError uploading z:  r1   filer>   r   )Z	filefieldr?   )openrequestspostr   	mimetypes
guess_typereadstatus_codeloggerinfor   r@   )r)   r   rB   Zsource_fileresponserA   r   r   r   r3      s,    	

z NucliaUnderstandingAPI._pushFile)r   rA   r    c                 C   s   t d| d tj| jd d dd| jd  d|d	}|jd
krpt d| d|j d|j  tdn6| d }t d| d|  |dd| j	|< |S d S )NzPushing z	 in queuer!   z/processing/pushzapplication/jsonrD   r"   rE   )rG   jsonrH   zError pushing field :rI   zError pushing fielduuidzField z pushed in queue, uuid: pending)rW   status)
rR   rS   rL   rM   r   rQ   r   r&   rU   r   )r)   r   rA   rT   rW   r   r   r   r@      s"    

z!NucliaUnderstandingAPI._pushField)r   r    c                 C   s`   |    | j|d }|s.t| d dS |d dkrTtd|d  d dS |d S d S )	Nz not in queuer1   rY   rX   zWaiting for rW   z to be processedr:   )_pull_queuer   r%   rR   rS   )r)   r   resultr   r   r   r5      s    zNucliaUnderstandingAPI._pull)r    c           
   
   C   sV  zddl m} W n. ty> } ztd|W Y d }~n
d }~0 0 zddlm} W n. ty~ } ztd|W Y d }~n
d }~0 0 tj| jd d dd	| jd
  id }|d dkrt	
d n|d dkrR|d }| }|t| |j}t	
d| d | |}|s(t	
d|  n*d| j| d< ||ddd}	|	| j| d< 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/pullrF   rD   r"   )rG   rY   emptyzQueue emptyokpayloadzPulled z from queuezNo matching id for doneT)Zpreserving_proto_field_nameZincluding_default_value_fieldsr:   )Znucliadb_protos.writer_pb2r\   ImportErrorZgoogle.protobuf.json_formatr]   rL   r%   r   rU   rR   rS   ZParseFromStringbase64	b64decoderW   _find_matching_idr   )
r)   r\   er]   resr`   ZpbrW   Zmatching_idr:   r   r   r   rZ      sN    

z"NucliaUnderstandingAPI._pull_queue)rW   r    c                 C   s,   | j  D ]\}}|d |kr
|  S q
d S )NrW   )r   items)r)   rW   r   r[   r   r   r   re      s    
z(NucliaUnderstandingAPI._find_matching_id)r   r   r    c                 C   s$   |s|st d|r |r t dd S )NzFile path or text is requiredz1Cannot process both file and text on a single run)r&   )r)   r   r   r   r   r   r2      s    z$NucliaUnderstandingAPI._check_params)F)N)NNN) r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r   boolr(   r   r   r6   r   r;   r4   r3   r@   r5   rZ   r   re   r2   __classcell__r   r   r,   r   r   9   sF   

    *r   )r   r8   rc   loggingrN   r#   typingr   r   r   r   r   rL   Zlangchain_core.callbacksr   r   Zlangchain_core.toolsr	   Zpydanticr
   r   	getLoggerr   rR   r   r   r   r   r   r   <module>   s   

