
    ug!                         d dl Z d dlmZ d dlZd dlmZmZmZ  e j                  e	      Z
 G d dee         Z G d dee         Zy)    N)cast)	DocumentsEmbeddingFunction
Embeddingsc                   4    e Zd ZdZ	 d	dedefdZdede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".
    api_key
model_namec                     d| | _         t        j                         | _        | j                  j                  j                  dd| i       y)a  
        Initialize the HuggingFaceEmbeddingFunction.

        Args:
            api_key (str): Your API key for the HuggingFace API.
            model_name (str, optional): The name of the model to use for text embeddings. Defaults to "sentence-transformers/all-MiniLM-L6-v2".
        zAhttps://api-inference.huggingface.co/pipeline/feature-extraction/AuthorizationzBearer N)_api_urlhttpxClient_sessionheadersupdate)selfr	   r
   s      v/var/www/openai/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/huggingface_embedding_function.py__init__z%HuggingFaceEmbeddingFunction.__init__   sF     \\f[gh$$o	7J%KL    inputreturnc           	          t        t        | j                  j                  | j                  |ddid      j                               S )a  
        Get the embeddings for a list of texts.

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

        Returns:
            Embeddings: The embeddings for the texts.

        Example:
            >>> hugging_face = HuggingFaceEmbeddingFunction(api_key="your_api_key")
            >>> texts = ["Hello, world!", "How are you?"]
            >>> embeddings = hugging_face(texts)
        wait_for_modelT)inputsoptionsjsonr   r   r   postr   r   r   r   s     r   __call__z%HuggingFaceEmbeddingFunction.__call__   sI      MM %3CT2JK   df
 	
r   N)z&sentence-transformers/all-MiniLM-L6-v2	__name__
__module____qualname____doc__strr   r   r   r"    r   r   r   r      s8     /WMM(+M
i 
J 
r   r   c                   ,    e Zd ZdZdefdZdede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.
    urlc                 F    | | _         t        j                         | _        y)z
        Initialize the HuggingFaceEmbeddingServer.

        Args:
            url (str): The URL of the HuggingFace Embedding Server.
        N)r   r   r   r   )r   r,   s     r   r   z#HuggingFaceEmbeddingServer.__init__>   s     %r   r   r   c                     t        t        | j                  j                  | j                  d|i      j                               S )a  
        Get the embeddings for a list of texts.

        Args:
            texts (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   r!   s     r   r"   z#HuggingFaceEmbeddingServer.__call__H   s<      **4==%?P*QVVX
 	
r   Nr#   r)   r   r   r+   r+   8   s&    
'C '
i 
J 
r   r+   )loggingtypingr   r   chromadb.api.typesr   r   r   	getLoggerr$   loggerr   r+   r)   r   r   <module>r4      sK       G G			8	$*
#4Y#? *
Z"
!29!= "
r   