o
    Zh                     @   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dedefddZedddefddZdee
 dee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e
dee f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}zCddlm}m} |dr|d |d< n|dr'|d |d< nt|d< t|d< ||d |d}||d |d}||d< ||d	< W |S  tyT   td
w )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   e/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/huggingface_hub.pyvalidate_environment5   s6   


z-HuggingFaceHubEmbeddings.validate_environmentafterc                 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textsc                 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      g | ]}| d dqS 
 replace.0textr   r   r#   
<listcomp>p       z<HuggingFaceHubEmbeddings.embed_documents.<locals>.<listcomp>inputsjsonr   )r   r   postr   r8   loadsdecoder(   r*   Z_model_kwargs	responsesr   r   r#   embed_documentsf   s   

z(HuggingFaceHubEmbeddings.embed_documentsc                    sF   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   r+   r,   r/   r1   r   r   r#   r4      r5   z=HuggingFaceHubEmbeddings.aembed_documents.<locals>.<listcomp>)r6   
parametersr7   N)r   r   r9   r   r8   r:   r;   r<   r   r   r#   aembed_documentsx   s   

z)HuggingFaceHubEmbeddings.aembed_documentsr3   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(   r3   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@   rA   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@   rC   rD   r   r   r   r#   r      s.   
 %	r   )r8   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    