U
    h                     @   s`   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 e eZG dd de	eZdS )    N)AnyDictListOptional)
Embeddings)	BaseModelFieldroot_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< G dd dZedd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_basec                   @   s   e Zd ZdZdS )zClarifaiEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/clarifai.pyConfig.   s   r   )pre)valuesreturnc              
   C   s   zddl m} W n tk
r,   tdY nX |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   r   validate_environment1   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 n2 tk
r }	 ztd|	  W 5 d}	~	X Y nX |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   r0   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_sizer5   ibatchZinput_batchpredict_responseer   r/   r   embed_documentsS   s&    	

"z"ClarifaiEmbeddings.embed_documents)textr   c              
   C   sf   z*| j jt|ddd}dd |jD }W n2 tk
r\ } ztd|  W 5 d}~X Y nX |d S )	zCall out to Clarifai's embedding models.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        zutf-8rF   )Z
input_typec                 S   s   g | ]}t |jjd  jqS r1   r2   )r-   opr   r   r   r0      s    z2ClarifaiEmbeddings.embed_query.<locals>.<listcomp>r7   Nr   )r   Zpredict_by_bytesbytesr<   r=   r>   r?   )r@   rF   rC   r5   rD   r   r   r   embed_queryv   s    
 
"zClarifaiEmbeddings.embed_query)r   r   r   __doc__r   r   r,   __annotations__r   r   r   r   r   r   r   r   r   r   r   r	   r   r(   r   floatrE   rI   r   r   r   r   r
   
   s   
!#r
   )loggingtypingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r   r	   	getLoggerr   r>   r
   r   r   r   r   <module>   s
   
