
    _j!                         d dl mZmZmZmZ d dlmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlZ G d dee                   Z G d dee                   ZdS )	    )
Embeddings	DocumentsEmbeddingFunctionSpace)ListDictAnyOptionalNvalidate_config_schemac                   :   e Zd ZdZ	 	 	 ddee         dedefdZd	ed
efdZ	e
d
efd            Zd
efdZd
ee         fdZe
deeef         d
dfd            Zd
eeef         fdZdeeef         deeef         d
dfdZe
deeef         d
dfd            ZdS )HuggingFaceEmbeddingFunctionz
    This class is used to get embeddings for a list of texts using the HuggingFace API.
    It requires an API key and a model name. The default model name is "sentence-transformers/all-MiniLM-L6-v2".
    N&sentence-transformers/all-MiniLM-L6-v2CHROMA_HUGGINGFACE_API_KEYapi_key
model_nameapi_key_env_varc                    	 ddl }n# t          $ r t          d          w xY w|t          j        dt
                     t          j        d          d| _        n|| _        |pt          j        | j                  | _	        | j	        st          d| j         d          || _
        d| | _        |                                | _        | j        j                            d	d
| j	         i           dS )a  
        Initialize the HuggingFaceEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the HuggingFace API.
                Defaults to "CHROMA_HUGGINGFACE_API_KEY".
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "sentence-transformers/all-MiniLM-L6-v2".
        r   NUThe httpx python package is not installed. Please install it with `pip install httpx`Direct api_key configuration will not be persisted. Please use environment variables via api_key_env_var for persistent storage.HUGGINGFACE_API_KEYzThe z! environment variable is not set.zAhttps://api-inference.huggingface.co/pipeline/feature-extraction/AuthorizationBearer )httpxImportError
ValueErrorwarningswarnDeprecationWarningosgetenvr   r   r   _api_urlClient_sessionheadersupdate)selfr   r   r   r   s        ~/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/huggingface_embedding_function.py__init__z%HuggingFaceEmbeddingFunction.__init__   s*   	LLLL 	 	 	g  	
 M_"  
 9*++7#8D  #2D A")D,@"A"A| 	Nt+NNN   %h\fhh$$o7O7O7O%PQQQQQ    !inputreturnc                     | j                             | j        |ddid                                          }d |D             S )a  
        Get the embeddings for a list of texts.

        Args:
            input (Documents): A list of texts to get embeddings for.

        Returns:
            Embeddings: The embeddings for the texts.

        Example:
            >>> hugging_face = HuggingFaceEmbeddingFunction(api_key_env_var="CHROMA_HUGGINGFACE_API_KEY")
            >>> texts = ["Hello, world!", "How are you?"]
            >>> embeddings = hugging_face(texts)
        wait_for_modelT)inputsoptionsjsonc                 N    g | ]"}t          j        |t           j                   #S )dtypenparrayfloat32.0	embeddings     r(   
<listcomp>z9HuggingFaceEmbeddingFunction.__call__.<locals>.<listcomp>R   )    PPP)"*555PPP    r$   postr"   r2   r'   r+   responses      r(   __call__z%HuggingFaceEmbeddingFunction.__call__<   s[      =%%M!/?.FGG & 
 
 $&& 	 QPxPPPPr?   c                      dS )Nhuggingface rG   r?   r(   namez!HuggingFaceEmbeddingFunction.nameT   s    }r?   c                     dS NcosinerG   r'   s    r(   default_spacez*HuggingFaceEmbeddingFunction.default_spaceX       xr?   c                 
    g dS N)rK   l2iprG   rL   s    r(   supported_spacesz-HuggingFaceEmbeddingFunction.supported_spaces[       %%%%r?   configEmbeddingFunction[Documents]c                     |                      d          }|                      d          }||
J d            t          ||          S )Nr   r   FzThis code should not be reachedr   r   )getr   )rU   r   r   s      r(   build_from_configz.HuggingFaceEmbeddingFunction.build_from_config^   s[     **%677ZZ--
"j&8;;;;;++

 
 
 	
r?   c                      | j         | j        dS )NrX   rX   rL   s    r(   
get_configz'HuggingFaceEmbeddingFunction.get_configj   s    #'#7tWWWr?   
old_config
new_configc                 ,    d|v rt          d          d S )Nr   zSThe model name cannot be changed after the embedding function has been initialized.)r   r'   r]   r^   s      r(   validate_config_updatez3HuggingFaceEmbeddingFunction.validate_config_updatem   s,     :%%e   &%r?   c                 &    t          | d           dS )
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        rF   Nr   rU   s    r(   validate_configz,HuggingFaceEmbeddingFunction.validate_configu   s     	v}55555r?   )Nr   r   )__name__
__module____qualname____doc__r
   strr)   r   r   rD   staticmethodrH   r   rM   r   rS   r   r	   rZ   r\   ra   re   rG   r?   r(   r   r   	   s         "&B;	+R +R#+R +R 	+R +R +R +RZQi QJ Q Q Q Q0 #    \u    &$u+ & & & & 	
$sCx. 	
5S 	
 	
 	
 \	
XDcN X X X XsCx.6:38n	    
6S#X 
64 
6 
6 
6 \
6 
6 
6r?   r   c                   D   e Zd ZdZ	 	 ddedee         dee         fdZdedefd	Z	e
defd
            ZdefdZdee         fdZe
deeef         ddfd            Zdeeef         fdZdeeef         deeef         ddfdZe
deeef         ddfd            ZdS )HuggingFaceEmbeddingServerz
    This class is used to get embeddings for a list of texts using the HuggingFace Embedding server
    (https://github.com/huggingface/text-embeddings-inference).
    The embedding model is configured in the server.
    Nurlr   r   c                    	 ddl }n# t          $ r t          d          w xY w|t          j        dt
                     || _        || _        t          j	        d          d| _        | j        !|pt          j	        | j                  | _
        n|| _
        | | _        |                                | _        | j
        +| j        j                            dd| j
         i           dS dS )aa  
        Initialize the HuggingFaceEmbeddingServer.

        Args:
            url (str): The URL of the HuggingFace Embedding Server.
            api_key (Optional[str]): The API key for the HuggingFace Embedding Server.
            api_key_env_var (str, optional): Environment variable name that contains your API key for the HuggingFace API.
        r   Nr   r   r   r   r   )r   r   r   r   r   r   rn   r   r    r!   r   r"   r#   r$   r%   r&   )r'   rn   r   r   r   s        r(   r)   z#HuggingFaceEmbeddingServer.__init__   s   	LLLL 	 	 	g  	
 M_"   .9*++7#8D +"Ebi0D&E&EDLL"DL<#M!((/;ST\;S;S)TUUUUU $#r*   r+   r,   c                     | j                             | j        d|i                                          }d |D             S )a  
        Get the embeddings for a list of texts.

        Args:
            input (Documents): A list of texts to get embeddings for.

        Returns:
            Embeddings: The embeddings for the texts.

        Example:
            >>> hugging_face = HuggingFaceEmbeddingServer(url="http://localhost:8080/embed")
            >>> texts = ["Hello, world!", "How are you?"]
            >>> embeddings = hugging_face(texts)
        r/   r1   c                 N    g | ]"}t          j        |t           j                   #S r4   r6   r:   s     r(   r=   z7HuggingFaceEmbeddingServer.__call__.<locals>.<listcomp>   r>   r?   r@   rB   s      r(   rD   z#HuggingFaceEmbeddingServer.__call__   sH      =%%dm8U:K%LLQQSS QPxPPPPr?   c                      dS )Nhuggingface_serverrG   rG   r?   r(   rH   zHuggingFaceEmbeddingServer.name   s    ##r?   c                     dS rJ   rG   rL   s    r(   rM   z(HuggingFaceEmbeddingServer.default_space   rN   r?   c                 
    g dS rP   rG   rL   s    r(   rS   z+HuggingFaceEmbeddingServer.supported_spaces   rT   r?   rU   rV   c                     |                      d          }|                      d          }|t          d          t          ||          S )Nrn   r   z3URL must be provided for HuggingFaceEmbeddingServerrn   r   )rY   r   rm   )rU   rn   r   s      r(   rZ   z,HuggingFaceEmbeddingServer.build_from_config   sL    jj **%677;RSSS)c?SSSSr?   c                      | j         | j        dS )Nrw   rw   rL   s    r(   r\   z%HuggingFaceEmbeddingServer.get_config   s    xD4HIIIr?   r]   r^   c                 R    d|v r |d         | j         k    rt          d          d S d S )Nrn   zLThe URL cannot be changed after the embedding function has been initialized.)rn   r   r`   s      r(   ra   z1HuggingFaceEmbeddingServer.validate_config_update   sC     J:e#4#@#@^   #@#@r?   c                 &    t          | d           dS )rc   rs   Nr   rd   s    r(   re   z*HuggingFaceEmbeddingServer.validate_config   s     	v';<<<<<r?   )NN)rf   rg   rh   ri   rj   r
   r)   r   r   rD   rk   rH   r   rM   r   rS   r   r	   rZ   r\   ra   re   rG   r?   r(   rm   rm      s         *.!%	+V +V+V "#+V #	+V +V +V +VZQi QJ Q Q Q Q* $# $ $ $ \$u    &$u+ & & & & T$sCx. T5S T T T \TJDcN J J J JsCx.6:38n	    
=S#X 
=4 
= 
= 
= \
= 
= 
=r?   rm   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r    numpyr7   *chromadb.utils.embedding_functions.schemasr   r   r   rm   rG   r?   r(   <module>r      s    N N N N N N N N N N N N , , , , , , , , , , , , 				     M M M M M M w6 w6 w6 w6 w6#4Y#? w6 w6 w6tr= r= r= r= r=!29!= r= r= r= r= r=r?   