
    3i                          S SK JrJrJrJr  S SKJrJrJrJ	r	  S SK
r
S SKrS SKJr  S SKr " S S\\   5      r " S S\\   5      rg)	    )
Embeddings	DocumentsEmbeddingFunctionSpace)ListDictAnyOptionalNvalidate_config_schemac                      \ rS rSrSr   SS\\   S\S\4S jjrS\S	\	4S
 jr
\S	\4S j5       rS	\4S jrS	\\   4S jr\S\\\4   S	S4S j5       rS	\\\4   4S jrS\\\4   S\\\4   S	S4S jr\S\\\4   S	S4S j5       rSrg)HuggingFaceEmbeddingFunction	   z
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                     SSK nUb  [        R                  " S[
        5        X0l        U=(       d    [        R                  " U5      U l	        U R                  (       d  [        SU S35      eX l
        SU 3U l        UR                  5       U l        U R                  R                  R                  SS	U R                   305        g! [         a    [        S5      ef = f)
aj  
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        {/home/james-whalen/.local/lib/python3.13/site-packages/chromadb/utils/embedding_functions/huggingface_embedding_function.py__init__%HuggingFaceEmbeddingFunction.__init__   s    	 MM_"
  /<"))O"<||tO#44UVWW$[\f[gh$$o7O%PQ)  	g 	s   C Cinputreturnc                     U R                   R                  U R                  USS0S.S9R                  5       nU Vs/ s H&  n[        R
                  " U[        R                  S9PM(     sn$ s  snf )am  
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    r0   nparrayfloat32r%   r)   response	embeddings       r&   __call__%HuggingFaceEmbeddingFunction.__call__6   si      ==%%MM!/?.FG & 
 $& 	 HPPx)"**5xPPPs   -A,c                      g)Nhuggingface r?       r&   name!HuggingFaceEmbeddingFunction.nameN   s    r@   c                     gNcosiner?   r%   s    r&   default_space*HuggingFaceEmbeddingFunction.default_spaceR       r@   c                 
    / SQ$ N)rE   l2ipr?   rF   s    r&   supported_spaces-HuggingFaceEmbeddingFunction.supported_spacesU       %%r@   configEmbeddingFunction[Documents]c                 r    U R                  S5      nU R                  S5      nUb  Uc   S5       e[        XS9$ )Nr   r   zThis code should not be reachedr   r   )getr   )rQ   r   r   s      r&   build_from_config.HuggingFaceEmbeddingFunction.build_from_configX   sE     **%67ZZ-
"j&8;;;5++
 	
r@   c                 4    U R                   U R                  S.$ )NrT   rT   rF   s    r&   
get_config'HuggingFaceEmbeddingFunction.get_configd   s    #'#7#7tWWr@   
old_config
new_configc                 &    SU;   a  [        S5      eg )Nr   zSThe model name cannot be changed after the embedding function has been initialized.)r   r%   r[   r\   s      r&   validate_config_update3HuggingFaceEmbeddingFunction.validate_config_updateg   s!     :%e  &r@   c                     [        U S5        g)
Validate the configuration using the JSON schema.

Args:
    config: Configuration to validate

Raises:
    ValidationError: If the configuration does not match the schema
r>   Nr   rQ   s    r&   validate_config,HuggingFaceEmbeddingFunction.validate_configo   s     	v}5r@   )r    r"   r   r   r   )Nz&sentence-transformers/all-MiniLM-L6-v2CHROMA_HUGGINGFACE_API_KEY)__name__
__module____qualname____firstlineno____doc__r
   strr'   r   r   r;   staticmethodrA   r   rG   r   rN   r   r	   rV   rY   r_   rd   __static_attributes__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                      \ rS rSrSr  SS\S\\   S\\   4S jjrS\S	\	4S
 jr
\S	\4S j5       rS	\4S jrS	\\   4S jr\S\\\4   S	S4S j5       rS	\\\4   4S jrS\\\4   S\\\4   S	S4S jr\S\\\4   S	S4S j5       rSrg)HuggingFaceEmbeddingServer}   z
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                     SSK nUb  [        R                  " S[
        5        Xl        X l        U R                  b/  U=(       d     [        R                  " U R                  5      U l
        OX0l
        U U l        UR                  5       U l        U R                  b5  U R                  R                  R                  SSU R                   305        gg! [         a    [        S5      ef = f)a1  
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   rr   r   r   r   r   r    r!   r"   r#   r$   )r%   rr   r   r   r   s        r&   r'   #HuggingFaceEmbeddingServer.__init__   s    	 MM_" .+"Ebii0D0D&EDL"L%<<#MM!!((/WT\\N;S)TU $/  	g 	s   C C(r)   r*   c                     U R                   R                  U R                  SU0S9R                  5       nU Vs/ s H&  n[        R
                  " U[        R                  S9PM(     sn$ s  snf )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/   r1   r3   r8   s       r&   r;   #HuggingFaceEmbeddingServer.__call__   sZ      ==%%dmm8U:K%LQQS HPPx)"**5xPPPs   -A)c                      g)Nhuggingface_serverr?   r?   r@   r&   rA   HuggingFaceEmbeddingServer.name   s    #r@   c                     grD   r?   rF   s    r&   rG   (HuggingFaceEmbeddingServer.default_space   rI   r@   c                 
    / SQ$ rK   r?   rF   s    r&   rN   +HuggingFaceEmbeddingServer.supported_spaces   rP   r@   rQ   rR   c                 t    U R                  S5      nU R                  S5      nUc  [        S5      e[        XS9$ )Nrr   r   z3URL must be provided for HuggingFaceEmbeddingServerrr   r   )rU   r   rp   )rQ   rr   r   s      r&   rV   ,HuggingFaceEmbeddingServer.build_from_config   s;    jj **%67;RSS)cSSr@   c                 4    U R                   U R                  S.$ )Nr   r   rF   s    r&   rY   %HuggingFaceEmbeddingServer.get_config   s    xxD4H4HIIr@   r[   r\   c                 N    SU;   a  US   U R                   :w  a  [        S5      eg g )Nrr   zLThe URL cannot be changed after the embedding function has been initialized.)rr   r   r^   s      r&   r_   1HuggingFaceEmbeddingServer.validate_config_update   s5     J:e#4#@^  $Ar@   c                     [        U S5        g)rb   rx   Nr   rc   s    r&   rd   *HuggingFaceEmbeddingServer.validate_config   s     	v';<r@   )r    r"   r   r   rr   )NN)rg   rh   ri   rj   rk   rl   r
   r'   r   r   r;   rm   rA   r   rG   r   rN   r   r	   rV   rY   r_   rd   rn   r?   r@   r&   rp   rp   }   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@   rp   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r   numpyr5   *chromadb.utils.embedding_functions.schemasr   r   r   rp   r?   r@   r&   <module>r      sF    N N , , 	  M q6#4Y#? q6ho=!29!= o=r@   