
    sOi                         d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZ d dlmZ  G d	 d
e      Zy)    )AnyIterableSequenceType)asdict)DenseModelDescription)
NumpyArrayDevice)OnnxProvider)Colbert)JinaColbert) LateInteractionTextEmbeddingBasec                       e Zd ZU eegZeee      e	d<   e
deeeef      fd       Ze
dee   fd       Zdddej$                  ddfdededz  d	edz  d
ee   dz  deez  dee   dz  dedef fdZedefd       Ze
dedefd       Z	 	 ddeee   z  dededz  dedee   f
dZdeee   z  dedee   fdZ	 	 	 ddeee   z  dededededefdZ xZ S )LateInteractionTextEmbeddingEMBEDDINGS_REGISTRYreturnc                 Z    | j                         D cg c]  }t        |       c}S c c}w )a  
        Lists the supported models.

        Returns:
            list[dict[str, Any]]: A list of dictionaries containing the model information.

            Example:
                ```
                [
                    {
                        "model": "colbert-ir/colbertv2.0",
                        "dim": 128,
                        "description": "Late interaction model",
                        "license": "mit",
                        "size_in_GB": 0.44,
                        "sources": {
                            "hf": "colbert-ir/colbertv2.0",
                        },
                        "model_file": "model.onnx",
                    },
                ]
                ```
        )_list_supported_modelsr   )clsmodels     /volume1/homes/robertsu/coba/app/.venv/lib/python3.12/site-packages/fastembed/late_interaction/late_interaction_text_embedding.pylist_supported_modelsz2LateInteractionTextEmbedding.list_supported_models   s+    2 ,/+E+E+GH+G%u+GHHHs   (c                 j    g }| j                   D ]!  }|j                  |j                                # |S N)r   extendr   )r   result	embeddings      r   r   z3LateInteractionTextEmbedding._list_supported_models,   s1    .000IMM)::<= 1    NF
model_name	cache_dirthreads	providerscuda
device_ids	lazy_loadkwargsc           
          t        |   ||fi | | j                  D ]=  }	|	j                         }
t	        fd|
D              s( |	|f|||||d|| _         y  t        d d      )Nc              3   t   K   | ]/  }j                         |j                  j                         k(   1 y wr   )lowerr   ).0r   r   s     r   	<genexpr>z8LateInteractionTextEmbedding.__init__.<locals>.<genexpr>A   s/     [JZ:##%):):)<<JZs   58)r!   r"   r#   r$   r%   zModel z is not supported in LateInteractionTextEmbedding.Please check the supported models using `LateInteractionTextEmbedding.list_supported_models()`)super__init__r   r   anyr   
ValueError)selfr   r    r!   r"   r#   r$   r%   r&   EMBEDDING_MODEL_TYPEsupported_models	__class__s    `         r   r-   z%LateInteractionTextEmbedding.__init__3   s     	YB6B$($<$< 3JJL[JZ[[1	 $')'	 	
  %= ZL !m m
 	
r   c                 r    | j                    | j                  | j                        | _         | j                   S )z+Get the embedding size of the current model)_embedding_sizeget_embedding_sizer   )r0   s    r   embedding_sizez+LateInteractionTextEmbedding.embedding_sizeS   s3     '#'#:#:4??#KD ###r   c                    | j                         }d}|D ];  }|j                  j                         |j                         k(  s/|j                  } n |*|D cg c]  }|j                   }}t	        d| d|       |S c c}w )a0  Get the embedding size of the passed model

        Args:
            model_name (str): The name of the model to get embedding size for.

        Returns:
            int: The size of the embedding.

        Raises:
            ValueError: If the model name is not found in the supported models.
        NzEmbedding size for model z" was None. Available model names: )r   r   r)   dimr/   )r   r   descriptionsr7   descriptionmodel_namess         r   r6   z/LateInteractionTextEmbedding.get_embedding_sizeZ   s     113%)'K  &&(J,<,<,>>!, ( !@LM;,,KM+J< 8**58   Ns   B	documents
batch_sizeparallelc              +   ^   K    | j                   j                  |||fi |E d{    y7 w)a  
        Encode a list of documents into list of embeddings.
        We use mean pooling with attention so that the model can handle variable-length inputs.

        Args:
            documents: Iterator of documents or single document to embed
            batch_size: Batch size for encoding -- higher values will use more memory, but be faster
            parallel:
                If > 1, data-parallel encoding will be used, recommended for offline encoding of large datasets.
                If 0, use all available cores.
                If None, don't use data-parallel processing, use default onnxruntime threading instead.

        Returns:
            List of embeddings, one per document
        N)r   embed)r0   r=   r>   r?   r&   s        r   rA   z"LateInteractionTextEmbedding.embedu   s+     , $4::##Iz8NvNNNs   #-+-queryc              +   Z   K    | j                   j                  |fi |E d{    y7 w)z
        Embeds queries

        Args:
            query (Union[str, Iterable[str]]): The query to embed, or an iterable e.g. list of queries.

        Returns:
            Iterable[NdArray]: The embeddings.
        N)r   query_embed)r0   rB   r&   s      r   rD   z(LateInteractionTextEmbedding.query_embed   s'      *4::))%:6:::s   !+)+textsis_docinclude_extensionc                 D     | j                   j                  |f|||d|S )a  Returns the number of tokens in the texts.

        Args:
            texts (str | Iterable[str]): The list of texts to embed.
            batch_size (int): Batch size for encoding
            is_doc (bool): Whether the texts are documents (disable embedding a query with include_mask=True).
            include_extension (bool): Turn on to count DOC / QUERY marker tokens, and [MASK] token in query mode.

        Returns:
            int: Sum of number of tokens in the texts.
        )r>   rF   rG   )r   token_count)r0   rE   r>   rF   rG   r&   s         r   rI   z(LateInteractionTextEmbedding.token_count   s8    & &tzz%%
!/	

 
 	
r   )   N)i   TF)!__name__
__module____qualname__r   r   r   listr   r   __annotations__classmethoddictstrr   r   r   r   r
   AUTOintr   r   boolr-   propertyr7   r6   r   r	   rA   rD   rI   __classcell__)r3   s   @r   r   r      s   IPR]H^d#CDE^Id4S>&: I I4 t,A'B   !%"37$kk'+

 :
 t	

 L)D0
 Vm
 I$
 
 
@ $ $ $ C C  : #	O#&O O *	O
 O 
*	O0;x}!4 ; ;Q[H\ ;" "'
Xc]"
 
 	

  
 
 

r   r   N)typingr   r   r   r   dataclassesr   "fastembed.common.model_descriptionr   fastembed.common.typesr	   r
   fastembed.commonr   "fastembed.late_interaction.colbertr   'fastembed.late_interaction.jina_colbertr   :fastembed.late_interaction.late_interaction_embedding_baser   r    r   r   <module>ra      s1    0 0  D 5 ) 6 ?
f
#C f
r   