a
    bg5                     @   sZ   d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZ G dd de
eZdS )    )AnyDictListOptional)
Embeddings)get_from_dict_or_env)	BaseModelFieldmodel_validatorc                   @   s   e Zd ZU dZedddZeed< eddZe	ed< e	ed	< dZ
ee ed
< eddeeedddZe	ee dddZee	 eee  dddZdS )ZhipuAIEmbeddingsu  ZhipuAI embedding model integration.

    Setup:

        To use, you should have the ``zhipuai`` python package installed, and the
        environment variable ``ZHIPU_API_KEY`` set with your API KEY.

        More instructions about ZhipuAi Embeddings, you can get it
        from  https://open.bigmodel.cn/dev/api#vector

        .. code-block:: bash

            pip install -U zhipuai
            export ZHIPU_API_KEY="your-api-key"

    Key init args — completion params:
        model: Optional[str]
            Name of ZhipuAI model to use.
        api_key: str
            Automatically inferred from env var `ZHIPU_API_KEY` if not provided.

    See full list of supported init args and their descriptions in the params section.

    Instantiate:

        .. code-block:: python

            from langchain_community.embeddings import ZhipuAIEmbeddings

            embed = ZhipuAIEmbeddings(
                model="embedding-2",
                # api_key="...",
            )

    Embed single text:
        .. code-block:: python

            input_text = "The meaning of life is 42"
            embed.embed_query(input_text)

        .. code-block:: python

            [-0.003832892, 0.049372625, -0.035413884, -0.019301128, 0.0068899863, 0.01248398, -0.022153955, 0.006623926, 0.00778216, 0.009558191, ...]


    Embed multiple text:
        .. code-block:: python

            input_texts = ["This is a test query1.", "This is a test query2."]
            embed.embed_documents(input_texts)

        .. code-block:: python

            [
                [0.0083934665, 0.037985895, -0.06684559, -0.039616987, 0.015481004, -0.023952313, ...],
                [-0.02713102, -0.005470169, 0.032321047, 0.042484466, 0.023290444, 0.02170547, ...]
            ]
    NT)defaultexcludeclientzembedding-2)r   modelapi_key
dimensionsbefore)mode)valuesreturnc                 C   sR   t |dd|d< z"ddlm} ||d d|d< W n tyL   tdY n0 |S )z/Validate that auth token exists in environment.r   ZZHIPUAI_API_KEYr   )ZhipuAI)r   r   zUCould not import zhipuai python package.Please install it with `pip install zhipuai`.)r   Zzhipuair   ImportError)clsr   r    r   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/zhipuai.pyvalidate_environmentO   s    
z&ZhipuAIEmbeddings.validate_environment)textr   c                 C   s   |  |g}|d S )z
        Embeds a text using the AutoVOT algorithm.

        Args:
            text: A text to embed.

        Returns:
            Input document's embedded list.
        r   )embed_documents)selfr   respr   r   r   embed_query_   s    
zZhipuAIEmbeddings.embed_query)textsr   c                 C   sL   | j dur$| jjj| j|| j d}n| jjj| j|d}dd |jD }|S )a0  
        Embeds a list of text documents using the AutoVOT algorithm.

        Args:
            texts: A list of text documents to embed.

        Returns:
            A list of embeddings for each document in the input list.
            Each embedding is represented as a list of float values.
        N)r   inputr   )r   r"   c                 S   s   g | ]
}|j qS r   )Z	embedding).0rr   r   r   
<listcomp>       z5ZhipuAIEmbeddings.embed_documents.<locals>.<listcomp>)r   r   
embeddingscreater   data)r   r!   r   r'   r   r   r   r   l   s    
z!ZhipuAIEmbeddings.embed_documents)__name__
__module____qualname____doc__r	   r   r   __annotations__r   strr   r   intr
   classmethodr   r   r   floatr    r   r   r   r   r   r      s   
;r   N)typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   Zpydanticr   r	   r
   r   r   r   r   r   <module>   s   