
    Ng                     X    d dl mZmZmZmZ d dlmZ d dlmZm	Z	  G d dee          Z
dS )    )AnyDictListOptional)
Embeddings)	BaseModel
ConfigDictc                       e Zd ZdZ	 	 	 	 	 	 	 d dededed	ee         d
ee         dedee         dee         de	ddf fdZ
d!dZ edd          Zde	de	fdZede	de	fd            Zede	de	de	fd            Zdee         deee                  fdZdee         deee                  fdZdedee         fdZ xZS )"QuantizedBiEncoderEmbeddingsaK  Quantized bi-encoders embedding models.

    Please ensure that you have installed optimum-intel and ipex.

    Input:
        model_name: str = Model name.
        max_seq_len: int = The maximum sequence length for tokenization. (default 512)
        pooling_strategy: str =
            "mean" or "cls", pooling strategy for the final layer. (default "mean")
        query_instruction: Optional[str] =
            An instruction to add to the query before embedding. (default None)
        document_instruction: Optional[str] =
            An instruction to add to each document before embedding. (default None)
        padding: Optional[bool] =
            Whether to add padding during tokenization or not. (default True)
        model_kwargs: Optional[Dict] =
            Parameters to add to the model during initialization. (default {})
        encode_kwargs: Optional[Dict] =
            Parameters to add during the embedding forward pass. (default {})

    Example:

    from langchain_community.embeddings import QuantizedBiEncoderEmbeddings

    model_name = "Intel/bge-small-en-v1.5-rag-int8-static"
    encode_kwargs = {'normalize_embeddings': True}
    hf = QuantizedBiEncoderEmbeddings(
        model_name,
        encode_kwargs=encode_kwargs,
        query_instruction="Represent this sentence for searching relevant passages: "
    )
       meanNT
model_namemax_seq_lenpooling_strategyquery_instructiondocument_instructionpaddingmodel_kwargsencode_kwargskwargsreturnc	                 Z    t                      j        di |	 || _        || _        || _        || _        |pi | _        |pi | _        | j                            dd          | _	        | j                            dd          | _
        || _        || _        |                                  d S )Nnormalize_embeddingsF
batch_size     )super__init__model_name_or_pathr   poolingr   r   r   get	normalizer   r   r   
load_model)selfr   r   r   r   r   r   r   r   r   	__class__s             h/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/optimum_intel.pyr   z%QuantizedBiEncoderEmbeddings.__init__)   s     	""6"""",&'*0b(.B+//0FNN,00rBB!2$8!    c                 p   	 ddl m} n"# t          $ r}t          d          |d }~ww xY w	 ddlm}  |j        | j        fi | j        | _        n-# t          $ r }t          d| j         d| d          d }~ww xY w|                    | j                  | _
        | j                                         d S )	Nr   )AutoTokenizerzQUnable to import transformers, please install with `pip install -U transformers`.)	IPEXModelz
Failed to load model z, due to the following error:
a  
Please ensure that you have installed optimum-intel and ipex correctly,using:

pip install optimum[neural-compressor]
pip install intel_extension_for_pytorch

For more information, please visit:
* Install optimum-intel as shown here: https://github.com/huggingface/optimum-intel.
* Install IPEX as shown here: https://intel.github.io/intel-extension-for-pytorch/index.html#installation?platform=cpu&version=v2.2.0%2Bcpu.
)pretrained_model_name_or_path)transformersr)   ImportErroroptimum.intelr*   from_pretrainedr   r   transformer_model	Exceptiontransformer_tokenizereval)r$   r)   er*   s       r&   r#   z'QuantizedBiEncoderEmbeddings.load_modelE   s;   	2222222 	 	 	1  	
	//////%>Y%>'& &+/+<& &D""  	 	 	-     	 &3%B%B*.*A &C &
 &
" 	##%%%%%s'   	 
(#(#A 
A:A55A:allowr   )extraprotected_namespacesinputsc                    	 dd l }n"# t          $ r}t          d          |d }~ww xY w|                                5   | j        d
i |}| j        dk    r|                     ||d                   }n0| j        dk    r|                     |          }nt          d          | j        r"|j	        j
                            |dd	          }|cd d d            S # 1 swxY w Y   d S )Nr   CUnable to import torch, please install with `pip install -U torch`.r   attention_maskclszpooling method no supported      )pdimr   )torchr-   inference_moder0   r    _mean_pooling_cls_pooling
ValueErrorr"   nn
functional)r$   r8   rA   r4   outputsembs         r&   _embedz#QuantizedBiEncoderEmbeddings._embedl   sa   	LLLL 	 	 	U 	 !!## 	 	,d,66v66G|v%%((&9I2JKK&&''00 !>???~ Eh)33C1!3DD	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s"    
&!&BCC!CrH   c                 f    t          | t                    r	| d         }n| d         }|d d df         S )Nlast_hidden_stater   )
isinstancedict)rH   token_embeddingss     r&   rD   z)QuantizedBiEncoderEmbeddings._cls_pooling   s@    gt$$ 	*&':;&qz1%%r'   r;   c                    	 dd l }n"# t          $ r}t          d          |d }~ww xY wt          | t                    r	| d         }n| d         }|                    d                              |                                                                          }|                    ||z  d          }|	                    |                    d          d          }||z  S )Nr   r:   rL   r>   g&.>)min)
rA   r-   rM   rN   	unsqueezeexpandsizefloatsumclamp)rH   r;   rA   r4   rO   input_mask_expandedsum_embeddingssum_masks           r&   rC   z*QuantizedBiEncoderEmbeddings._mean_pooling   s    	LLLL 	 	 	U 	 gt$$ 	*&':;  'qz$$R((//0@0E0E0G0GHHNNPP 	 #36I#I1MM;;266q99t;DD((s    
&!&textsc                     |                      || j        d| j        d          }|                     |                                          S )NTpt)
max_length
truncationr   return_tensors)r2   r   r   rJ   tolist)r$   r\   r8   s      r&   _embed_textz(QuantizedBiEncoderEmbeddings._embed_text   sP    ++'L , 
 
 {{6""))+++r'   c                     	 ddl }n"# t          $ r}t          d          |d}~ww xY w	 ddlm} n"# t          $ r}t          d          |d}~ww xY w fd|D             }|                    |dg                                          }|d	          j        z  |d
<   t          |                    d
g          d                             t                              }g } ||d          D ]}	| 	                    |	          z  }|S )zEmbed a list of text documents using the Optimized Embedder model.

        Input:
            texts: List[str] = List of text documents to embed.
        Output:
            List[List[float]] = The embeddings of each text document.
        r   NzEUnable to import pandas, please install with `pip install -U pandas`.)tqdmzAUnable to import tqdm, please install with `pip install -U tqdm`.c                 6    g | ]}j         r
j         |z   n|S r   )r   ).0dr$   s     r&   
<listcomp>z@QuantizedBiEncoderEmbeddings.embed_documents.<locals>.<listcomp>   s>     
 
 
 .2-FMD%))A
 
 
r'   r\   )columnsindexbatch_indexBatches)desc)
pandasr-   re   	DataFramereset_indexr   listgroupbyapplyrc   )
r$   r\   pdr4   re   docstext_list_dfbatchesvectorsbatchs
   `         r&   embed_documentsz,QuantizedBiEncoderEmbeddings.embed_documents   sx   	 	 	 	W 		!!!!!!! 	 	 	S 	
 
 
 

 
 
 ||D7)|<<HHJJ '37&;t&N]# |++]O<<WEKKDQQRRT'	222 	/ 	/Et''...GGs$    
'"'2 
AAAtextc                 \    | j         r
| j         |z   }|                     |g          d         S )Nr   )r   rc   )r$   r|   s     r&   embed_queryz(QuantizedBiEncoderEmbeddings.embed_query   s5    ! 	1)D0D''**r'   )r   r   NNTNN)r   N)__name__
__module____qualname____doc__strintr   boolr   r   r   r#   r	   model_configrJ   staticmethodrD   rC   r   rV   rc   r{   r~   __classcell__)r%   s   @r&   r   r      s        H  &+/.2'+(,   	
 $C= 'sm  tn  ~  
     8 &  &  &  &D :  L
S S    ( &c &c & & & \& )s )C )C ) ) ) \)&,c ,tDK/@ , , , ,%T#Y %4U3D % % % %N+ +U + + + + + + + +r'   r   N)typingr   r   r   r   langchain_core.embeddingsr   pydanticr   r	   r   r   r'   r&   <module>r      s    , , , , , , , , , , , , 0 0 0 0 0 0 * * * * * * * *I+ I+ I+ I+ I+9j I+ I+ I+ I+ I+r'   