
    +#h                         d dl Z d dlZd dlmZ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)    N)AnyList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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	<   	  e ed
d            Ze	e
   ed<    ed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y)HuggingFaceEndpointEmbeddingsax  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",
            )
    Nclientasync_clientmodelrepo_idr   taskmodel_kwargsHUGGINGFACEHUB_API_TOKEN)default)default_factoryhuggingfacehub_api_tokenforbid )extraprotected_namespacesafter)modereturnc                    | j                   xs t        j                  d      }	 ddlm}m} | j                  r| j                  | _        n4| j                  r| j                  | _        nt        | _        t        | _         || j                  |      } || j                  |      }| j                  t        vr t        d| j                   dt         d      || _        || _        | S # t        $ r t        d      w xY w)	z?Validate that api key and python package exists in environment.HF_TOKENr   )AsyncInferenceClientInferenceClient)r   tokenzGot invalid task z, currently only z are supportedzfCould not import huggingface_hub python package. Please install it with `pip install huggingface_hub`.)r   osgetenvhuggingface_hubr#   r$   r   r   DEFAULT_MODELr   VALID_TASKS
ValueErrorr   r   ImportError)selfr   r#   r$   r   r   s         r/var/www/html/sandstorm/venv/lib/python3.12/site-packages/langchain_huggingface/embeddings/huggingface_endpoint.pyvalidate_environmentz2HuggingFaceEndpointEmbeddings.validate_environment5   s     $(#@#@ $
BIIE
 $	
 zz#zz!\\
*
,$jj.F
 0jj.L
 yy+ '		{ 3&&1].B  !DK ,D   	H 	s   CC) )C>textsc                    |D cg c]  }|j                  dd       }}| j                  xs i }| j                  j                  d|i|| j                        }t        j                  |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.
        
 inputsjsonr   )replacer   r   postr   r6   loadsdecoder-   r0   text_model_kwargs	responsess        r.   embed_documentsz-HuggingFaceEndpointEmbeddings.embed_documentsc   s}     6;;TdC(;;))/RKK$$E3]3$)) % 
	 zz)**,-- <s   A>c                    K   |D cg c]  }|j                  dd       }}| j                  xs i }| j                  j                  ||d| j                         d{   }t        j                  |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.
        r2   r3   )r4   
parametersr5   N)r7   r   r   r8   r   r6   r9   r:   r;   s        r.   aembed_documentsz.HuggingFaceEndpointEmbeddings.aembed_documentsu   s      6;;TdC(;;))/R++00!?dii 1 
 
	 zz)**,-- <
s   BB?BB -Br<   c                 0    | 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-   r<   responses      r.   embed_queryz)HuggingFaceEndpointEmbeddings.embed_query   s      ''/2    c                 L   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   )rB   rD   s      r.   aembed_queryz*HuggingFaceEndpointEmbeddings.aembed_query   s,      //77; 8s   $"
$)__name__
__module____qualname____doc__r   r   __annotations__r   r   r   strr   r   r   dictr
   r   r   r	   model_configr   r   r/   r   floatr?   rB   rF   rI   r   rG   r.   r   r      s#   $ FCL#E8C=!GXc]!C.D(3-.&#'L(4.'1.3 !;TJ/hsm  L
 '"+d + #+Z.T#Y .4U3D .$.DI .$tE{:K ."
 
U 

s 
tE{ 
rG   r   )r6   r&   typingr   r   r   langchain_core.embeddingsr   langchain_core.utilsr   pydanticr   r	   r
   r   typing_extensionsr   r)   r*   r   r   rG   r.   <module>rX      s:     	 & & 0 ) B B "9%NIz NrG   