a
    bg                     @   s~   d dl Z d dlmZmZ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 d dlmZmZmZmZ G dd de	eZdS )	    N)AnyDictListLiteralOptionalType)
Embeddings)run_in_executor)convert_to_secret_strget_from_dict_or_env)	BaseModelField	SecretStrmodel_validatorc                   @   s$  e Zd ZU dZedddZee ed< edddZ	ee ed< dZ
ed	 ed
< dZeed< dZeed< edddZeed< edddZee ed< eddeedddZeee dddZee eee  dddZeee dddZee eee  dd d!Zeee dd"d#ZdS )$HunyuanEmbeddingszTencent Hunyuan embedding models API by Tencent.

    For more information, see https://cloud.tencent.com/document/product/1729
    Z	secret_idN)aliasdefaulthunyuan_secret_idZ
secret_keyhunyuan_secret_keyap-guangzhou)r   z
ap-beijingregioni   embedding_ctx_lengthFshow_progress_barT)r   excludeclientrequest_clsbefore)mode)valuesreturnc                 C   s   t t|dd|d< t t|dd|d< z4ddlm} ddlm} ddlm} dd	lm	} W n t
yv   t
d
Y n0 | }d|j_||d  |d  }||d< |||d ||d< |S )z?Validate that api key and python package exists in environment.r   ZHUNYUAN_SECRET_IDr   ZHUNYUAN_SECRET_KEYr   )
Credential)ClientProfile)HunyuanClient)GetEmbeddingRequestz{Could not import tencentcloud sdk python package. Please install it with `pip install "tencentcloud-sdk-python>=3.0.1139"`.   r   r   r   )r
   r   Ztencentcloud.common.credentialr    Z*tencentcloud.common.profile.client_profiler!   Z-tencentcloud.hunyuan.v20230901.hunyuan_clientr"   Z%tencentcloud.hunyuan.v20230901.modelsr#   ImportErrorZhttpProfileZpre_conn_pool_sizeZget_secret_value)clsr   r    r!   r"   r#   Zclient_profileZ
credential r'   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/hunyuan.pyvalidate_environment    s@    


z&HunyuanEmbeddings.validate_environment)textr   c                 C   sn   | j d u rtd|   }||_| j|}t| }|d}|sPt	d|d d}|sjt	d|S )Nz!Request class is not initialized.ZDataz,Occur hunyuan embedding error: Data is emptyr   Z	Embeddingz1Occur hunyuan embedding error: Embedding is empty)
r   AssertionErrorZInputr   ZGetEmbeddingjsonloadsZto_json_stringgetRuntimeError)selfr*   requestresponse	_responsedataZ	embeddingr'   r'   r(   _embed_textJ   s    

zHunyuanEmbeddings._embed_text)textsr   c              
   C   sz   g }| j rXzddlm} W n. tyH } ztd|W Y d}~n
d}~0 0 ||dd}n|}|D ]}|| | q`|S )zEmbed search docs.r   )tqdmzPackage tqdm must be installed if show_progress_bar=True. Please install with 'pip install tqdm' or set show_progress_bar=False.NzHunyuan Embedding)iterabledesc)r   r7   r%   appendembed_query)r0   r6   Z
embeddingsr7   eZ_iterr*   r'   r'   r(   embed_documents^   s    z!HunyuanEmbeddings.embed_documentsc                 C   s
   |  |S )zEmbed query text.)r5   r0   r*   r'   r'   r(   r;   r   s    zHunyuanEmbeddings.embed_queryc                    s   t d| j|I dH S )zAsynchronous Embed search docs.N)r	   r=   )r0   r6   r'   r'   r(   aembed_documentsv   s    z"HunyuanEmbeddings.aembed_documentsc                    s   t d| j|I dH S )zAsynchronous Embed query text.N)r	   r;   r>   r'   r'   r(   aembed_queryz   s    zHunyuanEmbeddings.aembed_query)__name__
__module____qualname____doc__r   r   r   r   __annotations__r   r   r   r   intr   boolr   r   r   r   r   r   r)   strr   floatr5   r=   r;   r?   r@   r'   r'   r'   r(   r   
   s   
)r   )r,   typingr   r   r   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.runnables.configr	   Zlangchain_core.utilsr
   r   Zpydanticr   r   r   r   r   r'   r'   r'   r(   <module>   s    