
    _jq                         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dS )    )
Embeddings
EmbeddableEmbeddingFunctionSpaceis_imageis_document)ListDictAnyOptionalNvalidate_config_schemac                   6   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dS )CohereEmbeddingFunctionNlargeCHROMA_COHERE_API_KEYapi_key
model_nameapi_key_env_varc                    	 dd l }n# t          $ r t          d          w xY w	 t          j        d          | _        n# t          $ r t          d          w xY w|t          j        dt                     t          j
        d          d| _        n|| _        |pt          j
        | j                  | _        | j        st          d| j         d          || _        |                    | j                  | _        d S )	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.COHERE_API_KEYzThe z! environment variable is not set.)cohereImportError
ValueError	importlibimport_module	_PILImagewarningswarnDeprecationWarningosgetenvr   r   r   Clientclient)selfr   r   r   r   s        y/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/cohere_embedding_function.py__init__z CohereEmbeddingFunction.__init__   s>   	MMMM 	 	 	i  	
	&4[AADNN 	 	 	f  	
 M_"  
 9%&&2#3D  #2D A")D,@"A"A| 	Nt+NNN   %mmDL11s    !? Ainputreturnc                    t          d |D                       r;d | j                            d |D             | j        d          j        D             S t          d |D                       r0g }|D ]}t          |t          j                  st          dt          |                     	 | j
                            |          }t          j                    }|                    |d	           |                                }t!          j        |                              d
          }d| }|                    |           # t(          $ r}	t          d|	           |	d}	~	ww xY wd | j                            || j        d          j        D             S t+          d |D                       }
t+          d |D                       }|
r|rt          d          t          d          )z
        Generate embeddings for the given documents.

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

        Returns:
            Embeddings for the documents.
        c              3   4   K   | ]}t          |          V  d S Nr   .0items     r&   	<genexpr>z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>I   s*      33T{4  333333    c                 N    g | ]"}t          j        |t           j                   #S )dtypenparrayfloat32r/   
embeddingss     r&   
<listcomp>z4CohereEmbeddingFunction.__call__.<locals>.<listcomp>J   9        2:666  r2   c                 ,    g | ]}t          |          S  )strr.   s     r&   r<   z4CohereEmbeddingFunction.__call__.<locals>.<listcomp>M   s    7773t99777r2   search_document)textsmodel
input_typec              3   4   K   | ]}t          |          V  d S r,   r   r.   s     r&   r1   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>S   s(      22D$222222r2   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: Nc                 N    g | ]"}t          j        |t           j                   #S r4   r6   r:   s     r&   r<   z4CohereEmbeddingFunction.__call__.<locals>.<listcomp>m   r=   r2   image)imagesrC   rD   c              3   4   K   | ]}t          |          V  d S r,   r-   r.   s     r&   r1   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>w   s*      @@$K--@@@@@@r2   c              3   4   K   | ]}t          |          V  d S r,   rF   r.   s     r&   r1   z3CohereEmbeddingFunction.__call__.<locals>.<genexpr>x   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$   embedr   r;   
isinstancer7   ndarrayr   typer   	fromarrayioBytesIOsavegetvaluebase64	b64encodedecodeappend	Exceptionany)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__=   ss    33U33333 7	 "&+"3"377777/0 #4 # #     22E22222 -	M!  !(BJ77 $YhYY   $ 8 8 B BIZ\\FNN6%N888 & 1 1I %+$4Y$?$?$F$Fw$O$OMGGGH!((2222    $VSTVV 
 "&+"3"3(/& #4 # #     @@%@@@@@I>>>>>>>J Z   I   !f  s   /BE		
E+E&&E+c                      dS )Nr   r?   r?   r2   r&   namezCohereEmbeddingFunction.name   s    xr2   c                      | j         dk    rdS dS )Nembed-multilingual-v2.0ipcosiner   r%   s    r&   default_spacez%CohereEmbeddingFunction.default_space   s    ?7774xr2   c                 ^    | j         dk    rdgS | j         dk    rdgS | j         dk    rdgS g dS )Nzembed-english-v2.0rn   zembed-english-light-v2.0rl   rm   )rn   l2rm   ro   rp   s    r&   supported_spacesz(CohereEmbeddingFunction.supported_spaces   sO    ?222:_ ::::_ 9996M))))r2   configzEmbeddingFunction[Embeddable]c                     |                      d          }|                      d          }||
J d            t          ||          S )Nr   r   FzThis code should not be reachedr   r   )getr   )ru   r   r   s      r&   build_from_configz)CohereEmbeddingFunction.build_from_config   s[     **%677ZZ--
"j&8;;;;;&+

 
 
 	
r2   c                      | j         | j        dS )Nrw   rw   rp   s    r&   
get_configz"CohereEmbeddingFunction.get_config   s    #'#7tWWWr2   
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%   r|   r}   s      r&   validate_config_updatez.CohereEmbeddingFunction.validate_config_update   s,     :%%e   &%r2   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   )ru   s    r&   validate_configz'CohereEmbeddingFunction.validate_config   s     	vx00000r2   )Nr   r   )__name__
__module____qualname__r   r@   r'   r   r   rh   staticmethodrj   r   rq   r	   rt   r
   r   ry   r{   r   r   r?   r2   r&   r   r      s        "&!6	'2 '2#'2 '2 	'2 '2 '2 '2RCj CZ C C C CJ #    \u    
*$u+ * * * * 
$sCx. 
5T 
 
 
 \
XDcN X X X XsCx.6:38n	    
1S#X 
14 
1 
1 
1 \
1 
1 
1r2   r   )chromadb.api.typesr   r   r   r   r   r   typingr	   r
   r   r   r!   numpyr7   *chromadb.utils.embedding_functions.schemasr   rX   rT   r   r   r   r?   r2   r&   <module>r      s                  - , , , , , , , , , , , 				     M M M M M M  				     c1 c1 c1 c1 c1/
; c1 c1 c1 c1 c1r2   