a
    bgD&                     @   s<  d dl Z d dlZd dlmZmZmZ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mZmZmZmZmZmZmZmZ d dlmZmZmZ d dlmZmZm Z m!Z! d dl"m#Z#m$Z$m%Z%m&Z& e'e(Z)ee*d	d
dZ+ee,ef edddZ-ee,ef e	e edddZ.ee,ef edddZ/G dd deZ0dS )    N)AnyDictIteratorListMappingOptionalType)CallbackManagerForLLMRun)BaseChatModelgenerate_from_stream)		AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkHumanMessageHumanMessageChunkSystemMessage)ChatGenerationChatGenerationChunk
ChatResult)convert_to_secret_strget_from_dict_or_envget_pydantic_field_namespre_init)
ConfigDictField	SecretStrmodel_validator)messagereturnc                 C   st   t | tr| j| jd}nVt | tr2d| jd}n>t | trJd| jd}n&t | trbd| jd}ntd|  |S )N)RoleContentsystemuser	assistantzGot unknown type )
isinstancer   rolecontentr   r   r   	TypeError)r    Zmessage_dict r+   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/chat_models/hunyuan.py_convert_message_to_dict!   s    



r-   )_dictr!   c                 C   sn   | d }|dkr&t | ddp ddS |dkr<t| d dS |dkrZt| ddpTddS t| d |dS d S )	Nr"   r$   r#    r)   r%   r&   r)   r(   )r   getr   r   r   )r.   r(   r+   r+   r,   _convert_dict_to_message1   s    r3   )r.   default_classr!   c                 C   sr   |  d}|  dpd}|dks(|tkr2t|dS |dksB|tkrLt|dS |sX|tkrdt||dS ||dS d S )Nr"   r#   r/   r%   r0   r&   r1   )r2   r   r   r   )r.   r4   r(   r)   r+   r+   r,   _convert_delta_to_message_chunk=   s    


r5   )responser!   c                 C   sX   g }| d D ].}t |d }| dd|_|t|d q| d }d|i}t||dS )	NChoicesMessageIdr/   r    Usagetoken_usage)generations
llm_output)r3   r2   idappendr   r   )r6   r=   choicer    r<   r>   r+   r+   r,   _create_chat_resultM   s    rB   c                   @   s  e Zd ZU dZeeeef dddZeedddZ	dZ
ee ed< dZee ed	< dZee ed
< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< eedZeeef ed< eddZedde eeef edddZ!e"eeddd Z#eeeef dd!d"Z$d-e%e& ee%e  ee' ee(d#d$d%Z)d.e%e& ee%e  ee' ee*e+ d#d&d'Z,e%e& eed(d)d*Z-eedd+d,Z.dS )/ChatHunyuanzTencent Hunyuan chat models API by Tencent.

    For more information, see https://cloud.tencent.com/document/product/1729
    )r!   c                 C   s   ddddS )NHUNYUAN_APP_IDHUNYUAN_SECRET_IDHUNYUAN_SECRET_KEY)hunyuan_app_idhunyuan_secret_idhunyuan_secret_keyr+   selfr+   r+   r,   
lc_secrets_   s    zChatHunyuan.lc_secretsc                 C   s   dS )NTr+   rJ   r+   r+   r,   lc_serializableg   s    zChatHunyuan.lc_serializableNrG   rH   rI   F	streaming<   request_timeoutg      ?temperaturetop_pzhunyuan-litemodelstream_moderationTenable_enhancement)default_factorymodel_kwargs)Zpopulate_by_namebefore)mode)valuesr!   c              
   C   s   t | }|di }t|D ]P}||v r8td| d||vrtd| d| d| d ||||< q|| }|rtd| d	||d< |S )
z>Build extra kwargs from additional params that were passed in.rW   zFound z supplied twice.z	WARNING! z/ is not default parameter.
                    zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.zParameters za should be specified explicitly. Instead they were passed in as part of `model_kwargs` parameter.)	r   r2   list
ValueErrorloggerwarningpopintersectionkeys)clsrZ   Zall_required_field_namesextra
field_nameZinvalid_model_kwargsr+   r+   r,   build_extra   s*    
zChatHunyuan.build_extrac                 C   s8   t |dd|d< t |dd|d< tt |dd|d< |S )NrG   rD   rH   rE   rI   rF   )r   r   )rb   rZ   r+   r+   r,   validate_environment   s$    z ChatHunyuan.validate_environmentc                 C   s,   | j | j| j| j| j| jd}i || jS )z3Get the default parameters for calling Hunyuan API.)ZTemperatureZTopPZModelZStreamZStreamModerationZEnableEnhancement)rQ   rR   rS   rN   rT   rU   rW   )rK   Znormal_paramsr+   r+   r,   _default_params   s    zChatHunyuan._default_params)messagesstoprun_managerkwargsr!   c                 K   sJ   | j r&| jf |||d|}t|S | j|fi |}tt| S )N)rh   ri   rj   )rN   _streamr   _chatrB   jsonloadsZto_json_string)rK   rh   ri   rj   rk   Zstream_iterresr+   r+   r,   	_generate   s    zChatHunyuan._generatec                 k   s   | j |fi |}t}|D ]}|dd}t|dkr8qt|}d|v rXtd| |d D ]J}	t|	d |}|dd|_|j	}t
|d	}
|r|j|j|
d
 |
V  q`qd S )Ndatar/   r   errorz!Error from Hunyuan api response: r7   Deltar9   r:   )chunk)rm   r   r2   lenrn   ro   r\   r5   r?   	__class__r   Zon_llm_new_tokenr)   )rK   rh   ri   rj   rk   rp   Zdefault_chunk_classru   r6   rA   Zcg_chunkr+   r+   r,   rl      s&    

zChatHunyuan._stream)rh   rk   r!   c                 K   s   | j d u rtdz ddlm} ddlm}m} W n tyL   tdY n0 i | j|}|	| j
t| j  }||d}| }	ddd	 |D i|}
|	t|
 ||	}|S )
NzHunyuan secret key is not set.r   )
credential)hunyuan_clientmodelszkCould not import tencentcloud python package. Please install it with `pip install tencentcloud-sdk-python`.r/   ZMessagesc                 S   s   g | ]}t |qS r+   )r-   ).0mr+   r+   r,   
<listcomp>      z%ChatHunyuan._chat.<locals>.<listcomp>)rI   r\   Ztencentcloud.commonrx   Ztencentcloud.hunyuan.v20230901ry   rz   ImportErrorrg   Z
CredentialrH   strZget_secret_valueZHunyuanClientZChatCompletionsRequestZfrom_json_stringrn   dumpsZChatCompletions)rK   rh   rk   rx   ry   rz   
parameterscredclientreqparamsrespr+   r+   r,   rm      s,    


zChatHunyuan._chatc                 C   s   dS )Nzhunyuan-chatr+   rJ   r+   r+   r,   	_llm_type  s    zChatHunyuan._llm_type)NN)NN)/__name__
__module____qualname____doc__propertyr   r   rL   boolrM   rG   r   int__annotations__rH   rI   r   rN   rP   rQ   floatrR   rS   rT   rU   r   dictrW   r   r   Zmodel_configr   classmethodre   r   rf   rg   r   r   r	   r   rq   r   r   rl   rm   r   r+   r+   r+   r,   rC   Y   s^   
  
  
rC   )1rn   loggingtypingr   r   r   r   r   r   r   Zlangchain_core.callbacksr	   Z*langchain_core.language_models.chat_modelsr
   r   Zlangchain_core.messagesr   r   r   r   r   r   r   r   r   Zlangchain_core.outputsr   r   r   Zlangchain_core.utilsr   r   r   r   Zpydanticr   r   r   r   	getLoggerr   r]   r   r-   r   r3   r5   rB   rC   r+   r+   r+   r,   <module>   s    $,
