o
    Zh8                     @   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dedefddZdee deee  fddZdedee f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w |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   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/clarifai.pyvalidate_environment0   s4   








z'ClarifaiEmbeddings.validate_environmenttextsc           
   
      s   ddl m} || jj d}g }z2tdt||D ]&}||||  } fddt|D }| j|}|	dd |j
D  qW |S  ty^ }	 ztd|	  W Y d}	~	|S d}	~	ww )	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      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_sizer4   ibatchZinput_batchpredict_responseer   r-   r%   embed_documentsS   s.   	
z"ClarifaiEmbeddings.embed_documentstextc              
   C   sl   z| j jt|ddd}dd |jD }W |d S  ty5 } ztd|  W Y d}~|d S d}~ww )	zCall out to Clarifai's embedding models.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        zutf-8rE   )Z
input_typec                 S   r/   r0   r1   )r+   opr   r   r%   r.      s    z2ClarifaiEmbeddings.embed_query.<locals>.<listcomp>r6   Nr   )r   Zpredict_by_bytesbytesr;   r<   r=   r>   )r?   rE   rB   r4   rC   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   floatrD   rH   r   r   r   r%   r   
   s0   
 !#r   )loggingtypingr   r   r   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r	   r
   	getLoggerrI   r=   r   r   r   r   r%   <module>   s    
