
    ^Njv6                        U d dl mZmZmZmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZmZ d dlmZ d dlmZmZ  edd	d
dd eddd      d       edd	ddd eddd      d       eddddd ed      d       eddd
dd  ed!d"d      d       ed#dddd$ ed%      d       ed&d'd(dd) ed*d+d      d       ed,dd-d.d/ ed,      d0       ed1dd-d.d) ed1      d0       ed2dd-d.d  ed2      d0       ed3d	d-d.d4 ed5      d0       ed6d	d7d.d8 ed6      d0       ed9dd-d.d: ed9      d0       ed;d	d<d.d= ed;      d>      gZee   ed?<    G d@ dAeee         Z G dB dCee         ZyD)E    )AnyIterableSequenceType)
NumpyArrayOnnxProviderDevice)OnnxOutputContext)define_cache_dir	normalize)OnnxTextModelTextEmbeddingWorker)TextEmbeddingBase)DenseModelDescriptionModelSourcezBAAI/bge-base-eni   z}Text embeddings, Unimodal (text), English, 512 input tokens truncation, Prefixes for queries/documents: necessary, 2023 year.mitgzG?zQdrant/fast-bge-base-enzGhttps://storage.googleapis.com/qdrant-fastembed/fast-bge-base-en.tar.gzT)hfurl_deprecated_tar_structzmodel_optimized.onnx)modeldimdescriptionlicense
size_in_GBsources
model_filezBAAI/bge-base-en-v1.5zText embeddings, Unimodal (text), English, 512 input tokens truncation, Prefixes for queries/documents: not so necessary, 2023 year.gzG?zqdrant/bge-base-en-v1.5-onnx-qzLhttps://storage.googleapis.com/qdrant-fastembed/fast-bge-base-en-v1.5.tar.gzzBAAI/bge-large-en-v1.5   g333333?zqdrant/bge-large-en-v1.5-onnx)r   z
model.onnxzBAAI/bge-small-eni  gp=
ף?zQdrant/bge-small-enzHhttps://storage.googleapis.com/qdrant-fastembed/BAAI-bge-small-en.tar.gzBAAI/bge-small-en-v1.5gx&?zqdrant/bge-small-en-v1.5-onnx-qzBAAI/bge-small-zh-v1.5i   zText embeddings, Unimodal (text), Chinese, 512 input tokens truncation, Prefixes for queries/documents: not so necessary, 2023 year.g
ףp=
?zQdrant/bge-small-zh-v1.5zMhttps://storage.googleapis.com/qdrant-fastembed/fast-bge-small-zh-v1.5.tar.gzz"mixedbread-ai/mxbai-embed-large-v1z}Text embeddings, Unimodal (text), English, 512 input tokens truncation, Prefixes for queries/documents: necessary, 2024 year.z
apache-2.0g{Gz?zonnx/model.onnxz#snowflake/snowflake-arctic-embed-xsz"snowflake/snowflake-arctic-embed-sz"snowflake/snowflake-arctic-embed-mgQ?z"Snowflake/snowflake-arctic-embed-mz'snowflake/snowflake-arctic-embed-m-longz~Text embeddings, Unimodal (text), English, 2048 input tokens truncation, Prefixes for queries/documents: necessary, 2024 year.gHzG?z"snowflake/snowflake-arctic-embed-lgRQ?zjinaai/jina-clip-v1zkText embeddings, Multimodal (text&image), English, Prefixes for queries/documents: not necessary, 2024 yearg?zonnx/text_model.onnxsupported_onnx_modelsc                       e Zd ZdZedee   fd       Zddddej                  dd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dz  de
dz  def fdZ	 	 dde
ee
   z  dededz  dedee   f
dZeded   fd       Zdee
ef   dedee
ef   fdZdededee   fdZd dZ	 d!de
ee
   z  dededefdZ xZS )"OnnxTextEmbeddingz+Implementation of the Flag Embedding model.returnc                     t         S )z
        Lists the supported models.

        Returns:
            list[DenseModelDescription]: A list of DenseModelDescription objects containing the model information.
        )r   clss    n/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastembed/text/onnx_embedding.py_list_supported_modelsz(OnnxTextEmbedding._list_supported_models   s
     %$    r   NF
model_name	cache_dirthreads	providerscuda
device_ids	lazy_load	device_idspecific_model_pathkwargsc
                    t        |   |||fi |
 || _        || _        | j	                  |
      | _        || _        || _        d| _        ||| _        n | j                  | j                  d   | _        | j                  |      | _
        t        t        |            | _        |	| _        | j                  | j                  | j                  | j                   | j                        | _        | j                  s| j%                          yy)a  
        Args:
            model_name (str): The name of the model to use.
            cache_dir (str, optional): The path to the cache directory.
                                       Can be set using the `FASTEMBED_CACHE_PATH` env variable.
                                       Defaults to `fastembed_cache` in the system's temp directory.
            threads (int, optional): The number of threads single onnxruntime session can use. Defaults to None.
            providers (Optional[Sequence[OnnxProvider]], optional): The list of onnxruntime providers to use.
                Mutually exclusive with the `cuda` and `device_ids` arguments. Defaults to None.
            cuda (Union[bool, Device], optional): Whether to use cuda for inference. Mutually exclusive with `providers`
                Defaults to Device.AUTO.
            device_ids (Optional[list[int]], optional): The list of device ids to use for data parallel processing in
                workers. Should be used with `cuda` equals to `True`, `Device.AUTO` or `Device.CUDA`, mutually exclusive
                with `providers`. Defaults to None.
            lazy_load (bool, optional): Whether to load the model during class initialization or on demand.
                Should be set to True when using multiple-gpu and parallel encoding. Defaults to False.
            device_id (Optional[int], optional): The device id to use for loading the model in the worker process.
            specific_model_path (Optional[str], optional): The specific path to the onnx model dir if it should be imported from somewhere else

        Raises:
            ValueError: If the model_name is not in the format <org>/<model> e.g. BAAI/bge-base-en.
        Nr   )local_files_onlyr1   )super__init__r,   r/   _select_exposed_session_options_extra_session_optionsr.   r-   r0   _get_model_descriptionmodel_descriptionstrr   r*   _specific_model_pathdownload_model_local_files_only
_model_dirload_onnx_model)selfr)   r*   r+   r,   r-   r.   r/   r0   r1   r2   	__class__s              r&   r6   zOnnxTextEmbedding.__init__   s    F 	YB6B""&*&J&J6&R#$	 &* &DN__(!__Q/DN!%!<!<Z!H-i89$7!--""NN!33 $ 9 9	 . 
 ~~  " r(   	documents
batch_sizeparallelc              +     K    | j                   d| j                  t        | j                        |||| j                  | j
                  | j                  | j                  | j                  | j                  d|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
        )r)   r*   rC   rD   rE   r,   r-   r.   r4   r1   extra_session_optionsN )
_embed_documentsr)   r;   r*   r,   r-   r.   r>   r<   r8   )rA   rC   rD   rE   r2   s        r&   embedzOnnxTextEmbedding.embed  s}     , )4(( 
$..)!nn!33 $ 9 9"&"="=
 
 	
 	
s   A;B=B>BzTextEmbeddingWorker[NumpyArray]c                     t         S )N)OnnxTextEmbeddingWorkerr$   s    r&   _get_worker_classz#OnnxTextEmbedding._get_worker_class+  s    &&r(   
onnx_inputc                     |S )z,
        Preprocess the onnx input.
        rH   )rA   rN   r2   s      r&   _preprocess_onnx_inputz(OnnxTextEmbedding._preprocess_onnx_input/  s
     r(   outputc                     |j                   }|j                  dk(  r|d d df   }t	        |      S |j                  dk(  r|}t	        |      S t        d|j                         )N   r      zUnsupported embedding shape: )model_outputndim
ValueErrorshaper   )rA   rQ   r2   
embeddingsprocessed_embeddingss        r&   _post_process_onnx_outputz+OnnxTextEmbedding._post_process_onnx_output7  sv     ((
??a#-ad#3 
 -..	 __!#-  -.. <Z=M=M<NOPPr(   c           	          | j                  | j                  | j                  j                  | j                  | j
                  | j                  | j                  | j                         y )N)	model_dirr   r+   r,   r-   r0   rG   )	_load_onnx_modelr?   r:   r   r+   r,   r-   r0   r8   )rA   s    r&   r@   z!OnnxTextEmbedding.load_onnx_modelD  sP    oo--88LLnnnn"&"="= 	 	
r(   textsc                 ,     | j                   |fd|i|S )NrD   )_token_count)rA   r_   rD   r2   s       r&   token_countzOnnxTextEmbedding.token_countO  s!     !t  H:HHHr(   )   N)r"   N)r   )__name__
__module____qualname____doc__classmethodlistr   r'   r	   AUTOr;   intr   r   boolr   r6   r   r   rJ   r   rM   dictrP   r
   r[   r@   rb   __classcell__)rB   s   @r&   r!   r!      s   5%t,A'B % % 3 $"37$kk'+ $*.=#=# :=# t	=#
 L)D0=# Vm=# I$=# =# :=# !4Z=# =#D #	#
#&#
 #
 *	#

 #
 
*	#
J '$'H"I ' 'sJ/;>	c:o	/'/36/	*	/	
 =AI8C=(I69ILOI	Ir(   r!   c                   $    e Zd ZdedededefdZy)rL   r)   r*   r2   r"   c                      t        d||dd|S )N   )r)   r*   r+   rH   )r!   )rA   r)   r*   r2   s       r&   init_embeddingz&OnnxTextEmbeddingWorker.init_embeddingV  s)     ! 
!
 	
 	
r(   N)rd   re   rf   r;   r   r!   rr   rH   r(   r&   rL   rL   U  s-    

 
 	

 

r(   rL   N)typingr   r   r   r   fastembed.common.typesr   r   r	   fastembed.common.onnx_modelr
   fastembed.common.utilsr   r   fastembed.text.onnx_text_modelr   r   "fastembed.text.text_embedding_baser   "fastembed.common.model_descriptionr   r   r   ri   __annotations__r!   rL   rH   r(   r&   <module>r{      s   0 0 C C 9 > M @ Q  D (Y#'

 *  %K /^#'

 *  &K >? !D $Z#'

 *  &K @A) &K )_#'

 *  2D CD$ 3D DE$ 2D CD$ 2D CD$ 7D HI$ 2D CD$ #' 45)Cm6 t12 m`XI)=+D XIv
1*= 
r(   