
    7|h1                     ~    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y)    )Path)AnyDictList)
Embeddings)	BaseModel
ConfigDictFieldz<Represent the question for retrieving supporting documents: z9Represent this question for searching relevant passages: u9   为这个句子生成表示以用于检索相关文章：c                   X    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        |   di | 	 ddlm} 	 ddlm 	 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} |j                  | j                        | _        y# t        $ r}t	        d      |d}~ww xY w# t        $ r}t	        d      |d}~ww xY w# t        $ r}t	        d      |d}~ww xY w)$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  }|j                         r*|dz  j                          xs |dz  j                          S  
       }	 |j                  | |xs d      }|d nt        |      j	                         }|j
                  D cg c]+  }||j                  j                  |      r|j                  - }}|dn| d}	|	|vxs |	j                  dd      |vS c c}w # t        $ r Y yw xY w)	Nzopenvino_model.xmlzopenvino_model.binmain)r   z/openvino_model.xmlz.xmlz.binT)
r   is_direxists
model_infoas_posixsiblings	rfilename
startswithreplace	Exception)r   r   r   	model_dirhf_apir!   normalized_subfolderfilemodel_filesov_model_pathr   s             f/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/openvino.pyrequire_model_exportz9OpenVINOEmbeddings.__init__.<locals>.require_model_exportE   sF    XI$%	1	!"%99AACC G%(<<DDFF WF#..x(BTf.U
%-D4	?3K3K3M %
 !+ 3 3+3~~001EF NN  !( )011DE  "4 P$,,VV<KO  s%   AC2 0C-$C2 -C2 2	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   transformersr1   r   )selfr   r   er/   r1   r   	__class__s         @r.   r4   zOpenVINOEmbeddings.__init__/   s`   "6"	J	- CG!	!	%(!	<?!	!	F   7 78G7GG''048<8I8IDM
 H7GG''+/+<+<DM	2 '66t7N7NOK  	: 		  	4 		n  	1 	sF   C C2 .D 	C/C**C/2	D;DD	D)D$$D)textr   c                 8   t        |t              r+t        t        t	        |j                                           S t        |d      syt        |      dk(  st        |d   t              rt        |      S t        |D cg c]  }t        |       c}      S c c}w )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   )	
isinstancedictlennextitervalueshasattrintsum)r<   r?   ts      r.   _text_lengthzOpenVINOEmbeddings._text_length|   sw     dD!tD/011y)Y!^z$q'37t9 -1A-..-s   >B	sentences
batch_sizeshow_progress_barconvert_to_numpyconvert_to_tensormean_poolingnormalize_embeddingsc                 D   	 ddl }	 ddlm}
 	 ddldt
        dt
        d	t
        ffd
}|rd}d}t        |t              st        |d      s|g}d}g }|j                  |D cg c]  }| j                  |        c}      }|D cg c]  }||   	 }} |
dt        |      |d|       D ]  }||||z    }| j                  j                  j                  d   j                         d   }|j                   r| j#                  |ddd      }n$| j#                  |d|j%                         dd      } | j                  di |}|r |||d         }n|d   dddf   }|r(j&                  j(                  j+                  |dd      }|r|j-                         }|j/                  |        |j                  |      D cg c]  }||   	 }}|r.t        |      rj1                  |      }n@j3                         }n/|r-|j5                  |D cg c]  }|j                          c}      }|r|d   }|S # t        $ r}	t        d      |	d}	~	ww xY w# t        $ r}	t        d      |	d}	~	ww xY w# t        $ r}	t        d      |	d}	~	ww xY wc c}w c c}w c c}w c c}w )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                     | d   }|j                  d      j                  |j                               j                         }j	                  ||z  d      j                  |j	                  d      d      z  S )Nr   rB   g&.>)min)	unsqueezeexpandsizefloatrK   clamp)rW   rX   token_embeddingsinput_mask_expandedtorchs       r.   run_mean_poolingz3OpenVINOEmbeddings.encode.<locals>.run_mean_pooling   s    +  ((,334D4I4I4KLRRT   99-0CCQG%++#''* KV K      FrA   TBatches)descdisablerB   pt)padding
truncationreturn_tensors
max_length)rj   rm   rk   rl      )pdimr2   )numpyr6   tqdmrV   rc   r   rC   r8   rI   argsortrM   rE   r   requestinputsget_partial_shape
is_dynamicr   
get_lengthnn
functional	normalizecpuextendstackTensorasarray)r<   rN   rO   rP   rQ   rR   rS   rT   npr=   rV   rd   input_was_stringall_embeddingssenlength_sorted_idxidxsentences_sortedstart_indexsentences_batchlengthfeaturesout_features
embeddingsembrc   s                            @r.   encodezOpenVINOEmbeddings.encode   s   .	
	#
			3 		 		 		 $ i%Wy.
 #I# JJ9'UC):):3)?(?'UV6GHsIcNHH!s9~z	GXCX
 	.K /{[:=UVO]]**11!4FFHKF  >>#TdSW *   >>#(%002##' *  )4==484L-lHEU<VW
)!_QT2
#"XX00:::PQ:R
  '^^-
!!*-?	.B :<DU9VW#.-WW>"!&^!<!&ZZ(O(OPN+A.Nq  	U	  	S	  	U	6 (VHF X )Ps]   H7 I I1 #JJJJ7	I II	I.I))I.1	J:JJforbidr2   )extraprotected_namespacestextsc                     t        t        d |            } | j                  |fd| j                  i| j                  }|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                 &    | j                  dd      S )N
 )r&   )xs    r.   <lambda>z4OpenVINOEmbeddings.embed_documents.<locals>.<lambda>  s    199T3#7 re   rP   )listmapr   r   r   tolist)r<   r   r   s      r.   embed_documentsz"OpenVINOEmbeddings.embed_documents  sV     S7?@ T[[
%)%7%7
;?;M;M

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

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   )r<   r?   s     r.   embed_queryzOpenVINOEmbeddings.embed_query  s     ##TF+A..re   
model_pathc                     | j                   j                          | j                   j                  |       | j                  j                  |       y)NT)r   halfsave_pretrainedr   )r<   r   s     r.   
save_modelzOpenVINOEmbeddings.save_model  s;     	%%j1&&z2re   )   FTFFT)__name__
__module____qualname____doc__r   r   __annotations__r   r8   r
   rD   r   r   r   r   r9   r4   rJ   rM   r   r	   model_configr   r_   r   r   r   __classcell__r>   s   @r.   r   r      s_   " Hc Is(#(#>L$sCx.>1$)$$?M4S>?RM4)KP KPZ/ / /* "'!%"'"%)qq q  	q
 q  q q #q 
qf H2FL#T#Y #4U3D #"	/ 	/U 	/ 
re   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                 X    t        |   di | d| j                  v rt        | _        yy)r   z-zhNr2   )r3   r4   r    DEFAULT_QUERY_BGE_INSTRUCTION_ZHr   )r<   r   r>   s     r.   r4   zOpenVINOBgeEmbeddings.__init__@  s.    "6"D+++%ED" ,re   r   r   c                     |D cg c]!  }| j                   |j                  dd      z   # }} | j                  |fi | j                  }|j	                         S c c}w )r   r   r   )r   r&   r   r   r   )r<   r   rL   r   s       r.   r   z%OpenVINOBgeEmbeddings.embed_documentsG  s_     INN1''!))D#*>>NN T[[=$*<*<=
  "" Os   &Ar?   c                     |j                  dd      } | j                  | j                  |z   fi | j                  }|j	                         S )r   r   r   )r&   r   r   r   r   )r<   r?   	embeddings      r.   r   z!OpenVINOBgeEmbeddings.embed_queryT  sI     ||D#&DKK 6 6 =TASAST	!!re   )r   r   r   r    DEFAULT_QUERY_BGE_INSTRUCTION_ENr   r8   r   r   r   r4   r   r_   r   r   r   r   s   @r.   r   r   )  sk    " >s=1s4F F#T#Y #4U3D #" "U "re   r   N)pathlibr   typingr   r   r   langchain_core.embeddingsr   pydanticr   r	   r
   DEFAULT_QUERY_INSTRUCTIONr   r   r   r   r2   re   r.   <module>r      sT     " " 0 1 1 C  @ ! $_  VJ Vr6". 6"re   