a
    bg                     @   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	 d dl
mZ d dlmZmZmZ d dlmZ dZd	Zed
dddG dd dee	ZdS )    N)AnyDictListOptional)
deprecated)
Embeddings)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)Selfz'sentence-transformers/all-mpnet-base-v2)feature-extractionz0.2.2z1.0z3langchain_huggingface.HuggingFaceEndpointEmbeddings)ZsinceZremovalZalternative_importc                   @   s  e Zd ZU dZdZeed< dZ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	< dZe	e
 ed
< edddZeddeeedddZeddedddZee
 eee  dddZee
 eee  dddZe
ee dddZe
ee ddd ZdS )!HuggingFaceHubEmbeddingsaw  HuggingFaceHub embedding models.

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

    Example:
        .. code-block:: python

            from langchain_community.embeddings import HuggingFaceHubEmbeddings
            model = "sentence-transformers/all-mpnet-base-v2"
            hf = HuggingFaceHubEmbeddings(
                model=model,
                task="feature-extraction",
                huggingfacehub_api_token="my-api-key",
            )
    Nclientasync_clientmodelrepo_idr   taskmodel_kwargshuggingfacehub_api_tokenZforbid )extraZprotected_namespacesbefore)mode)valuesreturnc                 C   s   t |dd}zddlm}m} |dr6|d |d< n(|drN|d |d< nt|d< t|d< ||d |d}||d |d}||d< ||d	< W n ty   td
Y n0 |S )z?Validate that api key and python package exists in environment.r   ZHUGGINGFACEHUB_API_TOKENr   )AsyncInferenceClientInferenceClientr   r   )r   tokenr   r   zfCould not import huggingface_hub python package. Please install it with `pip install huggingface_hub`.)r   Zhuggingface_hubr   r   getDEFAULT_MODELImportError)clsr   r   r   r   r   r   r   r   |/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/huggingface_hub.pyvalidate_environment5   s4    


z-HuggingFaceHubEmbeddings.validate_environmentafter)r   c                 C   s&   | j tvr"td| j  dt d| S )z#Post init validation for the class.zGot invalid task z, currently only z are supported)r   VALID_TASKS
ValueError)selfr   r   r#   	post_init\   s    

z"HuggingFaceHubEmbeddings.post_init)textsr   c                 C   s@   dd |D }| j pi }| jjd|i|| jd}t| S )zCall out to HuggingFaceHub's embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                 S   s   g | ]}| d dqS 
 replace.0textr   r   r#   
<listcomp>p       z<HuggingFaceHubEmbeddings.embed_documents.<locals>.<listcomp>inputsjsonr   )r   r   postr   r7   loadsdecoder(   r*   Z_model_kwargs	responsesr   r   r#   embed_documentsf   s    

z(HuggingFaceHubEmbeddings.embed_documentsc                    sD   dd |D }| j pi }| jj||d| jdI dH }t| S )zAsync Call to HuggingFaceHub's embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                 S   s   g | ]}| d dqS r+   r.   r0   r   r   r#   r3      r4   z=HuggingFaceHubEmbeddings.aembed_documents.<locals>.<listcomp>)r5   
parametersr6   N)r   r   r8   r   r7   r9   r:   r;   r   r   r#   aembed_documentsx   s    

z)HuggingFaceHubEmbeddings.aembed_documents)r2   r   c                 C   s   |  |gd }|S )zCall out to HuggingFaceHub's embedding endpoint for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r=   r(   r2   responser   r   r#   embed_query   s    	z$HuggingFaceHubEmbeddings.embed_queryc                    s   |  |gI dH d }|S )zAsync Call to HuggingFaceHub's embedding endpoint for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        Nr   )r?   r@   r   r   r#   aembed_query   s    	z%HuggingFaceHubEmbeddings.aembed_query)__name__
__module____qualname____doc__r   r   __annotations__r   r   r   strr   r   r   dictr   r
   Zmodel_configr   classmethodr   r$   r   r)   r   floatr=   r?   rB   rC   r   r   r   r#   r      s$   
%	r   )r7   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr	   r
   r   Ztyping_extensionsr   r    r&   r   r   r   r   r#   <module>   s   