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	 d dl
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)	BaseModelroot_validator)get_from_dict_or_envz'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eed< 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
< G dd dZeddeedddZeddde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",
            )
    clientasync_clientNmodelrepo_idr   taskmodel_kwargshuggingfacehub_api_tokenc                   @   s   e Zd ZdZdS )zHuggingFaceHubEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   R/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/huggingface_hub.pyConfig2   s   r   T)pre)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k
r   td
Y nX |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   r   validate_environment5   s8      


z-HuggingFaceHubEmbeddings.validate_environmentF)r   Zskip_on_failurec                 C   s*   |d t kr&td|d  dt  d|S )z#Post init validation for the class.r   zGot invalid task z, currently only z are supported)VALID_TASKS
ValueError)r$   r   r   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>o   s     z<HuggingFaceHubEmbeddings.embed_documents.<locals>.<listcomp>inputsjsonr   )r   r   postr   r5   loadsdecodeselfr)   Z_model_kwargs	responsesr   r   r   embed_documentse   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-   r/   r   r   r   r2      s     z=HuggingFaceHubEmbeddings.aembed_documents.<locals>.<listcomp>)r3   
parametersr4   N)r   r   r6   r   r5   r7   r8   r9   r   r   r   aembed_documentsw   s    

 z)HuggingFaceHubEmbeddings.aembed_documents)r1   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:   r1   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)r   r   r   __doc__r   __annotations__r   r   strr   r   r   dictr   r   r	   r   r%   r(   r   floatr<   r>   rA   rB   r   r   r   r   r      s"   
%
	r   )r5   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r	   Zlangchain_core.utilsr
   r"   r&   r   r   r   r   r   <module>   s   