a
    bgF                     @  st   d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZmZ d dlmZ eeZG dd dee	ZdS )	    )annotationsN)AnyDictListOptional)
Embeddings)get_from_dict_or_envpre_init)	BaseModelc                   @  s   e Zd ZU dZdZded< dZded< dZded< d	Zded
< dZ	ded< dZ
ded< dZded< ded< edddddZdddddZddddd ZdS )!VolcanoEmbeddingsz)`Volcengine Embeddings` embedding models.NzOptional[str]
volcano_ak
volcano_skz*maas-api.ml-platform-cn-beijing.volces.comstrhostz
cn-beijingregionzbge-large-zhmodelz1.0versiond   int
chunk_sizer   clientr   )valuesreturnc                 C  s   t |dd|d< t |dd|d< zFddlm} ||d |d }||d  ||d  ||d	< W n ty   td
Y n0 |S )a6  
        Validate whether volcano_ak and volcano_sk in the environment variables or
        configuration file are available or not.

        init volcano embedding client with `ak`, `sk`, `host`, `region`

        Args:

            values: a dictionary containing configuration information, must include the
            fields of volcano_ak and volcano_sk
        Returns:

            a dictionary containing configuration information. If volcano_ak and
            volcano_sk are not provided in the environment variables or configuration
            file,the original values will be returned; otherwise, values containing
            volcano_ak and volcano_sk will be returned.
        Raises:

            ValueError: volcengine package not found, please install it with
            `pip install volcengine`
        r   ZVOLC_ACCESSKEYr   ZVOLC_SECRETKEYr   )MaasServicer   r   r   zMvolcengine package not found, please install it with `pip install volcengine`)r   volcengine.maasr   Zset_akZset_skImportError)clsr   r   r    r   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/volcengine.pyvalidate_environment.   s*    
z&VolcanoEmbeddings.validate_environmentzList[float])textr   c                 C  s   |  |gd S )Nr   )embed_documents)selfr    r   r   r   embed_query^   s    zVolcanoEmbeddings.embed_queryz	List[str]zList[List[float]])textsr   c           	        s    fddt dt jD }g }|D ]} j jd|d}z4ddlm}  j|}|	dd |d D  W q* |y } zt
d	| W Y d
}~q*d
}~0 0 q*|S )a_  
        Embeds a list of text documents using the AutoVOT algorithm.

        Args:
            texts (List[str]): A list of text documents to embed.

        Returns:
            List[List[float]]: A list of embeddings for each document in the input list.
                            Each embedding is represented as a list of float values.
        c                   s   g | ]}|| j   qS r   )r   ).0ir"   r$   r   r   
<listcomp>l   s   z5VolcanoEmbeddings.embed_documents.<locals>.<listcomp>r   )namer   )r   input)MaasExceptionc                 S  s   g | ]}|d  qS )Z	embeddingr   )r%   resr   r   r   r(   }       datazembed by volcengine Error: N)rangelenr   r   r   r   r+   r   Z
embeddingsextend
ValueError)	r"   r$   Ztext_in_chunkslstchunkreqr+   resper   r'   r   r!   a   s"    &z!VolcanoEmbeddings.embed_documents)__name__
__module____qualname____doc__r   __annotations__r   r   r   r   r   r   r	   r   r#   r!   r   r   r   r   r      s   
/r   )
__future__r   loggingtypingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   r	   Zpydanticr
   	getLoggerr8   loggerr   r   r   r   r   <module>   s   
