
    _j                     ~    d dl mZmZmZmZ d dlmZ d dlmZm	Z	m
Z
 d dlZd dlmZ dZ G d dee                   ZdS )	    )
Embeddings	DocumentsEmbeddingFunctionSpacevalidate_config_schema)ListDictAnyN)urlparsezchroma/all-minilm-l6-v2-f32c            	       2   e Zd ZdZdedfdedededd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 )OllamaEmbeddingFunctionz
    This class is used to generate embeddings for a list of texts using the Ollama Embedding API
    (https://github.com/ollama/ollama/blob/main/docs/api.md#generate-embeddings).
    zhttp://localhost:11434<   url
model_nametimeoutreturnNc                 4   	 ddl m} n# t          $ r t          d          w xY w|| _        || _        || _        || _        | j                            d          r%t          |          }|j
         d|j         | _         || j        |          | _        dS )a  
        Initialize the Ollama Embedding Function.

        Args:
            url (str): The Base URL of the Ollama Server (default: "http://localhost:11434").
            model_name (str): The name of the model to use for text embeddings.
                Defaults to "chroma/all-minilm-l6-v2-f32", for available models see https://ollama.com/library.
            timeout (int): The timeout for the API call in seconds. Defaults to 60.
        r   )ClientzWThe ollama python package is not installed. Please install it with `pip install ollama`z/api/embeddingsz://)hostr   N)ollamar   ImportError
ValueErrorr   r   r   	_base_urlendswithr   schemenetloc_client)selfr   r   r   r   
parsed_urls         y/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/ollama_embedding_function.py__init__z OllamaEmbeddingFunction.__init__   s    	%%%%%%% 	 	 	i  	
 $ >""#455 	J!#J * 1IIj6GIIDNv4>7CCCs   	 #inputc                 h    | j                             | j        |          }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:
            >>> ollama_ef = OllamaEmbeddingFunction()
            >>> texts = ["Hello, world!", "How are you?"]
            >>> embeddings = ollama_ef(texts)
        )modelr#   c                 N    g | ]"}t          j        |t           j                   #S ))dtype)nparrayfloat32).0	embeddings     r!   
<listcomp>z4OllamaEmbeddingFunction.__call__.<locals>.<listcomp>E   s9     
 
 
 HYbj111
 
 
    
embeddings)r   embedr   )r   r#   responses      r!   __call__z OllamaEmbeddingFunction.__call__2   sF      <%%DO5%II
 
%l3
 
 
 	
r.   c                      dS )Nr    r4   r.   r!   namezOllamaEmbeddingFunction.nameJ   s    xr.   c                     dS )Ncosiner4   r   s    r!   default_spacez%OllamaEmbeddingFunction.default_spaceN   s    xr.   c                 
    g dS )N)r7   l2ipr4   r8   s    r!   supported_spacesz(OllamaEmbeddingFunction.supported_spacesQ   s    %%%%r.   configzEmbeddingFunction[Documents]c                     |                      d          }|                      d          }|                      d          }|||
J d            t          |||          S )Nr   r   r   FzThis code should not be reachedr   r   r   )getr   )r>   r   r   r   s       r!   build_from_configz)OllamaEmbeddingFunction.build_from_configT   sg    jjZZ--
**Y'';*,;;;;;&3:wWWWWr.   c                 ,    | j         | j        | j        dS )Nr@   r@   r8   s    r!   
get_configz"OllamaEmbeddingFunction.get_config_   s    xt4<XXXr.   
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   rE   rF   s      r!   validate_config_updatez.OllamaEmbeddingFunction.validate_config_updateb   s,     :%%e   &%r.   c                 &    t          | d           dS )z
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        r   Nr   )r>   s    r!   validate_configz'OllamaEmbeddingFunction.validate_configj   s     	vx00000r.   )__name__
__module____qualname____doc__DEFAULT_MODEL_NAMEstrintr"   r   r   r2   staticmethodr5   r   r9   r	   r=   r
   r   rB   rD   rH   rJ   r4   r.   r!   r   r   
   s         ,,	 D  D D  D 	 D
 
 D  D  D  DD
i 
J 
 
 
 
0 #    \u    &$u+ & & & & X$sCx. X5S X X X \XYDcN Y Y Y YsCx.6:38n	    
1S#X 
14 
1 
1 
1 \
1 
1 
1r.   r   )chromadb.api.typesr   r   r   r   *chromadb.utils.embedding_functions.schemasr   typingr	   r
   r   numpyr(   urllib.parser   rO   r   r4   r.   r!   <module>rX      s    N N N N N N N N N N N N M M M M M M " " " " " " " " " "     ! ! ! ! ! !2 k1 k1 k1 k1 k1/	: k1 k1 k1 k1 k1r.   