Ë
    kQêj/  ã                   óä   — d dl mZmZ d dlZd dlmZmZmZmZ d dl	m
Z
 d dlZd dlZd dlmZmZmZ ddlmZmZ  G d„ d	e«      Z G d
„ dee«      Z G d„ de«      Z G d„ de«      Z G d„ de«      Zy)é    )ÚABCÚabstractmethodN)ÚListÚOptionalÚTupleÚUnion)Úadd_note)Ú	BaseModelÚFieldÚPrivateAttré   )ÚTEXTÚretry_with_exponential_backoffc                   ó   — e Zd ZdZy)ÚUntrustedEmbeddingConfigErrora<  
    Raised when using an embedding function restored from table metadata whose
    settings this process has not approved.

    See
    [allow_remote_code][lancedb.embeddings.EmbeddingFunctionRegistry.allow_remote_code]
    and
    [allow_endpoint][lancedb.embeddings.EmbeddingFunctionRegistry.allow_endpoint].
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú\/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/lancedb/embeddings/base.pyr   r      s   „ òr   r   c                   óF  — e Zd ZU dZdZeed<    e«       Zeed<    e«       Z	e
ed<   ed„ «       Zede
de
fd	„«       Zede
de
fd
„«       Zedee   fd„«       Zdeeeee   f      fd„Zedefd„«       Zdee   fd„Zedeeej:                  df      fd„«       Zedeeej:                  df      fd„«       Zdeeej:                  df      fd„Z deeej:                  df      fd„Z!de"deee   ejF                  f   fd„Z$d„ Z%edefd„«       Z&d„ Z'd„ Z(de)defd„Z*defd„Z+y)ÚEmbeddingFunctiona  
    An ABC for embedding functions.

    All concrete embedding functions must implement the following methods:
    1. compute_query_embeddings() which takes a query and returns a list of embeddings
    2. compute_source_embeddings() which returns a list of embeddings for
       the source column
    For text data, the two will be the same. For multi-modal data, the source column
    might be images and the vector column might be text.
    3. ndims() which returns the number of dimensions of the vector column
    é   Úmax_retriesÚ_ndimsÚ_original_argsc                 ó*  — ddl m} | j                  |«      } | di |¤Ž}t        |«      |_        |j                  «       �4d| j                  v r&|j                  j                  d|j                  «       |j                  «       j                  |«       |S )z>
        Create an instance of the embedding function
        r   ©ÚEmbeddingFunctionRegistryÚtrust_remote_coder   )Úregistryr!   Ú$_EmbeddingFunction__resolveVariablesÚdictr   Úremote_code_modelÚmodel_fieldsÚ
setdefaultr"   Úget_instanceÚ_trust)ÚclsÚkwargsr!   Úresolved_kwargsÚinstances        r   ÚcreatezEmbeddingFunction.create1   sŒ   € õ
 	8à×0Ñ0°Ó8ˆÙÑ)˜Ñ)ˆÜ"& v£,ˆÔà×&Ñ&Ó(Ð4Ø# s×'7Ñ'7Ñ7ð ×#Ñ#×.Ñ.Ø# X×%?Ñ%?ôð
 	"×.Ñ.Ó0×7Ñ7¸ÔAØˆr   ÚargsÚreturnc                 ó$   — | j                  |«      S ©N)r$   )r+   r0   s     r   Ú_resolve_variablesz$EmbeddingFunction._resolve_variablesI   s   € à×%Ñ% dÓ+Ð+r   c                 ó¾  — ddl m} t        j                  |«      }|j	                  «       }| j                  «       }|j                  «       D ]Á  \  }}t        |t        «      r2|j                  d«      s!||v rt        d|› d�«      }t        |d«       |‚t        |t        «      sŒY|j                  d«      sŒk|dd j                  d	d¬
«      }	t        |	«      dk(  r	 |j                  |	d   «      ||<   Œ¨|	\  }
}	 |j                  |
«      ||<   ŒÃ |S # t        $ r, t        dj!                  |	d   «      «      }t        |d«       |‚w xY w# t        $ r	 |||<   Y �Œw xY w)z/
        Resolve variables in the args
        r   r    z$var:zSensitive key 'z$' cannot be set to a hardcoded valuez2Help: Use $var: to set sensitive keys to variablesé   Nú:)Úmaxsplitr   z#Variable '{}' not found in registryzZHelp: Variables are reset in new Python sessions. Use `registry.set_var` to set variables.)r#   r!   ÚcopyÚdeepcopyr)   Úsensitive_keysÚitemsÚ
isinstanceÚstrÚ
startswithÚ
ValueErrorr	   ÚsplitÚlenÚget_varÚKeyErrorÚformat)r+   r0   r!   Únew_argsr#   r;   ÚkÚvÚexcÚpartsÚnameÚdefaults               r   Ú__resolveVariablesz$EmbeddingFunction.__resolveVariablesM   sp  € õ
 	8ä—=‘= Ó&ˆà,×9Ñ9Ó;ˆØ×+Ñ+Ó-ˆØ—N‘NÓ$ò 	.‰DˆAˆqÜ˜!œSÔ!¨!¯,©,°wÔ*?ÀAÈÑDWÜ Ø% a SÐ(LÐMó�ô ˜ÐRÔSØ�	ä˜!œSÕ! a§l¡l°7Õ&;Ø˜!˜"˜Ÿ™ C°!˜Ó4�Ü�u“: ’?ð"Ø&.×&6Ñ&6°u¸Q±xÓ&@˜ šð %*‘M�D˜'ð.Ø&.×&6Ñ&6°tÓ&<˜ šð5	.ð: ˆøô! $ò 	"Ü(ØA×HÑHÈÈqÉÓRó˜ô !ØðGôð
 "˜	ð	"ûô $ò .Ø&-˜ œð.ús   ÃDÃ:E
Ä5EÅ
EÅEc                  ó   — g S )z 
        Return a list of keys that are sensitive and should not be allowed
        to be set to hardcoded values in the config. For example, API keys.
        r   r   r   r   r;   z EmbeddingFunction.sensitive_keysw   s	   € ð ˆ	r   c                  ó   — y)aÊ  
        Return the model whose repository code this function runs when its
        `trust_remote_code` field is enabled, as a `(name, revision)` pair.

        Return None if the function never runs code from a model repository.
        When the function is restored from table metadata, `trust_remote_code`
        is only enabled for models allowed with
        [allow_remote_code][lancedb.embeddings.EmbeddingFunctionRegistry.allow_remote_code].
        Nr   ©Úselfs    r   r&   z#EmbeddingFunction.remote_code_model   s   € ð r   c                 ój   — | j                   j                  d«      }|�t        |j                  «      S dS )zÊ
        The `trust_remote_code` to use for table metadata that omits it. Only
        tables written before the flag was always recorded omit it, so this is
        the default from that time.
        r"   F)r'   ÚgetÚboolrL   )r+   Úfields     r   Ú!_legacy_trust_remote_code_defaultz3EmbeddingFunction._legacy_trust_remote_code_default‹   s4   € ð × Ñ ×$Ñ$Ð%8Ó9ˆØ&+Ð&7Œt�E—M‘MÓ"ÐB¸UÐBr   c                 ó   — g S )a  
        Return the non-default URLs this function sends data or credentials to.

        When the function is restored from table metadata, each of these must be
        allowed with
        [allow_endpoint][lancedb.embeddings.EmbeddingFunctionRegistry.allow_endpoint].
        r   rP   s    r   Ú	endpointszEmbeddingFunction.endpoints•   s	   € ð ˆ	r   Nc                  ó   — y)zÞ
        Compute the embeddings for a given user query

        Returns
        -------
        A list of embeddings for each input. The embedding of each input can be None
        when the embedding is not valid.
        Nr   ©rQ   r0   r,   s      r   Úcompute_query_embeddingsz*EmbeddingFunction.compute_query_embeddingsŸ   s   € ð 	r   c                  ó   — y)zäCompute the embeddings for the source column in the database

        Returns
        -------
        A list of embeddings for each input. The embedding of each input can be None
        when the embedding is not valid.
        Nr   rZ   s      r   Úcompute_source_embeddingsz+EmbeddingFunction.compute_source_embeddings«   s   € ð 	r   c                 óP   —  t        | j                  | j                  ¬«      |i |¤ŽS )zâCompute the embeddings for a given user query with retries

        Returns
        -------
        A list of embeddings for each input. The embedding of each input can be None
        when the embedding is not valid.
        ©r   )r   r[   r   rZ   s      r   Ú#compute_query_embeddings_with_retryz5EmbeddingFunction.compute_query_embeddings_with_retry¶   s9   € ð
Ô-Ø×)Ñ)°t×7GÑ7Gô
ð ð
ð ñ	
ð 	
r   c                 óP   —  t        | j                  | j                  ¬«      |i |¤ŽS )zòCompute the embeddings for the source column in the database with retries.

        Returns
        -------
        A list of embeddings for each input. The embedding of each input can be None
        when the embedding is not valid.
        r_   )r   r]   r   rZ   s      r   Ú$compute_source_embeddings_with_retryz6EmbeddingFunction.compute_source_embeddings_with_retryÇ   s5   € ð
Ô-Ø×*Ñ*¸×8HÑ8Hô
àðàñð 	r   Útextsc                 óø   — t        |t        «      r|g}|S t        |t        j                  «      r|j	                  «       }|S t        |t        j
                  «      r|j                  «       j	                  «       }|S )z?
        Sanitize the input to the embedding function.
        )r=   r>   ÚpaÚArrayÚ	to_pylistÚChunkedArrayÚcombine_chunks)rQ   rc   s     r   Úsanitize_inputz EmbeddingFunction.sanitize_inputÕ   sk   € ô �eœSÔ!Ø�GˆEð
 ˆô	 ˜œrŸx™xÔ(Ø—O‘OÓ%ˆEð ˆô ˜œrŸ™Ô/Ø×(Ñ(Ó*×4Ñ4Ó6ˆEØˆr   c                 óH   — t        | d«      st        d«      ‚| j                  S )Nr   zAEmbeddingFunction was not created with EmbeddingFunction.create())Úhasattrr@   r   rP   s    r   Úsafe_model_dumpz!EmbeddingFunction.safe_model_dumpá   s+   € Ü�tÐ-Ô.ÜØSóð ð ×"Ñ"Ð"r   c                  ó   — y)z<
        Return the dimensions of the vector column
        Nr   rP   s    r   ÚndimszEmbeddingFunction.ndimsè   ó   € ð
 	r   c                 ód   — t        |j                  dd«      xs i «      }| |d<   t        dd|i|¤ŽS )z€
        Creates a pydantic Field that can automatically annotate
        the source column for this embedding function
        Újson_schema_extraNÚsource_column_forr   ©r%   Úpopr   ©rQ   r,   rr   s      r   ÚSourceFieldzEmbeddingFunction.SourceFieldï   s@   € ô
 ! §¡Ð,?ÀÓ!FÒ!LÈ"ÓMÐØ15ÐÐ-Ñ.ÜÑCÐ'8ÐC¸FÑCÐCr   c                 ód   — t        |j                  dd«      xs i «      }| |d<   t        dd|i|¤ŽS )aH  
        Creates a pydantic Field that can automatically annotate
        the target vector column for this embedding function.

        The field can be annotated as ``list[float]`` for compatibility with
        static type checkers. LanceDB will infer the fixed vector dimension from
        this embedding function.
        rr   NÚvector_column_forr   rt   rv   s      r   ÚVectorFieldzEmbeddingFunction.VectorFieldø   s@   € ô ! §¡Ð,?ÀÓ!FÒ!LÈ"ÓMÐØ15ÐÐ-Ñ.ÜÑCÐ'8ÐC¸FÑCÐCr   Ú_EmbeddingFunction__valuec                 óJ   — t        |d«      syt        | «      t        |«      k(  S )NÚ__dict__F)rl   Úvars)rQ   r{   s     r   Ú__eq__zEmbeddingFunction.__eq__  s"   € Ü�w 
Ô+ØÜ�D‹zœT '›]Ñ*Ð*r   c                 óX   — t        t        t        | «      j                  «       «      «      S r3   )ÚhashÚ	frozensetr~   r<   rP   s    r   Ú__hash__zEmbeddingFunction.__hash__
  s   € Ü”Iœd 4›j×.Ñ.Ó0Ó1Ó2Ð2r   ),r   r   r   r   r   ÚintÚ__annotations__r   r   r   r%   Úclassmethodr/   r4   r$   Ústaticmethodr   r>   r;   r   r   r&   rT   rV   rX   r   Úlistr   ÚnpÚarrayr[   r]   r`   rb   r   Úndarrayrj   rm   ro   rw   rz   Úobjectr   rƒ   r   r   r   r   r      sø  … ñ
ð  €K�ÓÙ“-€FˆCÓÙ&›=€N�DÓ(àñó ðð. ð, dð ,¨tò ,ó ð,ð ð' dð '¨tò 'ó ð'ðR ð˜D ™Iò ó ðð
 8¨E°#°xÀ±}Ð2DÑ,EÑ#Fó 
ð ðC°$ò Có ðCð˜4 ™9ó ð ð	¸4ÀÀbÇhÁhÐPTÀnÑ@UÑ;Vò 	ó ð	ð ð¸DÀÀrÇxÁxÐQUÀ~ÑAVÑ<Wò ó ðð
à	ˆe�B—H‘H˜d�NÑ#Ñ	$ó
ð"à	ˆe�B—H‘H˜d�NÑ#Ñ	$óð
 Dð 
¨U°4¸±9¸b¿j¹jÐ3HÑ-Ió 
ò#ð ð�sò ó ðòDòDð+˜fð +¨ó +ð
3˜#ô 3r   r   c                   ó0   — e Zd ZU dZeed<   eed<   eed<   y)ÚEmbeddingFunctionConfigz²
    This model encapsulates the configuration for a embedding function
    in a lancedb table. It holds the embedding function, the source column,
    and the vector column
    Úvector_columnÚsource_columnÚfunctionN)r   r   r   r   r>   r…   r   r   r   r   rŽ   rŽ     s   … ñð ÓØÓØÔr   rŽ   c                   óÚ   — e Zd ZdZdedeeej                  df      fd„Z	de
deeej                  df      fd„Zedeee   ej                  f   deeej                  df      fd„«       Zy)	ÚTextEmbeddingFunctionzH
    A callable ABC for embedding functions that take text as input
    Úqueryr1   Nc                 ó.   —  | j                   |g|¢­i |¤ŽS r3   )r]   )rQ   r”   r0   r,   s       r   r[   z.TextEmbeddingFunction.compute_query_embeddings  s!   € ð .ˆt×-Ñ-¨eÐE°dÒE¸fÑEÐEr   rc   c                 óF   — | j                  |«      }| j                  |«      S r3   )rj   Úgenerate_embeddings©rQ   rc   r0   r,   s       r   r]   z/TextEmbeddingFunction.compute_source_embeddings$  s%   € ð ×#Ñ# EÓ*ˆØ×'Ñ'¨Ó.Ð.r   c                  ó   — y)z+Generate the embeddings for the given textsNr   r˜   s       r   r—   z)TextEmbeddingFunction.generate_embeddings*  rp   r   )r   r   r   r   r>   rˆ   r   r‰   rŠ   r[   r   r]   r   r   r‹   r—   r   r   r   r“   r“     s    „ ñðFØðFà	ˆe�B—H‘H˜d�NÑ#Ñ	$óFð
/Øð/à	ˆe�B—H‘H˜d�NÑ#Ñ	$ó/ð ðØ˜4 ™9 b§j¡jÐ0Ñ1ðà	ˆe�B—H‘H˜d�NÑ#Ñ	$òó ñr   r“   c                   óT   — e Zd ZU dZeed<   eedf   ed<   defd„Zd„ Z	d„ Z
defd	„Zy
)ÚUntrustedEmbeddingFunctionaž  
    Stands in for an embedding function restored from table metadata whose
    settings this process has not approved.

    The table stays usable for anything that doesn't compute embeddings, while
    computing embeddings raises
    [UntrustedEmbeddingConfigError][lancedb.embeddings.UntrustedEmbeddingConfigError].
    The real function is never constructed, since some load their model in
    `__init__`.
    Úfunction_name.Úreasonsr1   c                 ó~   — dj                  d„ | j                  D «       «      }t        d| j                  › d�|z   «      S )Nú
c              3   ó&   K  — | ]	  }d |› �–— Œ y­w)z  - Nr   )Ú.0Úreasons     r   ú	<genexpr>z4UntrustedEmbeddingFunction._error.<locals>.<genexpr>B  s   è ø€ ÒG°˜d 6 (œOÑGùs   ‚zEmbedding function 'zP' was restored from table metadata with settings this process has not approved:
)Újoinr�   r   rœ   )rQ   Údetailss     r   Ú_errorz!UntrustedEmbeddingFunction._errorA  sJ   € Ø—)‘)ÑG¸$¿,¹,ÔGÓGˆÜ,Ø" 4×#5Ñ#5Ð"6ð 7Fð FØHOñPó
ð 	
r   c                 ó"   — | j                  «       ‚r3   ©r¦   rZ   s      r   r[   z3UntrustedEmbeddingFunction.compute_query_embeddingsH  ó   € Ø�k‰k‹mÐr   c                 ó"   — | j                  «       ‚r3   r¨   rZ   s      r   r]   z4UntrustedEmbeddingFunction.compute_source_embeddingsK  r©   r   c                 ó"   — | j                  «       ‚r3   r¨   rP   s    r   ro   z UntrustedEmbeddingFunction.ndimsN  r©   r   N)r   r   r   r   r>   r…   r   r   r¦   r[   r]   r„   ro   r   r   r   r›   r›   2  s@   … ñ	ð ÓØ�3˜�8‰_Óð
Ð5ó 
òòð�sô r   r›   )Úabcr   r   r9   Útypingr   r   r   r   Úlancedb.utilr	   Únumpyr‰   Úpyarrowre   Úpydanticr
   r   r   Úutilsr   r   ÚPermissionErrorr   r   rŽ   r“   r›   r   r   r   ú<module>r´      sh   ð÷ $Û ß /Ó /å !Û Û ß 2Ñ 2ç 7ô	 Oô 	ôo3˜	 3ô o3ôd	 ˜iô 	 ôÐ-ô ô0Ð!2õ r   