
    7|h                         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e	e      Zy)	    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                      e Zd ZU dZ edd      Zee   ed<   	  e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      Zeed<   	  edd      Zee   ed<   	  ed      dedefd       Zdedee   fdZdee   deee      fdZdedee   fdZdee   deee      fdZdedee   fdZy)HunyuanEmbeddingszTencent Hunyuan embedding models API by Tencent.

    For more information, see https://cloud.tencent.com/document/product/1729
    	secret_idN)aliasdefaulthunyuan_secret_id
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                 |   t        t        |dd            |d<   t        t        |dd            |d<   	 ddlm} ddlm} ddlm} dd	lm	}  |       }d|j                  _         ||d   j                         |d   j                               }||d<    |||d   |      |d<   |S # t        $ r t        d
      w xY w)z?Validate that api key and python package exists in environment.r   HUNYUAN_SECRET_IDr   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   tencentcloud.common.credentialr'   *tencentcloud.common.profile.client_profiler(   -tencentcloud.hunyuan.v20230901.hunyuan_clientr)   %tencentcloud.hunyuan.v20230901.modelsr*   ImportErrorhttpProfilepre_conn_pool_sizeget_secret_value)clsr"   r'   r(   r)   r*   client_profile
credentials           e/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/hunyuan.pyvalidate_environmentz&HunyuanEmbeddings.validate_environment    s     '< ##'
"# (= $$(
#$		APSQ '89""5&'88:'(99;


 !4}(VH5E~Vx#  	J 	s   B& &B;textc                 \   | j                   t        d      | j                         }||_        | j                  j	                  |      }t        j                  |j                               }|j                  d      }|st        d      |d   j                  d      }|st        d      |S )Nz!Request class is not initialized.Dataz,Occur hunyuan embedding error: Data is emptyr   	Embeddingz1Occur hunyuan embedding error: Embedding is empty)
r   AssertionErrorInputr   GetEmbeddingjsonloadsto_json_stringgetRuntimeError)selfr9   requestresponse	_responsedata	embeddings          r7   _embed_textzHunyuanEmbeddings._embed_textJ   s    # !DEE""$;;++G4$(JJx/F/F/H$I	/8}}V/DMNNGKK,	RSS    textsc                     g }| j                   r	 ddlm}  ||d      }n|}|D ]"  }|j                  | j	                  |             $ |S # t        $ r}t        d      |d}~ww xY w)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   rO   r0   appendembed_query)rE   rM   
embeddingsrO   e_iterr9   s          r7   embed_documentsz!HunyuanEmbeddings.embed_documents^   s    
!!% %.ABEE 	6Dd..t45	6   !/ 	s   A 	A&A!!A&c                 $    | j                  |      S )zEmbed query text.)rK   rE   r9   s     r7   rS   zHunyuanEmbeddings.embed_queryr   s    %%rL   c                 L   K   t        d| j                  |       d{   S 7 w)zAsynchronous Embed search docs.N)r
   rW   )rE   rM   s     r7   aembed_documentsz"HunyuanEmbeddings.aembed_documentsv   s!     $T4+?+?GGGG   $"$c                 L   K   t        d| j                  |       d{   S 7 w)zAsynchronous Embed query text.N)r
   rS   rY   s     r7   aembed_queryzHunyuanEmbeddings.aembed_queryz   s!     $T4+;+;TBBBBr\   )__name__
__module____qualname____doc__r   r   r   r   __annotations__r   r   r   r   intr   boolr   r   r   r   r   r   r8   strr   floatrK   rW   rS   r[   r^    rL   r7   r   r   
   sJ   
 .3d-Sx	*S.3,PT.U+U4BFG01B( $#$S#t#=d3FC3""'d"CK$C0(#'$ '4 ' $'R U (T#Y 4U3D (& &U &HDI H$tE{:K HCs CtE{ CrL   r   )r@   typingr   r   r   r   r   r   langchain_core.embeddingsr	   langchain_core.runnables.configr
   langchain_core.utilsr   r   pydanticr   r   r   r   r   rh   rL   r7   <module>rn      s/     ; ; 0 ; L A ArC
I rCrL   