
    3i                         S SK JrJr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KrS SKrS SKr " S S\\   5      rg)    )
Embeddings
EmbeddableEmbeddingFunctionSpaceis_imageis_document)ListDictAnyOptionalNvalidate_config_schemac                      \ 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)CohereEmbeddingFunction   Napi_key
model_nameapi_key_env_varc                     SS K n [        R                  " S5      U l        Ub  [        R                  " S[        5        X0l	        U=(       d    [        R                  " U5      U l        U R                  (       d  [        SU S35      eX l        UR                  U R                  5      U l        g ! [         a    [        S5      ef = f! [         a    [        S5      ef = f)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        v/home/james-whalen/.local/lib/python3.13/site-packages/chromadb/utils/embedding_functions/cohere_embedding_function.py__init__ CohereEmbeddingFunction.__init__   s    		&44[ADN MM_"
  /<"))O"<||tO#44UVWW$mmDLL13  	i 	  	f 	s   B1 C
 1C
C inputreturnc                    [        S U 5       5      (       a{  U R                  R                  U Vs/ s H  n[        U5      PM     snU R                  SS9R
                   Vs/ s H&  n[        R                  " U[        R                  S9PM(     sn$ [        S U 5       5      (       Ga.  / nU H  n[        U[        R                  5      (       d  [        S[        U5       35      e U R                  R                  U5      n[        R                   " 5       nUR#                  USS9  UR%                  5       n[&        R(                  " U5      R+                  S	5      n	S
U	 3n
UR-                  U
5        M     U R                  R                  UU R                  SS9R
                   Vs/ s H&  n[        R                  " U[        R                  S9PM(     sn$ [1        S U 5       5      n[1        S U 5       5      nU(       a  U(       a  [        S5      e[        S5      es  snf s  snf ! [.         a  n[        SU 35      UeSnAff = fs  snf )z
Generate embeddings for the given documents.

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

Returns:
    Embeddings for the documents.
c              3   8   #    U  H  n[        U5      v   M     g 7fNr   .0items     r$   	<genexpr>3CohereEmbeddingFunction.__call__.<locals>.<genexpr>C   s     3UT{4  U   search_document)textsmodel
input_type)dtypec              3   8   #    U  H  n[        U5      v   M     g 7fr+   r   r-   s     r$   r0   r1   M   s     2ED$Er2   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)imagesr5   r6   c              3   8   #    U  H  n[        U5      v   M     g 7fr+   r,   r-   s     r$   r0   r1   q   s     @%$K--%r2   c              3   8   #    U  H  n[        U5      v   M     g 7fr+   r9   r-   s     r$   r0   r1   r   s     >Xd^^r2   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/   rC   base64_imagesimage_np	pil_imagebuffer	img_bytesbase64_stringdata_urie	has_texts
has_imagess                 r$   __call__ CohereEmbeddingFunction.__call__7   s;    3U333 #'++"3"31673t97//0 #4 # *	##J 2::6#  2E222M!!(BJJ7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+)-I+
I	5II	c                      g)Nr    rb       r$   nameCohereEmbeddingFunction.name|   s    rc   c                 &    U R                   S:X  a  gg)Nembed-multilingual-v2.0ipcosiner   r#   s    r$   default_space%CohereEmbeddingFunction.default_space   s    ??77rc   c                 |    U R                   S:X  a  S/$ U R                   S:X  a  S/$ U R                   S:X  a  S/$ / SQ$ )Nzembed-english-v2.0ri   zembed-english-light-v2.0rg   rh   )ri   l2rh   rj   rk   s    r$   supported_spaces(CohereEmbeddingFunction.supported_spaces   sF    ??22:__ :::__ 996M))rc   configzEmbeddingFunction[Embeddable]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   )rr   r   r   s      r$   build_from_config)CohereEmbeddingFunction.build_from_config   sE     **%67ZZ-
"j&8;;;5&+
 	
rc   c                 4    U R                   U R                  S.$ )Nrt   rt   rk   s    r$   
get_config"CohereEmbeddingFunction.get_config   s    #'#7#7tWWrc   
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_update.CohereEmbeddingFunction.validate_config_update   s!     :%e  &rc   c                     [        U S5        g)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   )rr   s    r$   validate_config'CohereEmbeddingFunction.validate_config   s     	vx0rc   )r   r   r   r"   r   )NlargeCHROMA_COHERE_API_KEY)__name__
__module____qualname____firstlineno__r   rB   r%   r   r   r_   staticmethodrd   r   rl   r	   rp   r
   r   rv   ry   r~   r   __static_attributes__rb   rc   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 
1rc   r   )chromadb.api.typesr   r   r   r   r   r   typingr	   r
   r   r   r   numpyrD   *chromadb.utils.embedding_functions.schemasr   rO   rK   r   r   r   rb   rc   r$   <module>r      s@     - , 	  M  	  ]1/
; ]1rc   