
    Bvhd                         d dl mZmZ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d dlZd dlZd dlZ G d dee         Zy)    )
Embeddings
EmbeddableEmbeddingFunctionSpaceis_imageis_document)ListDictAnyOptionalNvalidate_config_schemac                       e 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)CohereEmbeddingFunctionNapi_key
model_nameapi_key_env_varc                    	 dd l }	 t        j                  d      | _        |t        j                  dt               || _	        |xs t        j                  |      | _        | j                  st        d| d      || _        |j                  | j                        | _        y # t        $ r t        d      w xY w# t        $ r t        d      w xY w)Nr   zWThe cohere python package is not installed. Please install it with `pip install cohere`z	PIL.ImagezTThe PIL python package is not installed. Please install it with `pip install pillow`zDirect 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.)cohereImportError
ValueError	importlibimport_module	_PILImagewarningswarnDeprecationWarningr   osgetenvr   r   Clientclient)selfr   r   r   r   s        f/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/cohere_embedding_function.py__init__z CohereEmbeddingFunction.__init__   s    		&44[ADN MM_"
  /<"))O"<||tO#44UVWW$mmDLL13  	i 	  	f 	s   B% B= %B:=Cinputreturnc                 n   t        d |D              rz| j                  j                  |D cg c]  }t        |       c}| j                  d      j
                  D cg c]'  }t        j                  |t        j                        ) c}S t        d |D              r*g }|D ]  }t        |t        j                        st        dt        |             	 | j                  j                  |      }t        j                          }|j#                  |d       |j%                         }t'        j(                  |      j+                  d	      }	d
|	 }
|j-                  |
        | j                  j                  || j                  d      j
                  D cg c]'  }t        j                  |t        j                        ) c}S t1        d |D              }t1        d |D              }|r|rt        d      t        d      c c}w c c}w # t.        $ r}t        d|       |d}~ww xY wc c}w )z
        Generate embeddings for the given documents.

        Args:
            input: Documents or images to generate embeddings for.

        Returns:
            Embeddings for the documents.
        c              3   2   K   | ]  }t        |        y wNr   .0items     r#   	<genexpr>z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>C   s     3UT{4 U   search_document)textsmodel
input_type)dtypec              3   2   K   | ]  }t        |        y wr)   r   r+   s     r#   r.   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>M   s     2ED$Er/   z.Expected image input to be a numpy array, got PNG)formatzutf-8zdata:image/png;base64,z8Failed to convert image numpy array to base64 data URI: Nimage)imagesr2   r3   c              3   2   K   | ]  }t        |        y wr)   r*   r+   s     r#   r.   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>q   s     @%$K-%r/   c              3   2   K   | ]  }t        |        y wr)   r6   r+   s     r#   r.   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>r   s     >Xd^r/   zrInput contains a mix of text documents and images, which is not supported. Provide either all texts or all images.zPInput must be a list of text documents (str) or a list of images (numpy arrays).)allr!   embedstrr   
embeddingsnparrayfloat32
isinstancendarrayr   typer   	fromarrayioBytesIOsavegetvaluebase64	b64encodedecodeappend	Exceptionany)r"   r%   r-   r@   base64_imagesimage_np	pil_imagebuffer	img_bytesbase64_stringdata_urie	has_texts
has_imagess                 r#   __call__z CohereEmbeddingFunction.__call__7   s2    3U33 #'++"3"31673t97//0 #4 # *	##J 2::6#  2E22M!!(BJJ7$HhHXY  $ 8 8 BIZZ\FNN6%N8 & 1I %+$4$4Y$?$F$Fw$OM!7GH!((2# "4 #'++"3"3(//& #4 # *	##J 2::6#  @%@@I>>>JZ  I  !f c 8< ! $RSTRUV
s*   H,HBH,H2	H/H**H/c                       y)Nr    r^       r#   namezCohereEmbeddingFunction.name|   s    r_   c                 $    | j                   dk(  ryy)Nembed-multilingual-v2.0ipcosiner   r"   s    r#   default_spacez%CohereEmbeddingFunction.default_space   s    ??77r_   c                    | j                   dk(  rg dS | j                   dk(  rg dS | j                   dk(  rdgS | j                   dk(  rdgS | j                   dk(  rg dS | j                   d	k(  rg dS | j                   d
k(  rdgS g dS )Nzembed-english-v3.0)rd   l2rc   zembed-english-light-v3.0)rd   rc   ri   zembed-english-v2.0rd   zembed-english-light-v2.0zembed-multilingual-v3.0zembed-multilingual-light-v3.0rb   rc   re   rf   s    r#   supported_spacesz(CohereEmbeddingFunction.supported_spaces   s    ??22))__ ::))__ 44:__ :::__ 99))__ ??))__ 996M))r_   configzEmbeddingFunction[Embeddable]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)CohereEmbeddingFunction.build_from_config   sG     **%67ZZ-
"j&8;;;5&+

 	
r_   c                 4    | j                   | j                  dS )Nrm   rm   rf   s    r#   
get_configz"CohereEmbeddingFunction.get_config   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"   rr   rs   s      r#   validate_config_updatez.CohereEmbeddingFunction.validate_config_update   s!     :%e  &r_   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   )rk   s    r#   validate_configz'CohereEmbeddingFunction.validate_config   s     	vx0r_   )NlargeCHROMA_COHERE_API_KEY)__name__
__module____qualname__r   r?   r$   r   r   r\   staticmethodr`   r   rg   r	   rj   r
   r   ro   rq   ru   rw   r^   r_   r#   r   r      s    "&!6	!2#!2 !2 	!2FCj CZ CJ #  u 
*$u+ *$ 
$sCx. 
5T 
 
XDcN XsCx.6:38n	 
1S#X 
14 
1 
1r_   r   )chromadb.api.typesr   r   r   r   r   r   typingr	   r
   r   r   r   numpyrA   *chromadb.utils.embedding_functions.schemasr   rL   rH   r   r   r   r^   r_   r#   <module>r      s@     - , 	  M  	  e1/
; e1r_   