
    Ng1                         d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	m
Z
mZ dZdZdZ G d d	e	e          Z G d
 de          ZdS )    )Path)AnyDictList)
Embeddings)	BaseModel
ConfigDictFieldz<Represent the question for retrieving supporting documents: z9Represent this question for searching relevant passages: u9   为这个句子生成表示以用于检索相关文章：c                       e Zd ZU dZdZeed<   	 dZeed<   	 eed<   	  e	e
          Zeeef         ed<   	  e	e
          Zeeef         ed<   	 d	Zeed
<   	 def fdZdedefdZ	 	 	 	 	 	 d"dedededededededefdZ edd          Zdee         deee                  fdZdedee         fdZd edefd!Z xZS )#OpenVINOEmbeddingsa  OpenVINO embedding models.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import OpenVINOEmbeddings

            model_name = "sentence-transformers/all-mpnet-base-v2"
            model_kwargs = {'device': 'CPU'}
            encode_kwargs = {'normalize_embeddings': True}
            ov = OpenVINOEmbeddings(
                model_name_or_path=model_name,
                model_kwargs=model_kwargs,
                encode_kwargs=encode_kwargs
            )
    Nov_model	tokenizermodel_name_or_path)default_factorymodel_kwargsencode_kwargsFshow_progresskwargsc           	      X    t                      j        di | 	 ddlm} 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	 ddt          dt          d	t          d
t          ffd} || j
                  r  |j        | j
        fddi| j        | _        n |j        | j
        fi | j        | _        	 ddlm} n"# t          $ r}t	          d          |d}~ww xY w|                    | j
                  | _        dS )$Initialize the sentence_transformer.r   )OVModelForFeatureExtractionznCould not import optimum-intel python package. Please install it with: pip install -U 'optimum[openvino,nncf]'N)HfApizjCould not import huggingface_hub python package. Please install it with: `pip install -U huggingface_hub`.model_idrevision	subfolderreturnc                    t          |           }|||z  }|                                r0|dz                                   p|dz                                   S  	            }	 |                    | |pd          }|d n t          |                                          fd|j        D             }|dn d}||vp|                    dd          |vS # t          $ r Y d	S w xY w)
Nzopenvino_model.xmlzopenvino_model.binmain)r   c                 V    g | ]%}|j                                       |j         &S )N)	rfilename
startswith).0filenormalized_subfolders     c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/openvino.py
<listcomp>zMOpenVINOEmbeddings.__init__.<locals>.require_model_export.<locals>.<listcomp>V   sD       +3~001EFF 4 N333    z/openvino_model.xmlz.xmlz.binT)r   is_direxists
model_infoas_posixsiblingsreplace	Exception)
r   r   r   	model_dirhf_apir*   model_filesov_model_pathr$   r   s
           @r%   require_model_exportz9OpenVINOEmbeddings.__init__.<locals>.require_model_exportE   s_    XI$%	1	!! "%99AACCC G%(<<DDFFF UWWF#..x(BTf.UU
%-DD4	??3K3K3M3M %    * 3   !( )(0EEE  "4 P$,,VV<<KO    tts   (A5C 
C,+C,exportT)AutoTokenizerzQUnable to import transformers, please install with `pip install -U transformers`. )NN)super__init__optimum.intel.openvinor   ImportErrorhuggingface_hubr   strr   boolr   from_pretrainedr   r   transformersr5   r   )selfr   r   er3   r5   r   	__class__s         @r%   r8   zOpenVINOEmbeddings.__init__/   s   ""6"""	JJJJJJJ 	 	 	:  			------- 	 	 	4  		 CG!	 !	!	%(!	<?!	!	 !	 !	 !	 !	 !	F   788 		G7G' 048<8I DMM
 H7G' +/+< DM	2222222 	 	 	1  	
 '66t7NOOsB   $ 
A>AA 
A-A((A-"C) )
D3DDtextr   c                 r   t          |t                    r;t          t          t	          |                                                              S t          |d          sdS t          |          dk    st          |d         t                    rt          |          S t          d |D                       S )z
        Help function to get the length for the input text. Text can be either
        a list of ints (which means a single text as input), or a tuple of list of ints
        (representing several text inputs to the model).
        __len__   r   c                 ,    g | ]}t          |          S r6   )len)r"   ts     r%   r&   z3OpenVINOEmbeddings._text_length.<locals>.<listcomp>   s    ---1A---r'   )	
isinstancedictrH   nextitervalueshasattrintsumr@   rC   s     r%   _text_lengthzOpenVINOEmbeddings._text_length|   s     dD!! 		/tD//00111y)) 	/1YY!^^z$q'377^t99 -----...r'      T	sentences
batch_sizeshow_progress_barconvert_to_numpyconvert_to_tensormean_poolingnormalize_embeddingsc                 h    	 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	 ddln"# t          $ r}	t          d          |	d}	~	ww xY wdt
          dt
          d	t
          ffd
}|rd}d}t          t                    st          d          sgd}g |	                     fdD                       }fd|D             } |
dt                    |d|           D ]}||||z            } j        j        j        d                                         d         }|j        r                     |ddd          }n,                     |d|                                dd          }  j        di |}|r |||d                   }n|d         dddf         }|r"j        j                            |dd          }|r|                                }                    |           fd|	                    |          D             |r:t                    r                              n6                                n!|r|                    d D                       |rd         S )aw  
        Computes sentence embeddings.

        :param sentences: the sentences to embed.
        :param batch_size: the batch size used for the computation.
        :param show_progress_bar: Whether to output a progress bar.
        :param convert_to_numpy: Whether the output should be a list of numpy vectors.
        :param convert_to_tensor: Whether the output should be one large tensor.
        :param mean_pooling: Whether to pool returned vectors.
        :param normalize_embeddings: Whether to normalize returned vectors.

        :return: By default, a 2d numpy array with shape [num_inputs, output_dimension].
        r   NzCUnable to import numpy, please install with `pip install -U numpy`.)trangezAUnable to import tqdm, please install with `pip install -U tqdm`.zCUnable to import torch, please install with `pip install -U torch`.model_outputattention_maskr   c                 4   | d         }|                     d                              |                                                                          }                    ||z  d                              |                    d          d          z  S )Nr   rF   g&.>)min)	unsqueezeexpandsizefloatrQ   clamp)r^   r_   token_embeddingsinput_mask_expandedtorchs       r%   run_mean_poolingz3OpenVINOEmbeddings.encode.<locals>.run_mean_pooling   s    +  ((,,334D4I4I4K4KLLRRTT   99-0CCQGG%++#''** KV K K  r'   FrE   Tc                 <    g | ]}                     |           S r6   )rS   )r"   senr@   s     r%   r&   z-OpenVINOEmbeddings.encode.<locals>.<listcomp>   s*    'U'U'UC):):3)?)?(?'U'U'Ur'   c                      g | ]
}|         S r6   r6   )r"   idxrU   s     r%   r&   z-OpenVINOEmbeddings.encode.<locals>.<listcomp>   s    HHHsIcNHHHr'   Batches)descdisablerF   pt)padding
truncationreturn_tensors
max_length)rt   rw   ru   rv      )pdimc                      g | ]
}|         S r6   r6   )r"   ro   all_embeddingss     r%   r&   z-OpenVINOEmbeddings.encode.<locals>.<listcomp>   s    WWW#.-WWWr'   c                 6    g | ]}|                                 S r6   )numpy)r"   embs     r%   r&   z-OpenVINOEmbeddings.encode.<locals>.<listcomp>   s     (O(O(O(O(O(Or'   r6   )r~   r:   tqdmr]   rj   r   rJ   r<   rO   argsortrH   r   requestinputsget_partial_shape
is_dynamicr   
get_lengthnn
functional	normalizecpuextendstackTensorasarray)r@   rU   rV   rW   rX   rY   rZ   r[   nprA   r]   rk   input_was_stringlength_sorted_idxsentences_sortedstart_indexsentences_batchlengthfeaturesout_features
embeddingsr|   rj   s   ``                   @@r%   encodezOpenVINOEmbeddings.encode   s   .	 	 	 	X 		####### 	 	 	V 		LLLL 	 	 	X 	
		3 		 		 		 		 		 		 		 		  	%$ i%% 	$Wy.
 .
 	$ #I# JJ'U'U'U'U9'U'U'UVVHHHH6GHHH!6s9~~z	GXCX
 
 
 	. 	.K /{[:=U/UVO]*1!4FFHHKF  >>#TdSW *    >>#(%0022##' *   )4=44844L 3--lHEU<VWW

)!_QQQT2
# S"X0:::PQ:RR
   .'^^--
!!*----WWWWDU9V9VWWW 	Q>"" 0!&^!<!<!& 	QZZ(O(O(O(O(OPPN 	/+A.Ns<    
*%*5 
AAAA 
A<'A77A<forbidr6   )extraprotected_namespacestextsc                     t          t          d |                    } | j        |fd| j        i| j        }|                                S )Compute doc embeddings using a HuggingFace transformer model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                 .    |                      dd          S )N
 )r-   )xs    r%   <lambda>z4OpenVINOEmbeddings.embed_documents.<locals>.<lambda>  s    199T3#7#7 r'   rW   )listmapr   r   r   tolistr@   r   r   s      r%   embed_documentsz"OpenVINOEmbeddings.embed_documents  sd     S77??@@ T[
 
%)%7
;?;M
 

   """r'   c                 :    |                      |g          d         S )Compute query embeddings using a HuggingFace transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   rR   s     r%   embed_queryzOpenVINOEmbeddings.embed_query  s     ##TF++A..r'   
model_pathc                     | j                                          | j                             |           | j                            |           dS )NT)r   halfsave_pretrainedr   )r@   r   s     r%   
save_modelzOpenVINOEmbeddings.save_model  sI     	%%j111&&z222tr'   )rT   FTFFT)__name__
__module____qualname____doc__r   r   __annotations__r   r<   r
   rK   r   r   r   r   r=   r8   rP   rS   r   r	   model_configr   rf   r   r   r   __classcell__rB   s   @r%   r   r      s5         " Hc Is(#(5#>#>#>L$sCx.>>>1$)E$$?$?$?M4S>???RM4)KP KP KP KP KP KP KPZ/ / / / / /* "'!%"'"%)q qq q  	q
 q  q q #q 
q q q qf :H2FFFL#T#Y #4U3D # # # #"	/ 	/U 	/ 	/ 	/ 	/ 
       r'   r   c                        e Zd ZU dZeZeed<   	 dZeed<   	 de	f fdZ
dee         deee                  fd	Zd
edee         fdZ xZS )OpenVINOBgeEmbeddingsa  OpenVNO BGE embedding models.

    Bge Example:
        .. code-block:: python

            from langchain_community.embeddings import OpenVINOBgeEmbeddings

            model_name = "BAAI/bge-large-en-v1.5"
            model_kwargs = {'device': 'CPU'}
            encode_kwargs = {'normalize_embeddings': True}
            ov = OpenVINOBgeEmbeddings(
                model_name_or_path=model_name,
                model_kwargs=model_kwargs,
                encode_kwargs=encode_kwargs
            )
    query_instruction embed_instructionr   c                 h     t                      j        di | d| j        v rt          | _        dS dS )r   z-zhNr6   )r7   r8   r    DEFAULT_QUERY_BGE_INSTRUCTION_ZHr   )r@   r   rB   s     r%   r8   zOpenVINOBgeEmbeddings.__init__@  sD    ""6"""D+++%ED""" ,+r'   r   r   c                 n      fd|D             }  j         |fi  j        }|                                S )r   c                 L    g | ] }j         |                    d d          z   !S )r   r   )r   r-   )r"   rI   r@   s     r%   r&   z9OpenVINOBgeEmbeddings.embed_documents.<locals>.<listcomp>P  s/    NNN1'!))D#*>*>>NNNr'   )r   r   r   r   s   `  r%   r   z%OpenVINOBgeEmbeddings.embed_documentsG  sM     ONNNNNN T[==$*<==
  """r'   rC   c                     |                     dd          } | j        | j        |z   fi | j        }|                                S )r   r   r   )r-   r   r   r   r   )r@   rC   	embeddings      r%   r   z!OpenVINOBgeEmbeddings.embed_queryT  sM     ||D#&&DK 6 =TTASTT	!!!r'   )r   r   r   r    DEFAULT_QUERY_BGE_INSTRUCTION_ENr   r<   r   r   r   r8   r   rf   r   r   r   r   s   @r%   r   r   )  s          " >s===1s4F F F F F F F#T#Y #4U3D # # # #" "U " " " " " " " "r'   r   N)pathlibr   typingr   r   r   langchain_core.embeddingsr   pydanticr   r	   r
   DEFAULT_QUERY_INSTRUCTIONr   r   r   r   r6   r'   r%   <module>r      s          " " " " " " " " " " 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 C  @ ! $_  V V V V VJ V V Vr6" 6" 6" 6" 6". 6" 6" 6" 6" 6"r'   