o
    Zh.                     @   s  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 d dlmZmZ d dlmZ d dlmZ d dlmZmZmZ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% d d	l&m'Z' d d
l(m)Z)m*Z*m+Z+ d dl,m-Z- dedee defddZ.dedefddZ/eddddG dd deZ0de1deegef deegef fddZ2ddde0d e1d!ee d"edef
d#d$Z3ddde0d e1d!ee d"edef
d%d&Z4ddde0d e1d!ee d"edef
d'd(Z5	d+de0d!ee	eef  deegef fd)d*Z6dS ),    )	AnyAsyncIteratorCallableDictIteratorListOptionalTypeUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseChatModel)create_base_retry_decorator)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageFunctionMessageChunkHumanMessageHumanMessageChunkSystemMessageSystemMessageChunk)ChatGenerationChatGenerationChunk
ChatResult)convert_to_secret_str)get_from_dict_or_env)Field	SecretStrmodel_validatorconvert_message_to_dict_dictdefault_classreturnc                 C   s   | j }| jpd}i }|dks|tkrt|dS |dks|tkr%t||dS |dks-|tkr2t|dS |dks:|tkrAt|| jdS |sG|tkrMt||d	S ||dS )
z,Convert a delta response to a message chunk. usercontent	assistantr,   additional_kwargssystemfunctionr,   namer,   role)r5   r,   r   r   r   r   r3   r   )r&   r'   r5   r,   r/    r6   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/chat_models/fireworks.py_convert_delta_to_message_chunk*   s   



r8   c                 C   st   | j }| jpd}|dkrt|dS |dkr | j}i }t||dS |dkr)t|dS |dkr4t|| jdS t||d	S )
z%Convert a dict response to a message.r)   r*   r+   r-   r.   r0   r1   r2   r4   )r5   r,   r   r   r   r   r3   r   )r&   r5   r,   r/   r6   r6   r7   convert_dict_to_message@   s   


r9   z0.0.26z1.0z!langchain_fireworks.ChatFireworks)ZsinceZremovalZalternative_importc                   @   s  e Zd ZU dZdZeed< edd dZe	ed< dZ
ee ed	< d
Zeed< dZeed< edeeef fddZedefddZedee fddZeddededefddZedefddZ		d0dee deee  dee dedef
d d!Z		d0dee deee  dee  dedef
d"d#Z!d$eee	  de	fd%d&Z"d'edefd(d)Z#dee deeeef  fd*d+Z$		d0dee deee  dee dede%e& f
d,d-Z'		d0dee deee  dee  dede(e& f
d.d/Z)dS )1ChatFireworkszFireworks Chat models.z*accounts/fireworks/models/llama-v2-7b-chatmodelc                   C   s   dddd  S )Ngffffff?i      )ZtemperatureZ
max_tokensZtop_p)copyr6   r6   r6   r7   <lambda>\   s
   zChatFireworks.<lambda>)default_factorymodel_kwargsNfireworks_api_key   max_retriesT	use_retryr(   c                 C   s   ddiS )NrA   FIREWORKS_API_KEYr6   selfr6   r6   r7   
lc_secretsf   s   zChatFireworks.lc_secretsc                 C      dS )NTr6   clsr6   r6   r7   is_lc_serializablej   s   z ChatFireworks.is_lc_serializablec                 C   s   g dS )z*Get the namespace of the langchain object.)Z	langchainZchat_models	fireworksr6   rJ   r6   r6   r7   get_lc_namespacen   s   zChatFireworks.get_lc_namespacebefore)modevaluesc              
   C   sP   zddl }W n ty } ztd|d}~ww tt|dd}| |j_|S )z%Validate that api key in environment.r   Nz`Could not import fireworks-ai python package. Please install it with `pip install fireworks-ai`.rA   rE   )fireworks.clientImportErrorr   r    Zget_secret_valueclientZapi_key)rK   rQ   rM   erA   r6   r6   r7   validate_environments   s   
z"ChatFireworks.validate_environmentc                 C   rI   )zReturn type of llm.zfireworks-chatr6   rF   r6   r6   r7   	_llm_type   s   zChatFireworks._llm_typemessagesstoprun_managerkwargsc                 K   sD   |  |}| j|d| j|}t| | jf||d|}| |S N)r;   rX   rZ   rY   )_create_message_dictsr;   r@   completion_with_retryrD   _create_chat_resultrG   rX   rY   rZ   r[   message_dictsparamsresponser6   r6   r7   	_generate   s$   

zChatFireworks._generatec                    sL   |  |}| j|d| j|}t| | jf||d|I d H }| |S r\   )r^   r;   r@   acompletion_with_retryrD   r`   ra   r6   r6   r7   
_agenerate   s"   

zChatFireworks._ageneratellm_outputsc                 C   s   |d d u ri S |d S )Nr   r6   )rG   rh   r6   r6   r7   _combine_llm_outputs   s   z"ChatFireworks._combine_llm_outputsrd   c                 C   sN   g }|j D ]}t|j}t|t|jdd}|| qd| ji}t||dS )Nfinish_reasonmessagegeneration_infor;   )generations
llm_output)	choicesr9   rm   r   dictrk   appendr;   r   )rG   rd   ro   resrm   genrp   r6   r6   r7   r`      s   



z!ChatFireworks._create_chat_resultc                 C   s   dd |D }|S )Nc                 S   s   g | ]}t |qS r6   r$   ).0mr6   r6   r7   
<listcomp>   s    z7ChatFireworks._create_message_dicts.<locals>.<listcomp>r6   )rG   rX   rb   r6   r6   r7   r^      s   z#ChatFireworks._create_message_dictsc                 k   s    |  |}t}| j|dd| j|}t| | jf||d|D ]1}|jd }	t|	j|}|	j	}
|
d ur:t
|
dnd }|j}t||d}|rO|j|j|d |V  q!d S )NTr;   rX   streamr]   r   rj   rl   )chunk)r^   r   r;   r@   r_   rD   rq   r8   deltark   rr   	__class__r   on_llm_new_tokentextrG   rX   rY   rZ   r[   rb   Zdefault_chunk_classrc   r{   choicerk   rn   Zcg_chunkr6   r6   r7   _stream   s>   


zChatFireworks._streamc                 K  s   |  |}t}| j|dd| j|}t| | jf||d|I d H 2 z83 d H W }|jd }	t|	j|}|	j	}
|
d urAt
|
dnd }|j}t||d}|rY|j|j|dI d H  |V  q$6 d S )NTry   r]   r   rj   rl   )tokenr{   )r^   r   r;   r@    acompletion_with_retry_streamingrD   rq   r8   r|   rk   rr   r}   r   r~   r   r   r6   r6   r7   _astream   s>   

zChatFireworks._astream)NN)*__name__
__module____qualname____doc__r;   str__annotations__r!   r@   rr   rA   r   r"   rC   intrD   boolpropertyr   rH   classmethodrL   r   rN   r#   r   rV   rW   r   r   r   re   r   rg   ri   r`   r^   r   r   r   r   r   r6   r6   r6   r7   r:   R   s   
 




	

$
r:   	condition	decoratorc                    s0   dt tgtf dt tgtf f fdd}|S )zDefine conditional decorator.

    Args:
        condition: The condition.
        decorator: The decorator.

    Returns:
        The decorated function.
    funcr(   c                    s    r| S | S Nr6   )r   r   r   r6   r7   actual_decorator  s   z/conditional_decorator.<locals>.actual_decorator)r   r   )r   r   r   r6   r   r7   conditional_decorator  s   ,r   NrZ   llmrD   rZ   r[   c                   sB   ddl  t| |d}t||dtdtf fdd}|di |S )	*Use tenacity to retry the completion call.r   Nr   r[   r(   c                     s    j jjdi | S )r   Nr6   )rT   ChatCompletioncreater[   rM   r6   r7   _completion_with_retry/  s   z5completion_with_retry.<locals>._completion_with_retryr6   rR   _create_retry_decoratorr   r   r   rD   rZ   r[   Zretry_decoratorr   r6   r   r7   r_   #  s
   r_   c                   J   ddl  t| |d}t||dtdtf fdd}|di |I dH S )	z0Use tenacity to retry the async completion call.r   Nr   r[   r(   c                     s    j jjdi | I d H S Nr6   rT   r   Zacreater   r   r6   r7   r   E  s   z6acompletion_with_retry.<locals>._completion_with_retryr6   r   r   r6   r   r7   rf   9     rf   c                   r   )	z8Use tenacity to retry the completion call for streaming.r   Nr   r[   r(   c                     s    j jjdi | S r   r   r   r   r6   r7   r   Z  s   z@acompletion_with_retry_streaming.<locals>._completion_with_retryr6   r   r   r6   r   r7   r   N  r   r   c                 C   s<   ddl }|jjj|jjj|jjj|jjjg}t|| j|dS )zDefine retry mechanism.r   N)Zerror_typesrC   rZ   )	rR   rT   errorZRateLimitErrorZInternalServerErrorZBadGatewayErrorZServiceUnavailableErrorr   rC   )r   rZ   rM   errorsr6   r6   r7   r   c  s   r   r   )7typingr   r   r   r   r   r   r   r	   r
   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   r   Z*langchain_core.language_models.chat_modelsr   Z#langchain_core.language_models.llmsr   Zlangchain_core.messagesr   r   r   r   r   r   r   r   r   r   r   r   Zlangchain_core.outputsr   r   r   Zlangchain_core.utilsr   Zlangchain_core.utils.envr    Zpydanticr!   r"   r#   Z#langchain_community.adapters.openair%   r8   r9   r:   r   r   r_   rf   r   r   r6   r6   r6   r7   <module>   s   , 8
 8




