
    _j#\                        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
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eZ G d d	ee                   Z G d
 dee                   Z G d dee                   ZdS )    )
Embeddings	DocumentsEmbeddingFunctionSpace)__version__)ListDictAnycastOptionalNvalidate_config_schemac                      e Zd ZdZ	 	 	 	 	 	 	 ddedee         dee         dee         d	ee         d
ee         de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 )GoogleGeminiEmbeddingFunctionzqTo use this EmbeddingFunction, you must have the google-genai Python package installed and have a Gemini API key.gemini-embedding-001NGEMINI_API_KEY
model_name	task_type	dimensionapi_key_env_varvertexaiprojectlocationc                    	 ddl m} n# t          $ r t          d          w xY w|| _        || _        || _        || _        || _        || _	        || _
        | j        rt          j        | j                  nd| _        | j        r| j        rt          d          | j        s| j        st          d| j         d          ddl m}	 |                    | j        ||||	                    dd	t"           i
                    | _        dS )u  
        Initialize the GoogleGeminiEmbeddingFunction.

        Args:
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "gemini-embedding-001".
            task_type (str, optional): The task type for the embeddings.
                Valid values include SEMANTIC_SIMILARITY, CLASSIFICATION, CLUSTERING,
                RETRIEVAL_DOCUMENT, RETRIEVAL_QUERY, CODE_RETRIEVAL_QUERY,
                QUESTION_ANSWERING, FACT_VERIFICATION.
            dimension (int, optional): The output dimensionality for the embeddings.
                Supported range: 128–3072. If None, the model's default is used.
            api_key_env_var (str, optional): Environment variable name that contains your API key.
                Defaults to "GEMINI_API_KEY".
            vertexai (bool, optional): Whether to use Vertex AI.
                If enabled, an API key must not be provided, and the environment variable `GOOGLE_APPLICATION_CREDENTIALS` must be set to the path of your service account JSON file.
            project (str, optional): The Google Cloud project ID (required for Vertex AI).
            location (str, optional): The Google Cloud location/region (required for Vertex AI).
        r   NzcThe google-genai python package is not installed. Please install it with `pip install google-genai`zGVertex AI and API key are mutually exclusive in the client initializer.The z= environment variable must be set if vertexai is not enabled.)typesx-goog-api-clientchroma/)headers)api_keyr   r   r   http_options)google.genaigenaiImportError
ValueErrorr   r   r   r   r   r   r   osgetenvr    r   ClientHttpOptionsr   client)
selfr   r   r   r   r   r   r   r#   r   s
             y/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/utils/embedding_functions/google_embedding_function.py__init__z&GoogleGeminiEmbeddingFunction.__init__   sk   :	((((((( 	 	 	u  	
 %"".  :>:NXry!5666TX< 	DM 	Y   | 	DM 	jt+jjj   	'&&&&&llL**,.E.E.EF +   # 
 
   	 #inputreturnc                    |st          d          t          |t          t          f          st          d          t	          d |D                       st          d          ddlm}  || j        | j                  }	 | j	        j
                            | j        ||          }n2# t          $ r%}t          d	t          |                     |d
}~ww xY wt          |d          r|j        st          d          g }|j        D ]Y}t          |d          st          d          |                    t%          j        |j        t$          j                             Zt-          t.          |          S )
        Generate embeddings for the given documents.

        Args:
            input: Documents to generate embeddings for.

        Returns:
            Embeddings for the documents.
        zInput documents cannot be emptyz*Input must be a list or tuple of documentsc              3   @   K   | ]}t          |t                    V  d S N
isinstancestr).0docs     r,   	<genexpr>z9GoogleGeminiEmbeddingFunction.__call__.<locals>.<genexpr>]   s,      99C:c3''999999    z#All input documents must be stringsr   )EmbedContentConfig)r   output_dimensionality)modelcontentsconfigzFailed to generate embeddings: N
embeddingsz#No embeddings returned from the APIvaluesz.Malformed embedding response: missing 'values'dtype)r%   r6   listtupleallgoogle.genai.typesr<   r   r   r*   modelsembed_contentr   	Exceptionr7   hasattrrA   appendnparrayrB   float32r   r   )r+   r/   r<   r@   responseeembeddings_listces           r,   __call__z&GoogleGeminiEmbeddingFunction.__call__O   s     	@>???%$// 	KIJJJ99599999 	DBCCC999999##n"&.
 
 

	P{)77o 8  HH
  	P 	P 	PGs1vvGGHHaO	P x.. 	Dh6I 	DBCCC% 	J 	JB2x(( S !QRRR""28BIRZ#H#H#HIIIIJ000s   'B+ +
C5 CCc                      dS )Ngoogle_gemini rX   r;   r,   namez"GoogleGeminiEmbeddingFunction.name|       r;   c                     dS NcosinerX   r+   s    r,   default_spacez+GoogleGeminiEmbeddingFunction.default_space       xr;   c                 
    g dS N)r]   l2iprX   r^   s    r,   supported_spacesz.GoogleGeminiEmbeddingFunction.supported_spaces       %%%%r;   r@   EmbeddingFunction[Documents]c           	      x   |                      d          }|                      d          }|                      d          }|                      dd          }|                      d          }|                      d          }|                      d          }|t          d	          t          |||||||
          S )Nr   r   r   r   r   r   r   r   zThe model name is required.)r   r   r   r   r   r   r   )getr%   r   )r@   r   r   r   r   r   r   r   s           r,   build_from_configz/GoogleGeminiEmbeddingFunction.build_from_config   s    ZZ--
JJ{++	JJ{++	 **%68HII::j))**Y''::j)):;;;,!+
 
 
 	
r;   c                     | j         | j        | j        | j        | j        d}| j        
| j        |d<   | j        
| j        |d<   |S )N)r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r+   r@   s     r,   
get_configz(GoogleGeminiEmbeddingFunction.get_config   sW    /#3|"
 "
 >%"&.F;>%"&.F;r;   
old_config
new_configc                     d|v rt          d          d|v rt          d          d|v rt          d          d|v rt          d          d	|v rt          d
          d S )Nr   SThe model name cannot be changed after the embedding function has been initialized.r   RThe dimension cannot be changed after the embedding function has been initialized.r   zQThe vertexai cannot be changed after the embedding function has been initialized.r   zPThe project cannot be changed after the embedding function has been initialized.r   zQThe location cannot be changed after the embedding function has been initialized.r%   r+   rn   ro   s      r,   validate_config_updatez4GoogleGeminiEmbeddingFunction.validate_config_update   s     :%%e   *$$d   ##c   
""b   ##c   $#r;   c                 &    t          | d           dS )
        Validate the configuration using the JSON schema.

        Args:
            config: Configuration to validate

        Raises:
            ValidationError: If the configuration does not match the schema
        rW   Nr   r@   s    r,   validate_configz-GoogleGeminiEmbeddingFunction.validate_config        	v77777r;   )r   NNr   NNN)__name__
__module____qualname____doc__r7   r   intboolr-   r   r   rU   staticmethodrY   r   r_   r   re   r	   r
   rj   rm   ru   ry   rX   r;   r,   r   r      s       {{ 1#'#')9#'!%"&?
 ?
?
 C=?
 C=	?

 "#?
 4.?
 #?
 3-?
 ?
 ?
 ?
B+1i +1J +1 +1 +1 +1Z #    \u    &$u+ & & & & 
$sCx. 
5S 
 
 
 \
,DcN    sCx.6:38n	   0 
8S#X 
84 
8 
8 
8 \
8 
8 
8r;   r   c                   R   e Zd ZdZ	 	 	 	 	 ddee         deded	ed
e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 )#GoogleGenerativeAiEmbeddingFunctionzxTo use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a Google API key.Nr   RETRIEVAL_DOCUMENTr   r    r   r   r   r   c                    	 ddl m} 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d	d
t           ii           || _        dS )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 "GEMINI_API_KEY".
            model_name (str, optional): The name of the model to use for text embeddings.
                Defaults to "gemini-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".
            dimension (int, optional): The output dimensionality for the embeddings.
                If None, the model's default dimensionality is used.
        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.GOOGLE_API_KEYr   ! environment variable is not set.r   r   r   r    client_options)google.generativeaigenerativeair$   r%   warningswarnDeprecationWarningr&   r'   r   r    r   r   r   	configurer   _genai)r+   r    r   r   r   r   r#   s          r,   r-   z,GoogleGenerativeAiEmbeddingFunction.__init__   s;   ,	/////// 	 	 	 E  	
 M_"  
 9%&&2#3D  #2D A")D,@"A"A| 	Nt+NNN   %""L%(;=T{=T=T'UV 	 	
 	
 	
 r.   r/   r0   c                 b   t          d |D                       st          d          g }|D ]n}| j        || j        d}| j        
| j        |d<    | j        j        di |}|                    t          j	        |d         t          j
                             ot          t          |          S )	r2   c              3   @   K   | ]}t          |t                    V  d S r4   r5   r8   items     r,   r:   z?GoogleGenerativeAiEmbeddingFunction.__call__.<locals>.<genexpr>  ,      ;;T:dC((;;;;;;r;   z=Google Generative AI only supports text documents, not images)r>   contentr   Nr=   	embeddingrC   rX   )rG   r%   r   r   r   r   rJ   rM   rN   rO   rP   r   r   )r+   r/   rS   textkwargsembedding_results         r,   rU   z,GoogleGenerativeAiEmbeddingFunction.__call__  s     ;;U;;;;; 	O   :< 	 	D!^& &F
 ~)26../8t{8BB6BB"")+6bjIII    J000r;   c                      dS )Ngoogle_generative_airX   rX   r;   r,   rY   z(GoogleGenerativeAiEmbeddingFunction.name/  s    %%r;   c                     dS r\   rX   r^   s    r,   r_   z1GoogleGenerativeAiEmbeddingFunction.default_space3  r`   r;   c                 
    g dS rb   rX   r^   s    r,   re   z4GoogleGenerativeAiEmbeddingFunction.supported_spaces6  rf   r;   r@   rg   c                     |                      d          }|                      d          }|                      d          }|                      d          }|||
J d            t          ||||          S )Nr   r   r   r   FThis code should not be reachedr   r   r   r   )ri   r   )r@   r   r   r   r   s        r,   rj   z5GoogleGenerativeAiEmbeddingFunction.build_from_config9  s     **%677ZZ--
JJ{++	JJ{++	"j&8I<M;;;;;2+!	
 
 
 	
r;   c                 R    | j         | j        | j        d}| j        
| j        |d<   |S )N)r   r   r   r   r   rl   s     r,   rm   z.GoogleGenerativeAiEmbeddingFunction.get_configJ  s9    #3/"
 "

 >%"&.F;r;   rn   ro   c                 x    d|v rt          d          d|v rt          d          d|v rt          d          d S )Nr   rq   r   zRThe task type cannot be changed after the embedding function has been initialized.r   rr   rs   rt   s      r,   ru   z:GoogleGenerativeAiEmbeddingFunction.validate_config_updateT  sp     :%%e   *$$d   *$$d   %$r;   c                 &    t          | d           dS )rw   r   Nr   rx   s    r,   ry   z3GoogleGenerativeAiEmbeddingFunction.validate_configd  s     	v'=>>>>>r;   )Nr   r   r   N)r{   r|   r}   r~   r   r7   r   r-   r   r   rU   r   rY   r   r_   r   re   r	   r
   rj   rm   ru   ry   rX   r;   r,   r   r      s        C  C "&0-/#'6 6#6 6 	6
 6 C=6 6 6 6p1i 1J 1 1 1 1> &# & & & \&u    &$u+ & & & & 
$sCx. 
5S 
 
 
 \
 DcN    sCx.6:38n	     
?S#X 
?4 
? 
? 
? \
? 
? 
?r;   r   c                   :   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dS )GooglePalmEmbeddingFunctionzvTo use this EmbeddingFunction, you must have the google.generativeai Python package installed and have a PaLM API key.Nmodels/embedding-gecko-001CHROMA_GOOGLE_PALM_API_KEYr    r   r   c                    	 ddl m} 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d	d
t           ii           || _        dS )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   Nr   r   r   r   r   r   r   r   r   )r   r   r$   r%   r   r   r   r&   r'   r   r    r   r   r   _palm)r+   r    r   r   palms        r,   r-   z$GooglePalmEmbeddingFunction.__init__u  s-   	....... 	 	 	 E  	
 M_"  
 9%&&2#3D  #2D A")D,@"A"A| 	Nt+NNN   %L%(;=T{=T=T'UV 	 	
 	
 	
 


r.   r/   r0   c                 p     t          d |D                       st          d           fd|D             S )
        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                    V  d S r4   r5   r   s     r,   r:   z7GooglePalmEmbeddingFunction.__call__.<locals>.<genexpr>  r   r;   z4Google PaLM only supports text documents, not imagesc                     g | ]G}t          j        j                            j        |           d         t           j                  HS ))r>   r   r   rC   )rN   rO   r   generate_embeddingsr   rP   )r8   r   r+   s     r,   
<listcomp>z8GooglePalmEmbeddingFunction.__call__.<locals>.<listcomp>  se     
 
 
  H
..T_4.PP j	  
 
 
r;   )rG   r%   )r+   r/   s   ` r,   rU   z$GooglePalmEmbeddingFunction.__call__  sa     ;;U;;;;; 	USTTT
 
 
 
 
 
 
 	
r;   c                      dS )Ngoogle_palmrX   rX   r;   r,   rY   z GooglePalmEmbeddingFunction.name  s    }r;   c                     dS r\   rX   r^   s    r,   r_   z)GooglePalmEmbeddingFunction.default_space  r`   r;   c                 
    g dS rb   rX   r^   s    r,   re   z,GooglePalmEmbeddingFunction.supported_spaces  rf   r;   r@   rg   c                     |                      d          }|                      d          }||
J d            t          ||          S )Nr   r   Fr   r   r   )ri   r   )r@   r   r   s      r,   rj   z-GooglePalmEmbeddingFunction.build_from_config  s[     **%677ZZ--
"j&8;;;;;*+

 
 
 	
r;   c                      | j         | j        dS )Nr   r   r^   s    r,   rm   z&GooglePalmEmbeddingFunction.get_config  s    #'#7tWWWr;   rn   ro   c                 ,    d|v rt          d          d S )Nr   rq   rs   rt   s      r,   ru   z2GooglePalmEmbeddingFunction.validate_config_update  s,     :%%e   &%r;   c                 &    t          | d           dS )rw   r   Nr   rx   s    r,   ry   z+GooglePalmEmbeddingFunction.validate_config  s     	v}55555r;   )Nr   r   r{   r|   r}   r~   r   r7   r-   r   r   rU   r   rY   r   r_   r   re   r	   r
   rj   rm   ru   ry   rX   r;   r,   r   r   r  s        A  A "&6;	- -#- - 	- - - -^
i 
J 
 
 
 
0 #    \u    &$u+ & & & & 	
$sCx. 	
5S 	
 	
 	
 \	
XDcN X X X XsCx.6:38n	    
6S#X 
64 
6 
6 
6 \
6 
6 
6r;   r   c                   F   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dS )GoogleVertexEmbeddingFunctionzTo use this EmbeddingFunction, you must have the vertexai Python package installed and have Google Cloud credentials configured.Ntextembedding-geckocloud-large-language-modelsus-central1CHROMA_GOOGLE_VERTEX_API_KEYr    r   
project_idregionr   c                    	 ddl }ddlm} 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          || _        || _        || _        |                    ||d	d
t            fg           |                    |          | _        dS )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   r   r   r   )r   r   request_metadata)r   vertexai.language_modelsr   r$   r%   r   r   r   r&   r'   r   r    r   r   r   initr   from_pretrained_model)r+   r    r   r   r   r   r   r   s           r,   r-   z&GoogleVertexEmbeddingFunction.__init__  sM   *	OOOCCCCCCC 	 	 	|  	
 M_"  
 9%&&2#3D  #2D A")D,@"A"A| 	Nt+NNN   %$24Kk4K4KLM 	 	
 	
 	

 )88DDs   
 'r/   r0   c                 <   t          d |D                       st          d          g }|D ][}| j                            |g          }|                    t          j        |d         j        t
          j                             \t          t          |          S )r   c              3   @   K   | ]}t          |t                    V  d S r4   r5   r   s     r,   r:   z9GoogleVertexEmbeddingFunction.__call__.<locals>.<genexpr>2  r   r;   z6Google Vertex only supports text documents, not imagesr   rC   )rG   r%   r   get_embeddingsrM   rN   rO   rB   rP   r   r   )r+   r/   rS   r   r   s        r,   rU   z&GoogleVertexEmbeddingFunction.__call__'  s     ;;U;;;;; 	WUVVV9; 	 	D#{994&AA"")!,32:FFF   
 J000r;   c                      dS )Ngoogle_vertexrX   rX   r;   r,   rY   z"GoogleVertexEmbeddingFunction.name?  rZ   r;   c                     dS r\   rX   r^   s    r,   r_   z+GoogleVertexEmbeddingFunction.default_spaceC  r`   r;   c                 
    g dS rb   rX   r^   s    r,   re   z.GoogleVertexEmbeddingFunction.supported_spacesF  rf   r;   r@   rg   c                     |                      d          }|                      d          }|                      d          }|                      d          }||||
J d            t          ||||          S )Nr   r   r   r   Fr   r   r   r   r   )ri   r   )r@   r   r   r   r   s        r,   rj   z/GoogleVertexEmbeddingFunction.build_from_configI  s     **%677ZZ--
ZZ--
H%% #!!~;;;;;,+!!	
 
 
 	
r;   c                 8    | j         | j        | j        | j        dS )Nr   r   r^   s    r,   rm   z(GoogleVertexEmbeddingFunction.get_config_  s&    #3//k	
 
 	
r;   rn   ro   c                 x    d|v rt          d          d|v rt          d          d|v rt          d          d S )Nr   rq   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.rs   rt   s      r,   ru   z4GoogleVertexEmbeddingFunction.validate_config_updateg  sp     :%%e   :%%e   z!!a   "!r;   c                 &    t          | d           dS )rw   r   Nr   rx   s    r,   ry   z-GoogleVertexEmbeddingFunction.validate_configw  rz   r;   )Nr   r   r   r   r   rX   r;   r,   r   r     s        K  K "&/7#=7E 7E#7E 7E 	7E
 7E 7E 7E 7E 7Er1i 1J 1 1 1 10 #    \u    &$u+ & & & & 
$sCx. 
5S 
 
 
 \
*
DcN 
 
 
 
sCx.6:38n	     
8S#X 
84 
8 
8 
8 \
8 
8 
8r;   r   )chromadb.api.typesr   r   r   r   chromadbr   typingr   r	   r
   r   r   r&   numpyrN   numpy.typingnpt*chromadb.utils.embedding_functions.schemasr   r   r   GoogleGenaiEmbeddingFunctionr   r   r   rX   r;   r,   <module>r      s   N N N N N N N N N N N N             2 2 2 2 2 2 2 2 2 2 2 2 2 2 				           M M M M M M C8 C8 C8 C8 C8$5i$@ C8 C8 C8N  = Z? Z? Z? Z? Z?*;I*F Z? Z? Z?zv6 v6 v6 v6 v6"3I"> v6 v6 v6rW8 W8 W8 W8 W8$5i$@ W8 W8 W8 W8 W8r;   