
    Ng                         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           G 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)sinceremovalalternative_importc                      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          Z ed          ededefd                        Z ed          defd            Zdee
         deee                  fdZdee
         deee                  fdZde
dee         fdZde
dee         f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_tokenforbid )extraprotected_namespacesbefore)modevaluesreturnc                    t          |dd          }	 ddlm}m} |                    d          r|d         |d<   n5|                    d          r|d         |d<   nt
          |d<   t
          |d<    ||d         |          } ||d         |          }||d<   ||d	<   n# t          $ r t          d
          w xY w|S )z?Validate that api key and python package exists in environment.r   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	   huggingface_hubr%   r&   getDEFAULT_MODELImportError)clsr!   r   r%   r&   r   r   s          j/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/huggingface_hub.pyvalidate_environmentz-HuggingFaceHubEmbeddings.validate_environment5   s,    $8.0J$
 $
 	MMMMMMMMzz'"" 2$*7Oy!!I&& 2"("3w"/w$1y!$_Wo.  F
 0/Wo.  L
  &F8%1F>"" 	 	 	H  	
 s   BB" "B<afterc                 b    | 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)selfs    r-   	post_initz"HuggingFaceHubEmbeddings.post_init\   sO     9K''>DI > >"-> > >       textsc                     d |D             }| j         pi }| j                            d|i|| j                  }t	          j        |                                          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                 :    g | ]}|                     d d          S 
 replace.0texts     r-   
<listcomp>z<HuggingFaceHubEmbeddings.embed_documents.<locals>.<listcomp>p   &    ;;;TdC((;;;r5   inputsjsonr   )r   r   postr   rE   loadsdecoder3   r6   _model_kwargs	responsess       r-   embed_documentsz(HuggingFaceHubEmbeddings.embed_documentsf   so     <;U;;;)/RK$$E3]3$) % 
 
	 z)**,,---r5   c                    K   d |D             }| j         pi }| j                            ||d| j                   d{V }t	          j        |                                          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                 :    g | ]}|                     d d          S r9   r<   r>   s     r-   rA   z=HuggingFaceHubEmbeddings.aembed_documents.<locals>.<listcomp>   rB   r5   )rC   
parametersrD   N)r   r   rF   r   rE   rG   rH   rI   s       r-   aembed_documentsz)HuggingFaceHubEmbeddings.aembed_documentsx   s       <;U;;;)/R+00!??di 1 
 
 
 
 
 
 
 
	 z)**,,---r5   r@   c                 >    |                      |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   )rL   r3   r@   responses      r-   embed_queryz$HuggingFaceHubEmbeddings.embed_query   s#     ''//2r5   c                 N   K   |                      |g           d{V 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   )rP   rR   s      r-   aembed_queryz%HuggingFaceHubEmbeddings.aembed_query   s9       //77777777;r5   )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   strr   r   r   dictr   r   model_configr   classmethodr   r.   r   r4   r   floatrL   rP   rT   rV   r   r5   r-   r   r      s         $ FCL#E8C=!GXc]!!!C.D(3-...&#'L(4.'''1.2hsm222:H2FFFL_(####$ #3 # # # [ $##J _'"""4    #".T#Y .4U3D . . . .$.DI .$tE{:K . . . ."
 
U 
 
 
 

s 
tE{ 
 
 
 
 
 
r5   r   )rE   typingr   r   r   r   langchain_core._apir   langchain_core.embeddingsr   langchain_core.utilsr	   pydanticr
   r   r   typing_extensionsr   r*   r1   r   r   r5   r-   <module>rg      s    , , , , , , , , , , , , * * * * * * 0 0 0 0 0 0 5 5 5 5 5 5 ; ; ; ; ; ; ; ; ; ; " " " " " "9% 
L  
L L L L Ly* L L 
L L Lr5   