
    Bvh                          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y)	    )
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y)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api_key
model_nameapi_key_env_varc                    	 ddl }|t        j                  dt
               || _        |xs t        j                  |      | _	        | j                  st        d| d      || _
        d| | _        |j                         | _        | j                  j                  j                  dd	| j                   i       y# t        $ r t        d      w xY w)
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.zThe z! environment variable is not set.zAhttps://api-inference.huggingface.co/pipeline/feature-extraction/AuthorizationBearer )httpxImportError
ValueErrorwarningswarnDeprecationWarningr   osgetenvr   r   _api_urlClient_sessionheadersupdate)selfr   r   r   r   s        k/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/huggingface_embedding_function.py__init__z%HuggingFaceEmbeddingFunction.__init__   s    	 MM_"
  /<"))O"<||tO#44UVWW$[\f[gh$$o7O%PQ)  	g 	s   B= =Cinputreturnc                     | j                   j                  | j                  |ddid      j                         }|D cg c]'  }t	        j
                  |t        j                        ) c}S c c}w )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jsondtyper!   postr   r.   nparrayfloat32r$   r'   response	embeddings       r%   __call__z%HuggingFaceEmbeddingFunction.__call__6   si      ==%%MM!/?.FG & 
 $& 	 HPPx)"**5xPPPs   ,A.c                       y)Nhuggingface r<       r%   namez!HuggingFaceEmbeddingFunction.nameN   s    r=   c                      yNcosiner<   r$   s    r%   default_spacez*HuggingFaceEmbeddingFunction.default_spaceR       r=   c                 
    g dS N)rA   l2ipr<   rB   s    r%   supported_spacesz-HuggingFaceEmbeddingFunction.supported_spacesU       %%r=   configEmbeddingFunction[Documents]c                 v    | j                  d      }| j                  d      }||J d       t        ||      S )Nr   r   zThis code should not be reachedr   r   )getr   )rK   r   r   s      r%   build_from_configz.HuggingFaceEmbeddingFunction.build_from_configX   sG     **%67ZZ-
"j&8;;;5++

 	
r=   c                 4    | j                   | j                  dS )NrN   rN   rB   s    r%   
get_configz'HuggingFaceEmbeddingFunction.get_configd   s    #'#7#7tWWr=   
old_config
new_configc                 "    d|v rt        d      y )Nr   zSThe model name cannot be changed after the embedding function has been initialized.)r   r$   rS   rT   s      r%   validate_config_updatez3HuggingFaceEmbeddingFunction.validate_config_updateg   s!     :%e  &r=   c                     t        | d       y)
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        r;   Nr   rK   s    r%   validate_configz,HuggingFaceEmbeddingFunction.validate_configo   s     	v}5r=   )Nz&sentence-transformers/all-MiniLM-L6-v2CHROMA_HUGGINGFACE_API_KEY)__name__
__module____qualname____doc__r
   strr&   r   r   r9   staticmethodr>   r   rC   r   rI   r   r	   rP   rR   rW   r[   r<   r=   r%   r   r   	   s!    "&B;	%R#%R %R 	%RNQi QJ Q0 #  u &$u+ & 	
$sCx. 	
5S 	
 	
XDcN XsCx.6:38n	 
6S#X 
64 
6 
6r=   r   c                       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y)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 }|t        j                  dt
               || _        || _        | j                  )|xs t        j                  | j                        | _
        n|| _
        | | _        |j                         | _        | j                  5| j                  j                  j                  dd| j                   i       yy# t        $ r t        d      w xY w)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   re   r   r   r   r   r   r    r!   r"   r#   )r$   re   r   r   r   s        r%   r&   z#HuggingFaceEmbeddingServer.__init__   s    	 MM_" .+"Ebii0D0D&EDL"DL%<<#MM!!((/WT\\N;S)TU $/  	g 	s   C C r'   r(   c                     | j                   j                  | j                  d|i      j                         }|D cg c]'  }t	        j
                  |t        j                        ) c}S c c}w )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+   r-   r/   r1   r6   s       r%   r9   z#HuggingFaceEmbeddingServer.__call__   sZ      ==%%dmm8U:K%LQQS HPPx)"**5xPPPs   ,A+c                       y)Nhuggingface_serverr<   r<   r=   r%   r>   zHuggingFaceEmbeddingServer.name   s    #r=   c                      yr@   r<   rB   s    r%   rC   z(HuggingFaceEmbeddingServer.default_space   rD   r=   c                 
    g dS rF   r<   rB   s    r%   rI   z+HuggingFaceEmbeddingServer.supported_spaces   rJ   r=   rK   rL   c                 z    | j                  d      }| j                  d      }|t        d      t        ||      S )Nre   r   z3URL must be provided for HuggingFaceEmbeddingServerre   r   )rO   r   rd   )rK   re   r   s      r%   rP   z,HuggingFaceEmbeddingServer.build_from_config   s=    jj **%67;RSS)c?SSr=   c                 4    | j                   | j                  dS )Nrm   rm   rB   s    r%   rR   z%HuggingFaceEmbeddingServer.get_config   s    xxD4H4HIIr=   rS   rT   c                 H    d|v r|d   | j                   k7  rt        d      y y )Nre   zLThe URL cannot be changed after the embedding function has been initialized.)re   r   rV   s      r%   rW   z1HuggingFaceEmbeddingServer.validate_config_update   s5     J:e#4#@^  $Ar=   c                     t        | d       y)rY   ri   Nr   rZ   s    r%   r[   z*HuggingFaceEmbeddingServer.validate_config   s     	v';<r=   )NN)r]   r^   r_   r`   ra   r
   r&   r   r   r9   rb   r>   r   rC   r   rI   r   r	   rP   rR   rW   r[   r<   r=   r%   rd   rd   }   s&    *.!%	(V(V "#(V #	(VTQi QJ Q* $# $ $u &$u+ & T$sCx. T5S T TJDcN JsCx.6:38n	 
=S#X 
=4 
= 
=r=   rd   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r   numpyr3   *chromadb.utils.embedding_functions.schemasr   r   r   rd   r<   r=   r%   <module>ru      sF    N N , , 	  M q6#4Y#? q6ho=!29!= o=r=   