a
    bg1                     @   sr   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	 d	e	eZG d
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                
       s  e Zd ZU dZdZeed< dZeed< eed< e	e
dZeeef ed< e	e
dZeeef ed< d	Zeed
< ed fddZeedddZd!eeeeeeeedddZedddZee eee  dddZeee dddZeeddd Z  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              
      s@  t  jf i | zddlm} W n. tyP } ztd|W Y d}~n
d}~0 0 zddlm  W n. ty } ztd|W Y d}~n
d}~0 0 dtttt	d fdd	}|| j
r|j| j
fd
di| j| _n|j| j
fi | j| _zddlm} W n0 ty, } ztd|W Y d}~n
d}~0 0 || 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                    s   t | }|d ur|| }| r<|d   p:|d   S  }zn|j| |pPdd}|d u rbd n
t |   fdd|jD }|d u rdn  d}||vp|dd	|vW S  ty   Y d
S 0 d S )Nzopenvino_model.xmlzopenvino_model.binmain)r   c                    s&   g | ]} d u s|j  r|j qS )N)Z	rfilename
startswith).0fileZnormalized_subfolder u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/openvino.py
<listcomp>V   s   zMOpenVINOEmbeddings.__init__.<locals>.require_model_export.<locals>.<listcomp>z/openvino_model.xmlz.xmlz.binT)r   is_direxists
model_infoas_posixZsiblingsreplace	Exception)r   r   r   Z	model_dirZhf_apir&   Zmodel_filesZov_model_pathr   r    r"   require_model_exportE   s0    
z9OpenVINOEmbeddings.__init__.<locals>.require_model_exportZexportT)AutoTokenizerzQUnable to import transformers, please install with `pip install -U transformers`.)NN)super__init__Zoptimum.intel.openvinor   ImportErrorZhuggingface_hubr   strr   boolr   Zfrom_pretrainedr   r   Ztransformersr+   r   )selfr   r   er*   r+   	__class__r   r"   r-   /   sX     #

zOpenVINOEmbeddings.__init__textr   c                 C   sd   t |trttt| S t|ds,dS t|dksFt |d trNt|S tdd |D S 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                 S   s   g | ]}t |qS r!   )lenr   tr!   r!   r"   r#          z3OpenVINOEmbeddings._text_length.<locals>.<listcomp>N)	
isinstancedictr9   nextitervalueshasattrintsumr1   r6   r!   r!   r"   _text_length|   s    

zOpenVINOEmbeddings._text_length   T)	sentences
batch_sizeshow_progress_barconvert_to_numpyconvert_to_tensormean_poolingnormalize_embeddingsr   c              
      sv  zddl }W n. ty: }	 ztd|	W Y d}	~	n
d}	~	0 0 zddlm}
 W n. tyz }	 ztd|	W Y d}	~	n
d}	~	0 0 zddlW n. ty }	 ztd|	W Y d}	~	n
d}	~	0 0 tttdfdd	}|rd
}d
}ttstdsgd}g  |	fddD }fdd|D }|
dt
|d| dD ]}||||  }jjjd  d }|jrj|dddd}nj|d| ddd}jf i |}|r|||d }n|d dddf }|rjjj|ddd}|r| } | q> fdd|	|D  |rJt
 r@  n  n|rd|dd  D  |rr 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                    sD   | d }| d|  } || d j|ddd S )Nr   r8   g&.>)min)Z	unsqueezeexpandsizefloatrD   clamp)rP   rQ   Ztoken_embeddingsZinput_mask_expanded)torchr!   r"   run_mean_pooling   s    
z3OpenVINOEmbeddings.encode.<locals>.run_mean_poolingFr7   Tc                    s   g | ]}  | qS r!   )rF   )r   Zsenr1   r!   r"   r#      r<   z-OpenVINOEmbeddings.encode.<locals>.<listcomp>c                    s   g | ]} | qS r!   r!   r   idx)rH   r!   r"   r#      r<   ZBatches)descdisabler8   pt)padding
truncationreturn_tensors
max_length)r`   rc   ra   rb   rQ      )pdimc                    s   g | ]} | qS r!   r!   r[   )all_embeddingsr!   r"   r#      r<   c                 S   s   g | ]}|  qS r!   )numpy)r   Zembr!   r!   r"   r#      r<   )rh   r.   ZtqdmrO   rX   r   r=   r/   rB   Zargsortr9   r   requestinputsZget_partial_shapeZ
is_dynamicr   Z
get_lengthnnZ
functional	normalizecpuextendstackZTensorZasarray)r1   rH   rI   rJ   rK   rL   rM   rN   npr2   rO   rY   Zinput_was_stringZlength_sorted_idxZsentences_sortedstart_indexZsentences_batchlengthfeaturesZout_features
embeddingsr!   )rg   r1   rH   rX   r"   encode   s    


zOpenVINOEmbeddings.encodeZforbidr!   )extraZprotected_namespacestextsr   c                 C   s4   t td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                 S   s   |  ddS )N
 )r(   )xr!   r!   r"   <lambda>  r<   z4OpenVINOEmbeddings.embed_documents.<locals>.<lambda>rJ   )listmapru   r   r   tolistr1   rx   rt   r!   r!   r"   embed_documents  s    
z"OpenVINOEmbeddings.embed_documentsc                 C   s   |  |gd S )Compute query embeddings using a HuggingFace transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   rE   r!   r!   r"   embed_query  s    	zOpenVINOEmbeddings.embed_query)
model_pathr   c                 C   s&   | j   | j | | j| dS )NT)r   ZhalfZsave_pretrainedr   )r1   r   r!   r!   r"   
save_model  s    
zOpenVINOEmbeddings.save_model)rG   FTFFT)__name__
__module____qualname____doc__r   r   __annotations__r   r/   r	   r>   r   r   r   r   r0   r-   rC   rF   ru   r   Zmodel_configr   rV   r   r   r   __classcell__r!   r!   r3   r"   r
      s>   
M      sr
   c                       sp   e Zd ZU dZeZeed< dZeed< e	d fddZ
ee eee  dd	d
Zeee dddZ  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                    s&   t  jf i | d| jv r"t| _dS )r   z-zhN)r,   r-   r    DEFAULT_QUERY_BGE_INSTRUCTION_ZHr   )r1   r   r3   r!   r"   r-   @  s    
zOpenVINOBgeEmbeddings.__init__rw   c                    s.    fdd|D } j |fi  j}| S )ry   c                    s   g | ]} j |d d qS )rz   r{   )r   r(   r:   rZ   r!   r"   r#   P  r<   z9OpenVINOBgeEmbeddings.embed_documents.<locals>.<listcomp>)ru   r   r   r   r!   rZ   r"   r   G  s    	z%OpenVINOBgeEmbeddings.embed_documentsr5   c                 C   s.   | dd}| j| j| fi | j}| S )r   rz   r{   )r(   ru   r   r   r   )r1   r6   Z	embeddingr!   r!   r"   r   T  s    	z!OpenVINOBgeEmbeddings.embed_query)r   r   r   r    DEFAULT_QUERY_BGE_INSTRUCTION_ENr   r/   r   r   r   r-   r   rV   r   r   r   r!   r!   r3   r"   r   )  s   
r   N)pathlibr   typingr   r   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r	   ZDEFAULT_QUERY_INSTRUCTIONr   r   r
   r   r!   r!   r!   r"   <module>   s     