o
    Zh.                     @   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 dedefddZ eddddG dd deZ!de"deegef deegef fddZ#ddde!de"de
e dedef
ddZ$ddde!de"de
e dedef
d d!Z%ddde!de"de
e dedef
d"d#Z&ddde!de"de
e dedef
d$d%Z'ddde!de"de
e dedef
d&d'Z(ddde!de
eeef  deegef f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   Y/var/www/html/lang_env/lib/python3.10/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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edefddZedefddZ		d-dee deee  dee dedef
dd Z		d-dee deee  dee  dedef
d!d"Z!dee deee  fd#d$Z"d%edee defd&d'Z#		d-d(edeee  dee dede$e% f
d)d*Z&		d-d(edeee  dee  dede'e% f
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_retryr   c                 C   s   ddiS )Nr)   FIREWORKS_API_KEYr   selfr   r   r    
lc_secrets3   s   zFireworks.lc_secretsc                 C      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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`.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   r2   )zReturn type of llm.r6   r   r/   r   r   r    	_llm_typeP   s   zFireworks._llm_typepromptsstoprun_managerkwargsc           
      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
r0   r?   r@   rA   rB   paramssub_promptsr   Z_promptsresponser   r   r    	_generateU   s&   
zFireworks._generatec           
         sb   d| j i| j}| |}g }|D ]}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._ageneratec                    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?   r0   r   r    
<listcomp>   s    z/Fireworks.get_batch_prompts.<locals>.<listcomp>r   )rangelenr,   )r0   r?   rK   r   rR   r    rE      s   zFireworks.get_batch_promptsr   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   )r0   r   r?   rX   rQ   _Zsub_choicesrY   r   r   r    rH      s   
zFireworks.create_llm_resultrD   c                 k   s\    | j |dd| j}t| | jf||d|D ]}t|}|r(|j|j|d |V  qd S NT)r#   rD   stream)rA   r@   )chunk)r#   r(   completion_with_retryr-   r!   on_llm_new_tokenr   r0   rD   r@   rA   rB   rJ   Zstream_respr_   r   r   r    _stream   s&   
zFireworks._streamc                 K  sr   | j |dd| j}t| | jf||d|I d H 2 z3 d H W }t|}|r2|j|j|dI d H  |V  q6 d S r]   )r#   r(    acompletion_with_retry_streamingr-   r!   ra   r   rb   r   r   r    _astream   s&   zFireworks._astream)NN))__name__
__module____qualname____doc__r#   str__annotations__r   r(   r   r)   r	   r   r+   intr,   r-   boolpropertyr   r1   classmethodr5   r   r7   r   r=   r>   r   r   r   rM   r   rO   rE   rH   r   r   rc   r   re   r   r   r   r    r"      s   
 

$






r"   	condition	decoratorc                    s0   dt tgtf dt tgtf f 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   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   Nrw   rB   r   c                     s    j jjdi | S Nr   r;   
CompletioncreaterB   r6   r   r    _completion_with_retry   s   z5completion_with_retry.<locals>._completion_with_retryr   r9   _create_retry_decoratorrv   r   rx   r-   rA   rB   retry_decoratorr   r   r   r    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 )	ry   r   Nrw   rB   r   c                     s    j jjdi | I d H S rz   r;   r|   Zacreater~   r   r   r    r     s   z6acompletion_with_retry.<locals>._completion_with_retryr   r   r   r   r   r    acompletion_with_retry     r   c                   s^   ddl d d= t| |d}t||dtdtffdd dtf fdd	}| S )
ry   r   NrD   rw   r   c                    s    j jjdi d| iS NrD   r   r{   rD   r6   rB   r   r    r   %  s   z>completion_with_retry_batching.<locals>._completion_with_retryc                     s<   t  } t|  }W d    |S 1 sw   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                   s   ddl d d= t| |d}t||dtdtffdd dtd	td
tdtfdddtf fdd}| I dH S )ry   r   NrD   rw   r   c                    s$    j jjd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   c                 _   s<   t  }zt | || |i |W |  S |  w 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_loopc                     sJ   t  } t|  gt }W d    |S 1 sw   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                   r   )	z8Use tenacity to retry the completion call for streaming.r   Nrw   rB   r   c                     s    j jjdi | S rz   r   r~   r   r   r    r   h  s   z@acompletion_with_retry_streaming.<locals>._completion_with_retryr   r   r   r   r   r    rd   \  r   rd   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    (
 5




/

