
    h                        d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZmZ d dlmZ dZd	Z G d
 dee      Zy)    )annotationsN)AnyOptional)
Embeddings)from_env)	BaseModel
ConfigDictFieldmodel_validator)Selfz'sentence-transformers/all-mpnet-base-v2)feature-extractionc                      e Zd ZU dZdZded<   dZded<   dZded<   	 dZded<   	 dZ	ded	<   	 d
Z
ded<   	 dZded<   	  e edd            Zded<    edd      Z ed      dd       ZddZddZddZddZy)HuggingFaceEndpointEmbeddingsay  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_huggingface import HuggingFaceEndpointEmbeddings
            model = "sentence-transformers/all-mpnet-base-v2"
            hf = HuggingFaceEndpointEmbeddings(
                model=model,
                task="feature-extraction",
                huggingfacehub_api_token="my-api-key",
            )

    Nr   clientasync_clientzOptional[str]modelproviderrepo_idr   taskzOptional[dict]model_kwargsHUGGINGFACEHUB_API_TOKEN)default)default_factoryhuggingfacehub_api_tokenforbid )extraprotected_namespacesafter)modec                @   | j                   xs t        j                  d      }	 ddlm}m} | j                  r| j                  | _        n4| j                  r| j                  | _        nt        | _        t        | _         || j                  || j                        } || j                  || j                        }| j                  t        vr"d| j                   dt         d}t        |      || _        || _        | S # t        $ r}d}t        |      |d	}~ww xY w)
z?Validate that api key and python package exists in environment.HF_TOKENr   )AsyncInferenceClientInferenceClient)r   tokenr   zGot invalid task z, currently only z are supportedzfCould not import huggingface_hub python package. Please install it with `pip install huggingface_hub`.N)r   osgetenvhuggingface_hubr#   r$   r   r   DEFAULT_MODELr   r   VALID_TASKS
ValueErrorr   r   ImportError)selfr   r#   r$   r   r   msges           t/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/langchain_huggingface/embeddings/huggingface_endpoint.pyvalidate_environmentz2HuggingFaceEndpointEmbeddings.validate_environment;   s    $(#@#@ $
BIIE
 (	*
 zz#zz!\\
*
,$jj.F 0jj.L yy+'		{ 3&&1].B  !o% DK ,D   	*H  c")	*s   CD 	D
DDc                    |D cg c]  }|j                  dd       }}| j                  xs i } | j                  j                  dd|i|}|j	                         S c c}w )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.

        
 textr   )replacer   r   feature_extractiontolistr-   textsr5   _model_kwargs	responsess        r0   embed_documentsz-HuggingFaceEndpointEmbeddings.embed_documentsm   se     6;;UTdC(U;))/R2DKK22OOO	!!	 <s   Ac                   K   |D cg c]  }|j                  dd       }}| j                  xs i } | j                  j                  dd|i| d{   }|j	                         S c c}w 7 w)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.

        r3   r4   r5   Nr   )r6   r   r   r7   r8   r9   s        r0   aembed_documentsz.HuggingFaceEndpointEmbeddings.aembed_documents~   s      6;;UTdC(U;))/R>$++>> 

'
 
	 !! <
s   A/A(3A/A-A/c                ,    | j                  |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-   r5   s     r0   embed_queryz)HuggingFaceEndpointEmbeddings.embed_query   s     ##TF+A..    c                H   K   | j                  |g       d{   d   S 7 w)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   s     r0   aembed_queryz*HuggingFaceEndpointEmbeddings.aembed_query   s&      ++TF33Q773s   " ")returnr   )r:   z	list[str]rF   zlist[list[float]])r5   strrF   zlist[float])__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r
   r   r   r	   model_configr   r1   r=   r?   rB   rE   r   rC   r0   r   r      s    & FCL#E="Hm"p "G]!C.D-.&#'L.'1.3 !;TJ/m  L
 '"/ #/b"""$
/
8rC   r   )
__future__r   r&   typingr   r   langchain_core.embeddingsr   langchain_core.utilsr   pydanticr   r	   r
   r   typing_extensionsr   r)   r*   r   r   rC   r0   <module>rT      s7    " 	   0 ) B B "9%W8Iz W8rC   