a
    bg8                     @   sd   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
mZmZ e eZG dd de	eZdS )    N)AnyDictListOptional)
Embeddings)	BaseModel
ConfigDictFieldmodel_validatorc                   @   s  e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZee ed< eddd	Zee ed
< eddd	Zee ed< eddd	Zeed< dZeed< edddZeddeeedddZee eee  dddZeee dddZdS )ClarifaiEmbeddingsa  Clarifai embedding models.

    To use, you should have the ``clarifai`` python package installed, and the
    environment variable ``CLARIFAI_PAT`` set with your personal access token or pass it
    as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import ClarifaiEmbeddings
            clarifai = ClarifaiEmbeddings(user_id=USER_ID,
                                          app_id=APP_ID,
                                          model_id=MODEL_ID)
                             (or)
            Example_URL = "https://clarifai.com/clarifai/main/models/BAAI-bge-base-en-v15"
            clarifai = ClarifaiEmbeddings(model_url=EXAMPLE_URL)
    N	model_urlmodel_idmodel_version_idapp_iduser_idT)defaultexcludepattokenmodelzhttps://api.clarifai.comapi_baseZforbid )extraZprotected_namespacesbefore)mode)valuesreturnc              
   C   s   zddl m} W n ty*   tdY n0 |d}|d}|d}|d}|d}|d	}|d
}	|d}
||||t|d|	|
||d|d< |S )zuValidate that we have all required info to access Clarifai
        platform and python package exists in environment.r   )ModelzXCould not import clarifai python package. Please install it with `pip install clarifai`.r   r   r   r   r   r   r   r   )id)urlr   r   Zmodel_versionr   r   r   base_urlr   )Zclarifai.client.modelr   ImportErrorgetdict)clsr   r   r   r   r   r   r   r   r   r   r   r   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/clarifai.pyvalidate_environment0   s2    









z'ClarifaiEmbeddings.validate_environment)textsr   c           
   
      s   ddl m} || jj d}g }zbtdt||D ]L}||||  } fddt|D }| j|}|	dd |j
D  q4W n4 ty }	 ztd|	  W Y d}	~	n
d}	~	0 0 |S )	zCall out to Clarifai's embedding models.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        r   )Inputs    c                    s"   g | ]\}} j t||d qS ))Zinput_idZraw_text)Zget_text_inputstr).0r   inpZ	input_objr   r%   
<listcomp>e   s   z6ClarifaiEmbeddings.embed_documents.<locals>.<listcomp>c                 S   s   g | ]}t |jjd  jqS r   listdata
embeddingsZvector)r+   outputr   r   r%   r.   k   s   Predict failed, exception: N)Zclarifai.client.inputr(   Zfrom_auth_helperr   Zauth_helperrangelen	enumerateZpredictextendoutputs	Exceptionloggererror)
selfr'   r(   Z
batch_sizer3   ibatchZinput_batchpredict_responseer   r-   r%   embed_documentsS   s&    	

&z"ClarifaiEmbeddings.embed_documents)textr   c              
   C   sh   z*| j jt|ddd}dd |jD }W n4 ty^ } ztd|  W Y d}~n
d}~0 0 |d S )	zCall out to Clarifai's embedding models.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        zutf-8rD   )Z
input_typec                 S   s   g | ]}t |jjd  jqS r/   r0   )r+   opr   r   r%   r.      s   z2ClarifaiEmbeddings.embed_query.<locals>.<listcomp>r5   Nr   )r   Zpredict_by_bytesbytesr:   r;   r<   r=   )r>   rD   rA   r3   rB   r   r   r%   embed_queryv   s    


&zClarifaiEmbeddings.embed_query)__name__
__module____qualname____doc__r   r   r*   __annotations__r   r   r   r   r	   r   r   r   r   r   r   Zmodel_configr
   classmethodr   r&   r   floatrC   rG   r   r   r   r%   r   
   s    
!#r   )loggingtypingr   r   r   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r	   r
   	getLoggerrH   r<   r   r   r   r   r%   <module>   s
   
