a
    bg6                     @   sZ   d dl mZ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d	S )
    )AnyDictListOptional)
Embeddings)	BaseModel
ConfigDictFieldzBAAI/bge-small-en-v1.5z9Represent this question for searching relevant passages: u9   为这个句子生成表示以用于检索相关文章：c                       s   e Zd ZU dZdZeed< eZe	ed< dZ
ee	 ed< eedZee	ef ed< eedZee	ef ed< eZe	ed	< d
Ze	ed< ed fddZedddZee	 eee  dddZe	ee dddZ  ZS )IpexLLMBgeEmbeddingsa  Wrapper around the BGE embedding model
    with IPEX-LLM optimizations on Intel CPUs and GPUs.

    To use, you should have the ``ipex-llm``
    and ``sentence_transformers`` package installed. Refer to
    `here <https://python.langchain.com/v0.1/docs/integrations/text_embedding/ipex_llm/>`_
    for installation on Intel CPU.

    Example on Intel CPU:
        .. code-block:: python

            from langchain_community.embeddings import IpexLLMBgeEmbeddings

            embedding_model = IpexLLMBgeEmbeddings(
                model_name="BAAI/bge-large-en-v1.5",
                model_kwargs={},
                encode_kwargs={"normalize_embeddings": True},
            )

    Refer to
    `here <https://python.langchain.com/v0.1/docs/integrations/text_embedding/ipex_llm_gpu/>`_
    for installation on Intel GPU.

    Example on Intel GPU:
        .. code-block:: python

            from langchain_community.embeddings import IpexLLMBgeEmbeddings

            embedding_model = IpexLLMBgeEmbeddings(
                model_name="BAAI/bge-large-en-v1.5",
                model_kwargs={"device": "xpu"},
                encode_kwargs={"normalize_embeddings": True},
            )
    Nclient
model_namecache_folder)default_factorymodel_kwargsencode_kwargsquery_instruction embed_instruction)kwargsc              
      s  t  jf i | zddl}ddlm}m} W n@ tyn } z(d}td| d| d|W Y d}~n
d}~0 0 d| jvrd	| jd< | jd d
vrtd| jd  d|j	| j
fd| ji| j| _|| j| _|| j| _| jd dkr| j d| _d| j
v rt| _dS )z$Initialize the sentence_transformer.r   N)_optimize_post_optimize_prezChttps://python.langchain.com/v0.1/docs/integrations/text_embedding/zDCould not import ipex_llm or sentence_transformers. Please refer to zD/ipex_llm/ for install required packages on Intel CPU. And refer to z;/ipex_llm_gpu/ for install required packages on Intel GPU. Zdevicecpu)r   xpuzXIpexLLMBgeEmbeddings currently only supports device to be 'cpu' or 'xpu', but you have: .r   r   z-zh)super__init__sentence_transformersZipex_llm.transformers.convertr   r   ImportErrorr   
ValueErrorZSentenceTransformerr   r   r   Zhalfto DEFAULT_QUERY_BGE_INSTRUCTION_ZHr   )selfr   r   r   r   excbase_url	__class__ u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/ipex_llm.pyr   C   sJ    	

zIpexLLMBgeEmbeddings.__init__Zforbidr&   )extraZprotected_namespaces)textsreturnc                    s0    fdd|D } j j|fi  j}| S )zCompute 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   g | ]} j |d d qS )
 )r   replace).0tr!   r&   r'   
<listcomp>x       z8IpexLLMBgeEmbeddings.embed_documents.<locals>.<listcomp>)r   encoder   tolist)r!   r)   Z
embeddingsr&   r0   r'   embed_documentso   s    	z$IpexLLMBgeEmbeddings.embed_documents)textr*   c                 C   s0   | dd}| jj| j| fi | j}| S )zCompute query embeddings using a HuggingFace transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r+   r,   )r-   r   r3   r   r   r4   )r!   r6   Z	embeddingr&   r&   r'   embed_query|   s    	z IpexLLMBgeEmbeddings.embed_query)__name__
__module____qualname____doc__r   r   __annotations__DEFAULT_BGE_MODELr   strr   r   r	   dictr   r   r    DEFAULT_QUERY_BGE_INSTRUCTION_ENr   r   r   r   Zmodel_configr   floatr5   r7   __classcell__r&   r&   r$   r'   r
      s   
#*r
   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r	   r=   r@   r    r
   r&   r&   r&   r'   <module>   s   