Ë
    kQêj¨  ã                   ó¤   — d dl Z d dlmZ d dlmZmZmZ d dlZd dl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZmZ dZ ed«       G d„ de«      «       Zy)é    N)Úcached_property)ÚListÚOptionalÚUnion)Ú
ConfigDicté   )Úattempt_import_or_raiseé   )ÚTextEmbeddingFunction)Úregister)ÚTEXTÚapi_key_not_found_helpéd   zgemini-textc                   ó  — e Zd ZU dZdZeed<   dZee	   ed<   dZ
eed<   dZeed	<    eef¬
«      Zd„ Zdedeej$                     fd„Zdedeej$                     fd„Zdeee   ej.                  f   deej$                     fd„Zed„ «       Zy)Ú
GeminiTextaÃ
  
    An embedding function that uses Google's Gemini API. Requires GOOGLE_API_KEY to
    be set.

    https://ai.google.dev/gemini-api/docs/embeddings

    Supports various tasks types:
    | Task Type               | Description                                            |
    |-------------------------|--------------------------------------------------------|
    | "`retrieval_query`"     | Specifies the given text is a query in a               |
    |                         | search/retrieval setting.                              |
    | "`retrieval_document`"  | Specifies the given text is a document in a            |
    |                         | search/retrieval setting. Using this task type         |
    |                         | requires a title but is automatically provided by      |
    |                         | Embeddings API                                         |
    | "`semantic_similarity`" | Specifies the given text will be used for Semantic     |
    |                         | Textual Similarity (STS).                              |
    | "`classification`"      | Specifies that the embeddings will be used for         |
    |                         | classification.                                        |
    | "`clustering`"          | Specifies that the embeddings will be used for         |
    |                         | clustering.                                            |

    Note: The supported task types might change in the Gemini API, but as long as a
          supported task type and its argument set is provided, those will be delegated
          to the API calls.

    Parameters
    ----------
    name : str, default "gemini-embedding-001"
        The name of the model to use. Supported models include:
        - "gemini-embedding-001" (768 dimensions)

        Note: The legacy "models/embedding-001" format is also supported but
        "gemini-embedding-001" is recommended.

    query_task_type : str, default "retrieval_query"
        Sets the task type for the queries.
    source_task_type : str, default "retrieval_document"
        Sets the task type for ingestion.

    Examples
    --------
    import lancedb
    import pandas as pd
    from lancedb.pydantic import LanceModel, Vector
    from lancedb.embeddings import get_registry

    model = get_registry().get("gemini-text").create()

    class TextModel(LanceModel):
        text: str = model.SourceField()
        vector: Vector(model.ndims()) = model.VectorField()

    df = pd.DataFrame({"text": ["hello world", "goodbye world"]})
    db = lancedb.connect("~/.lancedb")
    tbl = db.create_table("test", schema=TextModel, mode="overwrite")

    tbl.add(df)
    rs = tbl.search("hello").limit(1).to_pandas()

    zgemini-embedding-001ÚnameNÚdimÚretrieval_queryÚquery_task_typeÚretrieval_documentÚsource_task_type)Úignored_typesc                 ó4   — | j                   r| j                   S y)Ni   )r   )Úselfs    úc/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/embeddings/gemini_text.pyÚndimszGeminiText.ndims[   s   € Ø�8Š8Ø—8‘8ˆOàó    ÚqueryÚreturnc                 ó<   — | j                  || j                  ¬«      S )N©Ú	task_type)Úcompute_source_embeddingsr   )r   r   ÚargsÚkwargss       r   Úcompute_query_embeddingsz#GeminiText.compute_query_embeddingsa   s   € Ø×-Ñ-¨e¸t×?SÑ?SÐ-ÓTÐTr   Útextsc                 óˆ   — | j                  |«      }|j                  d«      xs | j                  }| j                  ||¬«      S )Nr"   r!   )Úsanitize_inputÚgetr   Úgenerate_embeddings)r   r'   r$   r%   r"   s        r   r#   z$GeminiText.compute_source_embeddingsd   sF   € Ø×#Ñ# EÓ*ˆà�J‰J�{Ó#Ò< t×'<Ñ'<ð 	ð ×'Ñ'¨¸Ð'ÓCÐCr   c           	      ó~  — ddl m} |j                  d«      }g }|D ]7  }|dk(  r|j                  dddid|igi«       Œ"|j                  dd|igi«       Œ9 d| j	                  «       i}|r|j                  «       |d<   |r |j                  di |¤Žnd	}	g }
t        dt        |«      t        «      D ]†  }|||t        z    }| j                  j                  j                  | j                  ||	¬
«      }|
j                  |j                  D �cg c]!  }t!        j"                  |j$                  «      ‘Œ# c}«       Œˆ |
S c c}w )z®
        Get the embeddings for the given texts

        Parameters
        ----------
        texts: list[str] or np.ndarray (of str)
            The texts to embed
        r   )Útypesr"   r   ÚpartsÚtextzEmbedding of a documentÚoutput_dimensionalityN)ÚmodelÚcontentsÚconfig© )Úgoogle.genair-   r*   Úappendr   ÚupperÚEmbedContentConfigÚrangeÚlenÚEMBEDDING_BATCH_SIZEÚclientÚmodelsÚembed_contentr   ÚextendÚ
embeddingsÚnpÚarrayÚvalues)r   r'   r$   r%   r-   r"   r2   r/   Úconfig_kwargsr3   r@   ÚiÚchunkÚresponseÚes                  r   r+   zGeminiText.generate_embeddingsk   sR  € õ 	'à—J‘J˜{Ó+ˆ	ð ˆØò 	=ˆDØÐ0Ò0à—‘Ø Ð(AÐBÀVÈTÀNÐSÐTõð —‘ ¨F°D¨>Ð*:Ð ;Õ<ð	=ð 1°$·*±*³,Ð?ˆÙØ)2¯©Ó):ˆM˜+Ñ&á>KÐ)�×)Ñ)Ñ:¨MÒ:ÐQUˆð ˆ
Ü�qœ#˜h›-Ô)=Ó>ò 	QˆAØ˜Q Ô%9Ñ!9Ð:ˆEØ—{‘{×)Ñ)×7Ñ7Ø—i‘iØØð 8ó ˆHð
 ×Ñ¸8×;NÑ;NÖO°aœrŸx™x¨¯©Õ1ÒOÕPð	Qð Ðùò Ps   Ä	&D:
c                 óô   — t        dd«       t        j                  j                  d«      st	        d«       ddlm} ddlm} |j                  t        j                  j                  d«      dd	d
|› �ii¬«      S )Nzgoogle.genaizgoogle-genaiÚGOOGLE_API_KEYÚgoogler   )Úgenai)Ú__version__Úheaderszx-goog-api-clientzlancedb/)Úapi_keyÚhttp_options)
r	   ÚosÚenvironr*   r   rK   rL   ÚlancedbrM   ÚClient)r   Úgenai_modulerM   s      r   r<   zGeminiText.client™   sm   € ä °Ô?ä�z‰z�~‰~Ð.Ô/Ü" 8Ô,å0Ý'à×"Ñ"Ü—J‘J—N‘NÐ#3Ó4àØ'¨8°K°=Ð)Aððð #ó 
ð 	
r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__r   r   Úintr   r   r   r   Úmodel_configr   r   rA   rB   r&   r   r#   r   Úndarrayr+   r<   r4   r   r   r   r      sÉ   … ñ<ð| '€Dˆ#Ó&Ø€Cˆ�#‰ÓØ,€O�SÓ,Ø0Ð�cÓ0á¨_Ð,>Ô?€LòðU¨cð UÀtÈBÏHÉHÁ~ó UðD¨tð DÈÈbÏhÉhÉó Dð,Ø˜4 ™9 b§j¡jÐ0Ñ1ð,à	ˆb�h‰h‰ó,ð\ ñ
ó ñ
r   r   )rQ   Ú	functoolsr   Útypingr   r   r   ÚnumpyrA   Úpydanticr   Úutilr	   Úbaser   Úregistryr   Úutilsr   r   r;   r   r4   r   r   ú<module>rg      sO   ðó
 
Ý %ß (Ñ (ã Ý å *Ý 'Ý ß /àÐ ñ 
ˆ-ÓôU
Ð&ó U
ó ñU
r   