a
    bg.                     @   s  d dl 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 d dlmZ d dlmZmZ d dlmZmZ d dlmZmZmZ d dlmZmZ d d	lmZ d d
lmZmZ eedddZ eddddG dd deZ!e"eegef eegef dddZ#dde!e"e
e eedddZ$dde!e"e
e eedddZ%dde!e"e
e eedddZ&dde!e"e
e eeddd Z'dde!e"e
e eedd!d"Z(dde!e
eeef  eegef d#d$d%Z)dS )&    N)ThreadPoolExecutor)AnyAsyncIteratorCallableDictIteratorListOptionalUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLLMcreate_base_retry_decorator)
GenerationGenerationChunk	LLMResult)convert_to_secret_strpre_init)get_from_dict_or_env)Field	SecretStr)stream_responsereturnc                 C   s,   t | jd jt| jd j| jd jddS )z0Convert a stream response to a generation chunk.r   )finish_reasonlogprobs)textZgeneration_info)r   choicesr   dictr   r   )r    r   p/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/fireworks.py$_stream_response_to_generation_chunk   s    


r!   z0.0.26z1.0zlangchain_fireworks.Fireworks)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< dZeed< eeeef dddZeedddZeee dddZeeedddZee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dddZ!ee eee  d d!d"Z"eee e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% d&d)d*Z(dS )/	FireworkszFireworks 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)copyr   r   r   r    <lambda>(   s    zFireworks.<lambda>)default_factorymodel_kwargsNfireworks_api_key   max_retries
batch_sizeT	use_retryr   c                 C   s   ddiS )Nr)   FIREWORKS_API_KEYr   selfr   r   r    
lc_secrets3   s    zFireworks.lc_secretsc                 C   s   dS )NTr   clsr   r   r    is_lc_serializable7   s    zFireworks.is_lc_serializablec                 C   s   g dS )z*Get the namespace of the langchain object.)Z	langchainZllms	fireworksr   r3   r   r   r    get_lc_namespace;   s    zFireworks.get_lc_namespace)valuesr   c              
   C   s\   zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 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`.r)   r/   )fireworks.clientImportErrorr   r   Zget_secret_valueclientZapi_key)r4   r8   r6   er)   r   r   r    validate_environment@   s    
zFireworks.validate_environmentc                 C   s   dS )zReturn type of llm.r6   r   r0   r   r   r    	_llm_typeP   s    zFireworks._llm_type)promptsstoprun_managerkwargsr   c           
      K   sZ   d| j i| j}| |}g }|D ]*}t| | jf|||d|}	||	 q"| ||S )zCall out to Fireworks endpoint with k unique prompts.
        Args:
            prompts: The prompts to pass into the model.
            stop: Optional list of stop words to use when generating.
        Returns:
            The full LLM output.
        r#   promptrA   r@   )r#   r(   get_batch_promptscompletion_with_retry_batchingr-   extendcreate_llm_result
r1   r?   r@   rA   rB   paramssub_promptsr   Z_promptsresponser   r   r    	_generateU   s&    
zFireworks._generatec           
         s`   d| j i| j}| |}g }|D ]0}t| | jf|||d|I dH }	||	 q"| ||S )z;Call out to Fireworks endpoint async with k unique prompts.r#   rC   N)r#   r(   rE   acompletion_with_retry_batchingr-   rG   rH   rI   r   r   r    
_ageneratev   s&    	
zFireworks._agenerate)r?   r   c                    s&    fddt dt jD }|S )z!Get the sub prompts for llm call.c                    s   g | ]} ||j   qS r   )r,   ).0ir?   r1   r   r    
<listcomp>   s   z/Fireworks.get_batch_prompts.<locals>.<listcomp>r   )rangelenr,   )r1   r?   rK   r   rR   r    rE      s    zFireworks.get_batch_prompts)r   r?   r   c                 C   sP   g }t |D ],\}}|||d  }|dd |D  qd| ji}t||dS )z2Create the LLMResult from the choices and prompts.r$   c                 S   s"   g | ]}t |jd  d jdqS )r   r   )r   )r   __dict__r   )rP   choicer   r   r    rS      s   z/Fireworks.create_llm_result.<locals>.<listcomp>r#   )generations
llm_output)	enumerateappendr#   r   )r1   r   r?   rX   rQ   _Zsub_choicesrY   r   r   r    rH      s    
zFireworks.create_llm_result)rD   r@   rA   rB   r   c                 k   sZ   | j |dd| j}t| | jf||d|D ]&}t|}|rN|j|j|d |V  q.d S NT)r#   rD   stream)rA   r@   )chunk)r#   r(   completion_with_retryr-   r!   on_llm_new_tokenr   r1   rD   r@   rA   rB   rJ   Zstream_respr_   r   r   r    _stream   s"    
zFireworks._streamc                 K  sp   | j |dd| j}t| | jf||d|I d H 2 z43 d H W }t|}|rb|j|j|dI d H  |V  q46 d S r]   )r#   r(    acompletion_with_retry_streamingr-   r!   ra   r   rb   r   r   r    _astream   s"    zFireworks._astream)NN)NN)NN)NN))__name__
__module____qualname____doc__r#   str__annotations__r   r(   r   r)   r	   r   r+   intr,   r-   boolpropertyr   r2   classmethodr5   r   r7   r   r=   r>   r   r   r   rM   r   rO   rE   rH   r   r   rc   r   re   r   r   r   r    r"      sv   
  
$  

  
  
r"   )	condition	decoratorr   c                    s.   t tgtf t tgtf d fdd}|S )zConditionally apply a decorator.

    Args:
        condition: A boolean indicating whether to apply the decorator.
        decorator: A decorator function.

    Returns:
        A decorator function.
    )funcr   c                    s    r| S | S Nr   )rr   rp   rq   r   r    actual_decorator   s    z/conditional_decorator.<locals>.actual_decorator)r   r   )rp   rq   ru   r   rt   r    conditional_decorator   s    *rv   rA   )llmr-   rA   rB   r   c                   s@   ddl  t| |d}t||ttd fdd}|f i |S )*Use tenacity to retry the completion call.r   Nrw   rB   r   c                     s    j jjf i | S rs   r;   
CompletioncreaterB   r6   r   r    _completion_with_retry   s    z5completion_with_retry.<locals>._completion_with_retryr9   _create_retry_decoratorrv   r   rx   r-   rA   rB   retry_decoratorr   r   r   r    r`      s
    r`   c                   sF   ddl  t| |d}t||ttd fdd}|f i |I dH S )ry   r   Nrw   rz   c                     s    j jjf i | I d H S rs   r;   r|   Zacreater~   r   r   r    r     s    z6acompletion_with_retry.<locals>._completion_with_retryr   r   r   r   r    acompletion_with_retry  s
    r   c                   s\   ddl d d= t| |d}t||ttdfdd td fd	d
}| S )ry   r   NrD   rw   rD   r   c                    s    j jjf i d| iS NrD   r{   rD   r6   rB   r   r    r   %  s    z>completion_with_retry_batching.<locals>._completion_with_retryr.   c                     s:   t   } t|  }W d    n1 s,0    Y  |S rs   )r   listmapexecutorresults)r   rD   r   r    batch_sync_run)  s    .z6completion_with_retry_batching.<locals>.batch_sync_run)r9   r   rv   rj   r   r   rx   r-   rA   rB   r   r   r   )r   r6   rB   rD   r    rF     s    rF   c                   sx   ddl d d= t| |d}t||ttdfdd ttttdd	d
td fdd}| I dH S )ry   r   NrD   rw   r   c                    s"    j jjf i d| iI d H S r   r   r   r   r   r    r   @  s    z?acompletion_with_retry_batching.<locals>._completion_with_retry)coroutine_funcargsrB   r   c                 _   s@   t  }z(t | || |i |W |  S |  0 d S rs   )asyncionew_event_loopset_event_looprun_until_completeclose)r   r   rB   Znew_loopr   r   r    run_coroutine_in_new_loopD  s    
zBacompletion_with_retry_batching.<locals>.run_coroutine_in_new_loopr.   c                     sF   t  ,} t|  gt }W d    n1 s80    Y  |S rs   )r   r   r   rU   r   )r   rD   r   r   r    r   N  s    "z7acompletion_with_retry_batching.<locals>.batch_sync_run)r9   r   rv   rj   r   r   r   r   r   )r   r6   rB   rD   r   r    rN   1  s    
rN   c                   sF   ddl  t| |d}t||ttd fdd}|f i |I dH S )z8Use tenacity to retry the completion call for streaming.r   Nrw   rz   c                     s    j jjf i | S rs   r   r~   r   r   r    r   h  s    z@acompletion_with_retry_streaming.<locals>._completion_with_retryr   r   r   r   r    rd   \  s
    rd   )rx   rA   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_typesr+   rA   )	r9   r;   errorZRateLimitErrorZInternalServerErrorZBadGatewayErrorZServiceUnavailableErrorr   r+   )rx   rA   r6   errorsr   r   r    r   q  s    r   )*r   concurrent.futuresr   typingr   r   r   r   r   r   r	   r
   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   r   Z#langchain_core.language_models.llmsr   r   Zlangchain_core.outputsr   r   r   Zlangchain_core.utilsr   r   Zlangchain_core.utils.envr   Zpydanticr   r   r!   r"   rm   rv   r`   r   rF   rN   rd   r   r   r   r   r    <module>   s   ( 6/
