
    Bvh                     p    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y)	    )
Embeddings	DocumentsEmbeddingFunctionSpacevalidate_config_schema)ListDictAnyN)urlparsezchroma/all-minilm-l6-v2-f32c            	           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y)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                 8   	 ddl m} || _        || _        || _        || _        | j                  j                  d      r+t        |      }|j                   d|j                   | _         || j                  |      | _        y# t        $ r t        d      w xY w)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         f/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/ollama_embedding_function.py__init__z OllamaEmbeddingFunction.__init__   s    	% $ >>""#45!#J * 1 12#j6G6G5HIDN4>>7C  	i 	s   B Binputc                     | j                   j                  | j                  |      }|d   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:
            >>> ollama_ef = OllamaEmbeddingFunction()
            >>> texts = ["Hello, world!", "How are you?"]
            >>> embeddings = ollama_ef(texts)
        )modelr#   
embeddings)dtype)r   embedr   nparrayfloat32)r   r#   response	embeddings       r!   __call__z OllamaEmbeddingFunction.__call__2   s\      <<%%DOO5%I
 &l3
3	 HHYbjj13
 	
 
s   ,Ac                       y)Nr    r0       r!   namezOllamaEmbeddingFunction.nameJ   s    r1   c                      y)Ncosiner0   r   s    r!   default_spacez%OllamaEmbeddingFunction.default_spaceN   s    r1   c                 
    g dS )N)r4   l2ipr0   r5   s    r!   supported_spacesz(OllamaEmbeddingFunction.supported_spacesQ   s    %%r1   configzEmbeddingFunction[Documents]c                     | j                  d      }| j                  d      }| j                  d      }|||J d       t        |||      S )Nr   r   r   zThis code should not be reachedr   r   r   )getr   )r;   r   r   r   s       r!   build_from_configz)OllamaEmbeddingFunction.build_from_configT   sT    jjZZ-
**Y';*,;;;5&3:wWWr1   c                 J    | j                   | j                  | j                  dS )Nr=   r=   r5   s    r!   
get_configz"OllamaEmbeddingFunction.get_config_   s    xxt4<<XXr1   
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   rB   rC   s      r!   validate_config_updatez.OllamaEmbeddingFunction.validate_config_updateb   s!     :%e  &r1   c                     t        | d       y)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0r1   )__name__
__module____qualname____doc__DEFAULT_MODEL_NAMEstrintr"   r   r   r.   staticmethodr2   r   r6   r	   r:   r
   r   r?   rA   rE   rG   r0   r1   r!   r   r   
   s)    ,,	 D D  D 	 D
 
 DD
i 
J 
0 #  u &$u+ & X$sCx. X5S X XYDcN YsCx.6:38n	 
1S#X 
14 
1 
1r1   r   )chromadb.api.typesr   r   r   r   *chromadb.utils.embedding_functions.schemasr   typingr	   r
   r   numpyr)   urllib.parser   rL   r   r0   r1   r!   <module>rU      s4    N N M " "  !2 k1/	: k1r1   