Ë
    kQêjî  ã                   óˆ   — d dl 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mZ  ed	«       G d
„ de
«      «       Zy)é    N)ÚClassVarÚListÚUnioné   )Úattempt_import_or_raiseé   )ÚTextEmbeddingFunction)Úregister)Úapi_key_not_found_helpÚTEXTÚcoherec                   óø   — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed	<   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d„ Zy)ÚCohereEmbeddingFunctiona	  
    An embedding function that uses the Cohere API

    https://docs.cohere.com/docs/multilingual-language-models

    Parameters
    ----------
    name : str, default "embed-multilingual-v2.0"
        The name of the model to use. List of acceptable models:

            * embed-english-v3.0
            * embed-multilingual-v3.0
            * embed-english-light-v3.0
            * embed-multilingual-light-v3.0
            * embed-english-v2.0
            * embed-english-light-v2.0
            * embed-multilingual-v2.0

    source_input_type : str, default "search_document"
        The input type for the source column in the database

    query_input_type : str, default "search_query"
        The input type for the query column in the database

    Notes
    -----
    Cohere supports following input types:

    | Input Type               | Description                          |
    |-------------------------|---------------------------------------|
    | "`search_document`"     | Used for embeddings stored in a vector|
    |                         | database for search use-cases.        |
    | "`search_query`"        | Used for embeddings of search queries |
    |                         | run against a vector DB               |
    | "`semantic_similarity`" | Specifies the given text will be used |
    |                         | for Semantic Textual Similarity (STS) |
    | "`classification`"      | Used for embeddings passed through a  |
    |                         | text classifier.                      |
    | "`clustering`"          | Used for the embeddings run through a |
    |                         | clustering algorithm                  |

    Examples
    --------
    import lancedb
    from lancedb.pydantic import LanceModel, Vector
    from lancedb.embeddings import EmbeddingFunctionRegistry

    cohere = EmbeddingFunctionRegistry
        .get_instance()
        .get("cohere")
        .create(name="embed-multilingual-v2.0")

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

    data = [ { "text": "hello world" },
            { "text": "goodbye world" }]

    db = lancedb.connect("~/.lancedb")
    tbl = db.create_table("test", schema=TextModel, mode="overwrite")

    tbl.add(data)

    úembed-multilingual-v2.0ÚnameÚsearch_documentÚsource_input_typeÚsearch_queryÚquery_input_typeNÚclientc                 ó°   — | j                   dv ry| j                   dv ry| j                   dk(  ry| j                   dk(  ryt        d	| j                   › d
�«      ‚)N)zembed-english-v3.0zembed-multilingual-v3.0zembed-english-light-v2.0i   )zembed-english-light-v3.0zembed-multilingual-light-v3.0i€  zembed-english-v2.0i   r   i   zModel z not supported)r   Ú
ValueError)Úselfs    ú^/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/embeddings/cohere.pyÚndimszCohereEmbeddingFunction.ndimsY   s`   € à�9‰9ð 
ñ 
ð
 Ø�Y‰YÐWÑWØØ�Y‰YÐ.Ò.ØØ�Y‰YÐ3Ò3Øä˜v d§i¡i [°Ð?Ó@Ð@ó    ÚqueryÚreturnc                 ó<   — | j                  || j                  ¬«      S )N©Ú
input_type)Úcompute_source_embeddingsr   )r   r   ÚargsÚkwargss       r   Úcompute_query_embeddingsz0CohereEmbeddingFunction.compute_query_embeddingsj   s   € Ø×-Ñ-¨eÀ×@UÑ@UÐ-ÓVÐVr   Útextsc                 óˆ   — | j                  |«      }|j                  d«      xs | j                  }| j                  ||¬«      S )Nr!   r    )Úsanitize_inputÚgetr   Úgenerate_embeddings)r   r&   r#   r$   r!   s        r   r"   z1CohereEmbeddingFunction.compute_source_embeddingsm   sF   € Ø×#Ñ# EÓ*ˆà�J‰J�|Ó$Ò>¨×(>Ñ(>ð 	ð ×'Ñ'¨¸*Ð'ÓEÐEr   c                 ó¸   — | j                  «        t        j                  j                  d|| j                  dœ|¤Ž}|j
                  D �cg c]  }|‘Œ 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&   Úmodel© )Ú_init_clientr   r   Úembedr   Ú
embeddings)r   r&   r#   r$   ÚrsÚembs         r   r*   z+CohereEmbeddingFunction.generate_embeddingst   sV   € ð 	×ÑÔÜ$×+Ñ+×1Ñ1ð 
Ø˜tŸy™yñ
Ø,2ñ
ˆð  "Ÿ}™}Ö-˜’Ò-Ð-ùÒ-s   Á	Ac                 óæ   — t        d«      }t        j                  €Vt        j                  j                  d«      €t        d«       |j                  t        j                  d   «      t        _        y y )Nr   ÚCOHERE_API_KEY)r   r   r   ÚosÚenvironr)   r   ÚClient)r   r   s     r   r.   z$CohereEmbeddingFunction._init_client†   sU   € Ü(¨Ó2ˆÜ"×)Ñ)Ð1Ü�z‰z�~‰~Ð.Ó/Ð7Ü& xÔ0Ø-3¯]©]¼2¿:¹:ÐFVÑ;WÓ-XÔ#Õ*ð 2r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__r   r   r   r   r   r   ÚnpÚarrayr%   r   r"   r   Úndarrayr*   r.   r-   r   r   r   r      s¬   … ñ@ðD *€Dˆ#Ó)Ø.Ð�sÓ.Ø*Ð�cÓ*Ø€FˆHÓòAð"W¨cð WÀtÈBÏHÉHÁ~ó WðF¨tð FÈÈbÏhÉhÉó Fð.Ø˜4 ™9 b§j¡jÐ0Ñ1ð.à	ˆb�h‰h‰ó.ó$Yr   r   )r5   Útypingr   r   r   Únumpyr>   Úutilr   Úbaser	   Úregistryr
   Úutilsr   r   r   r-   r   r   ú<module>rG      sF   ðó
 
ß (Ñ (ã å *Ý 'Ý ß /ñ 
ˆ(ÓôzYÐ3ó zYó ñzYr   