
    Bvh7                         d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
 d dlZd dlZd dlmZ d dlmZ d dlZ G d dee         Z G d dee         Z G d	 d
ee         Zy)    )
Embeddings	DocumentsEmbeddingFunctionSpace)ListDictAnycastOptionalNvalidate_config_schemac                       e Zd 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)GooglePalmEmbeddingFunctionzvTo use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a PaLM API key.Napi_key
model_nameapi_key_env_varc                 N   	 ddl m} |t	        j
                  dt               || _        |xs t        j                  |      | _
        | j                  st        d| d      || _        |j                  | j                         || _        y# t        $ r t        d      w xY w)a  
        Initialize the GooglePalmEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the Google PaLM API.
                Defaults to "CHROMA_GOOGLE_PALM_API_KEY".
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "models/embedding-gecko-001".
        r   NrThe Google Generative AI python package is not installed. Please install it with `pip install google-generativeai`Direct api_key configuration will not be persisted. Please use environment variables via api_key_env_var for persistent storage.The ! environment variable is not set.r   )google.generativeaigenerativeaiImportError
ValueErrorwarningswarnDeprecationWarningr   osgetenvr   r   	configure_palm)selfr   r   r   palms        f/RAG/venv/lib/python3.12/site-packages/chromadb/utils/embedding_functions/google_embedding_function.py__init__z$GooglePalmEmbeddingFunction.__init__   s    	. MM_"
  /<"))O"<||tO#44UVWW$t||,
'  	 E 	s   B B$inputreturnc           	          t        d |D              st        d      |D cg c]O  }t        j                  | j                  j                  | j                  |      d   t        j                        Q c}S c c}w )
        Generate embeddings for the given documents.

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

        Returns:
            Embeddings for the documents.
        c              3   <   K   | ]  }t        |t                y wN
isinstancestr.0items     r&   	<genexpr>z7GooglePalmEmbeddingFunction.__call__.<locals>.<genexpr>>        ;UT:dC(U   z4Google PaLM only supports text documents, not images)modeltext	embeddingdtype)allr   nparrayr#   generate_embeddingsr   float32)r$   r(   r8   s      r&   __call__z$GooglePalmEmbeddingFunction.__call__3   s~     ;U;;STT 
  HH

..T__4.P jj	 
 	
 
s   AA9c                       y)Ngoogle_palm rD       r&   namez GooglePalmEmbeddingFunction.nameK   s    rE   c                      yNcosinerD   r$   s    r&   default_spacez)GooglePalmEmbeddingFunction.default_spaceO       rE   c                 
    g dS N)rI   l2iprD   rJ   s    r&   supported_spacesz,GooglePalmEmbeddingFunction.supported_spacesR       %%rE   configEmbeddingFunction[Documents]c                 v    | j                  d      }| j                  d      }||J d       t        ||      S )Nr   r   This code should not be reachedr   r   )getr   )rS   r   r   s      r&   build_from_configz-GooglePalmEmbeddingFunction.build_from_configU   sG     **%67ZZ-
"j&8;;;5*+

 	
rE   c                 4    | j                   | j                  dS )NrW   rW   rJ   s    r&   
get_configz&GooglePalmEmbeddingFunction.get_configa   s    #'#7#7tWWrE   
old_config
new_configc                 "    d|v rt        d      y )Nr   SThe model name cannot be changed after the embedding function has been initialized.r   r$   r\   r]   s      r&   validate_config_updatez2GooglePalmEmbeddingFunction.validate_config_updated   s!     :%e  &rE   c                     t        | d       y)
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        rC   Nr   rS   s    r&   validate_configz+GooglePalmEmbeddingFunction.validate_configl   s     	v}5rE   )Nzmodels/embedding-gecko-001CHROMA_GOOGLE_PALM_API_KEY__name__
__module____qualname____doc__r   r0   r'   r   r   rA   staticmethodrF   r   rK   r   rQ   r   r	   rY   r[   rb   rf   rD   rE   r&   r   r   
   s    A "&6;	$#$ $ 	$L
i 
J 
0 #  u &$u+ & 	
$sCx. 	
5S 	
 	
XDcN XsCx.6:38n	 
6S#X 
64 
6 
6rE   r   c            	          e Zd ZdZ	 	 	 	 ddee   d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)#GoogleGenerativeAiEmbeddingFunctionzxTo use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a Google API key.Nr   r   	task_typer   c                 \   	 ddl m} |t	        j
                  dt               || _        |xs t        j                  |      | _
        | j                  st        d| d      || _        || _        |j                  | j                         || _        y# t        $ r t        d      w xY w)a  
        Initialize the GoogleGenerativeAiEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the Google Generative AI API.
                Defaults to "CHROMA_GOOGLE_GENAI_API_KEY".
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "models/embedding-001".
            task_type (str, optional): The task type for the embeddings.
                Use "RETRIEVAL_DOCUMENT" for embedding documents and "RETRIEVAL_QUERY" for embedding queries.
                Defaults to "RETRIEVAL_DOCUMENT".
        r   Nr   r   r   r   r   )r   r   r   r   r   r   r   r   r    r!   r   r   rp   r"   _genai)r$   r   r   rp   r   genais         r&   r'   z,GoogleGenerativeAiEmbeddingFunction.__init__}   s    &	/ MM_"
  /<"))O"<||tO#44UVWW$"-)  	 E 	s   B B+r(   r)   c                 @   t        d |D              st        d      g }|D ]k  }| j                  j                  | j                  || j
                        }|j                  t        j                  |d   t        j                               m t        t        |      S )r+   c              3   <   K   | ]  }t        |t                y wr-   r.   r1   s     r&   r4   z?GoogleGenerativeAiEmbeddingFunction.__call__.<locals>.<genexpr>   r5   r6   z=Google Generative AI only supports text documents, not images)r7   contentrp   r9   r:   )r<   r   rr   embed_contentr   rp   appendr=   r>   r@   r
   r   r$   r(   embeddings_listr8   embedding_results        r&   rA   z,GoogleGenerativeAiEmbeddingFunction.__call__   s     ;U;;O  :<D#{{88oo..  9  
 "")+6bjjI  J00rE   c                       y)Ngoogle_generative_airD   rD   rE   r&   rF   z(GoogleGenerativeAiEmbeddingFunction.name   s    %rE   c                      yrH   rD   rJ   s    r&   rK   z1GoogleGenerativeAiEmbeddingFunction.default_space   rL   rE   c                 
    g dS rN   rD   rJ   s    r&   rQ   z4GoogleGenerativeAiEmbeddingFunction.supported_spaces   rR   rE   rS   rT   c                     | j                  d      }| j                  d      }| j                  d      }|||J d       t        |||      S )Nr   r   rp   rV   r   r   rp   )rX   ro   )rS   r   r   rp   s       r&   rY   z5GoogleGenerativeAiEmbeddingFunction.build_from_config   s\     **%67ZZ-
JJ{+	"j&8I<M;;;52+
i
 	
rE   c                 J    | j                   | j                  | j                  dS )Nr   r   rJ   s    r&   r[   z.GoogleGenerativeAiEmbeddingFunction.get_config   s#    #33//
 	
rE   r\   r]   c                 @    d|v rt        d      d|v rt        d      y )Nr   r_   rp   zRThe task type cannot be changed after the embedding function has been initialized.r`   ra   s      r&   rb   z:GoogleGenerativeAiEmbeddingFunction.validate_config_update   s;     :%e  *$d  %rE   c                     t        | d       y)rd   r}   Nr   re   s    r&   rf   z3GoogleGenerativeAiEmbeddingFunction.validate_config   s     	v'=>rE   )Nzmodels/embedding-001RETRIEVAL_DOCUMENTCHROMA_GOOGLE_GENAI_API_KEYrh   rD   rE   r&   ro   ro   z   s&    C "&0-<)#) ) 	)
 )V1i 1J 1< &# & &u &$u+ & 

$sCx. 

5S 

 


DcN 

sCx.
6:38n
	
 
?S#X 
?4 
? 
?rE   ro   c                      e Zd ZdZ	 	 	 	 	 ddee   ded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)GoogleVertexEmbeddingFunctionzTo use this EmbeddingFunction, you must have the vertexai Python package installed and have Google Cloud credentials configured.Nr   r   
project_idregionr   c                 ~   	 ddl }ddlm} |t        j                  dt               || _        |xs t        j                  |      | _        | j                  st	        d| d      || _        || _        || _        |j                  ||       |j!                  |      | _        y# t        $ r t	        d      w xY w)	a  
        Initialize the GoogleVertexEmbeddingFunction.

        Args:
            api_key_env_var (str, optional): Environment variable name that contains your API key for the Google Vertex AI API.
                Defaults to "CHROMA_GOOGLE_VERTEX_API_KEY".
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "textembedding-gecko".
            project_id (str, optional): The Google Cloud project ID.
                Defaults to "cloud-large-language-models".
            region (str, optional): The Google Cloud region.
                Defaults to "us-central1".
        r   N)TextEmbeddingModelzjThe vertexai python package is not installed. Please install it with `pip install google-cloud-aiplatform`r   r   r   )projectlocation)vertexaivertexai.language_modelsr   r   r   r   r   r   r   r    r!   r   r   r   r   initfrom_pretrained_model)r$   r   r   r   r   r   r   r   s           r&   r'   z&GoogleVertexEmbeddingFunction.__init__  s    *	C MM_"
  /<"))O"<||tO#44UVWW$$j6:(88D+  	| 	s   
B' 'B<r(   r)   c                 (   t        d |D              st        d      g }|D ]_  }| j                  j                  |g      }|j	                  t        j                  |d   j                  t
        j                               a t        t        |      S )r+   c              3   <   K   | ]  }t        |t                y wr-   r.   r1   s     r&   r4   z9GoogleVertexEmbeddingFunction.__call__.<locals>.<genexpr>;  r5   r6   z6Google Vertex only supports text documents, not imagesr   r:   )r<   r   r   get_embeddingsrx   r=   r>   valuesr@   r
   r   ry   s        r&   rA   z&GoogleVertexEmbeddingFunction.__call__0  s     ;U;;UVV9;D#{{994&A"")!,332::F  J00rE   c                       y)Ngoogle_vertexrD   rD   rE   r&   rF   z"GoogleVertexEmbeddingFunction.nameH  s    rE   c                      yrH   rD   rJ   s    r&   rK   z+GoogleVertexEmbeddingFunction.default_spaceL  rL   rE   c                 
    g dS rN   rD   rJ   s    r&   rQ   z.GoogleVertexEmbeddingFunction.supported_spacesO  rR   rE   rS   rT   c                     | j                  d      }| j                  d      }| j                  d      }| j                  d      }||||J d       t        ||||      S )Nr   r   r   r   rV   r   r   r   r   )rX   r   )rS   r   r   r   r   s        r&   rY   z/GoogleVertexEmbeddingFunction.build_from_configR  sx     **%67ZZ-
ZZ-
H% #!!~;;;5,+!!	
 	
rE   c                 `    | j                   | j                  | j                  | j                  dS )Nr   r   rJ   s    r&   r[   z(GoogleVertexEmbeddingFunction.get_configh  s*    #33////kk	
 	
rE   r\   r]   c                 ^    d|v rt        d      d|v rt        d      d|v rt        d      y )Nr   r_   r   zSThe project ID cannot be changed after the embedding function has been initialized.r   zOThe region cannot be changed after the embedding function has been initialized.r`   ra   s      r&   rb   z4GoogleVertexEmbeddingFunction.validate_config_updatep  sU     :%e  :%e  z!a  "rE   c                     t        | d       y)rd   r   Nr   re   s    r&   rf   z-GoogleVertexEmbeddingFunction.validate_config  s     	v7rE   )Nztextembedding-geckozcloud-large-language-modelszus-central1CHROMA_GOOGLE_VERTEX_API_KEYrh   rD   rE   r&   r   r      s9    K "&/7#=-E#-E -E 	-E
 -E -E^1i 1J 10 #  u &$u+ & 
$sCx. 
5S 
 
*
DcN 
sCx.6:38n	  
8S#X 
84 
8 
8rE   r   )chromadb.api.typesr   r   r   r   typingr   r   r	   r
   r   r    numpyr=   numpy.typingnpt*chromadb.utils.embedding_functions.schemasr   r   r   ro   r   rD   rE   r&   <module>r      s^    N N 2 2 	   M m6"3I"> m6`A?*;I*F A?HM8$5i$@ M8rE   