
    Ng.                     h   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Z  eddd           G d de                      Z!de"deegef         deegef         fdZ#ddde!de"de
e         dedef
dZ$ddde!de"de
e         dedef
dZ%ddde!de"de
e         dedef
dZ&ddde!de"de
e         dedef
dZ'ddde!de"de
e         dedef
d Z(ddde!de
eeef                  deegef         f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                     t          | j        d         j        t          | j        d         j        | j        d         j                            S )z0Convert a stream response to a generation chunk.r   )finish_reasonlogprobs)textgeneration_info)r   choicesr   dictr   r   )r   s    ^/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/fireworks.py$_stream_response_to_generation_chunkr#      sW     $Q',)1!4B$,Q/8
 
 
       z0.0.26z1.0zlangchain_fireworks.Fireworks)sinceremovalalternative_importc                      e Zd ZU dZdZeed<    e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            Zedefd            Zedee         fd            Zededefd            Zedefd            Z	 	 d!dee         deee                  dee         dedef
dZ	 	 d!dee         deee                  dee          dedef
dZ!dee         deee                  fdZ"dedee         defdZ#	 	 d!dedeee                  dee         dede$e%         f
dZ&	 	 d!dedeee                  dee          dede'e%         f
d Z(dS )"	FireworkszFireworks models.z*accounts/fireworks/models/llama-v2-7b-chatmodelc                  2    dddd                                 S )Ngffffff?i      )temperature
max_tokenstop_p)copy r$   r"   <lambda>zFireworks.<lambda>(   s%    !
 !
 $&&	 r$   )default_factorymodel_kwargsNfireworks_api_key   max_retries
batch_sizeT	use_retryr   c                 
    ddiS )Nr5   FIREWORKS_API_KEYr1   selfs    r"   
lc_secretszFireworks.lc_secrets3   s    #%899r$   c                     dS )NTr1   clss    r"   is_lc_serializablezFireworks.is_lc_serializable7   s    tr$   c                 
    g dS )z*Get the namespace of the langchain object.)	langchainllms	fireworksr1   r@   s    r"   get_lc_namespacezFireworks.get_lc_namespace;   s     2111r$   valuesc                     	 ddl }n"# t          $ r}t          d          |d}~ww xY 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`.r5   r;   )fireworks.clientImportErrorr   r   get_secret_valueclientapi_key)rA   rH   rF   er5   s        r"   validate_environmentzFireworks.validate_environment@   s    	##### 	 	 	E  	
 2 )<>QRR
 
 $5#E#E#G#G	 s    
&!&c                     dS )zReturn type of llm.rF   r1   r<   s    r"   	_llm_typezFireworks._llm_typeP   s	     {r$   promptsstoprun_managerkwargsc                     d| j         i| j        }|                     |          }g }|D ].}t          | | j        f|||d|}	|                    |	           /|                     ||          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*   promptrU   rT   )r*   r4   get_batch_promptscompletion_with_retry_batchingr9   extendcreate_llm_result
r=   rS   rT   rU   rV   paramssub_promptsr    _promptsresponses
             r"   	_generatezFireworks._generateU   s     TZ

 ,,W55# 		% 		%H5  '   H NN8$$$$%%gw777r$   c                    K   d| j         i| j        }|                     |          }g }|D ]4}t          | | j        f|||d| d{V }	|                    |	           5|                     ||          S )z;Call out to Fireworks endpoint async with k unique prompts.r*   rX   N)r*   r4   rZ   acompletion_with_retry_batchingr9   r\   r]   r^   s
             r"   
_ageneratezFireworks._ageneratev   s       TZ

 ,,W55# 		% 		%H<  '         H NN8$$$$%%gw777r$   c                 j      fdt          dt                     j                  D             }|S )z!Get the sub prompts for llm call.c                 4    g | ]}||j         z            S r1   )r8   ).0irS   r=   s     r"   
<listcomp>z/Fireworks.get_batch_prompts.<locals>.<listcomp>   s:     
 
 
 ADO++,
 
 
r$   r   )rangelenr8   )r=   rS   r`   s   `` r"   rZ   zFireworks.get_batch_prompts   sM    

 
 
 
 
1c'llDO<<
 
 
 r$   r    c                     g }t          |          D ]1\  }}|||dz            }|                    d |D                        2d| j        i}t          ||          S )z2Create the LLMResult from the choices and prompts.r,   c                 Z    g | ](}t          |j        d          d         j                  )S )r    r   )r   )r   __dict__r   )ri   choices     r"   rk   z/Fireworks.create_llm_result.<locals>.<listcomp>   sK         #_Y7:?    r$   r*   )generations
llm_output)	enumerateappendr*   r   )r=   r    rS   rr   rj   _sub_choicesrs   s           r"   r]   zFireworks.create_llm_result   s    g&& 		 		DAq!!q1u+.K  #.	      tz*
[ZHHHHr$   rY   c              +      K   | j         |dd| j        }t          | | j        f||d|D ]3}t	          |          }|r|                    |j        |           |V  4d S NT)r*   rY   stream)rU   rT   )chunk)r*   r4   completion_with_retryr9   r#   on_llm_new_tokenr   r=   rY   rT   rU   rV   r_   stream_respr{   s           r"   _streamzFireworks._stream   s       Z
 
 	
 1$.
.9
 
HN
 
 	 	K 9EEE F,,UZu,EEEKKKK	 	r$   c                   K   | j         |dd| j        }t          | | j        f||d| d {V 2 3 d {V }t	          |          }|r"|                    |j        |           d {V  |W V  @6 d S ry   )r*   r4    acompletion_with_retry_streamingr9   r#   r}   r   r~   s           r"   _astreamzFireworks._astream   s       Z
 
 	
 (H$.(
.9(
 (
HN(
 (
 "
 "
 "
 "
 "
 "
 	 	 	 	 	 	 	+ 9EEE L!225:U2KKKKKKKKKKKKKK"
 "
 "
s   A1)NN))__name__
__module____qualname____doc__r*   str__annotations__r   r4   r!   r5   r
   r   r7   intr8   r9   boolpropertyr   r>   classmethodrB   r	   rG   r   rP   rR   r   r   r   rc   r   rf   rZ   r]   r   r   r   r   r   r1   r$   r"   r)   r)      st         =E3===   L$    .2x	*111KJIt:DcN : : : X: 4    [ 2c 2 2 2 [2 $ 4    X 3    X %):>	8 8c8 tCy!8 67	8
 8 
8 8 8 8H %)?C	8 8c8 tCy!8 ;<	8
 8 
8 8 8 86	c	 
d3i	 	 	 	I ItCy IY I I I I& %):>	  tCy! 67	
  
/	"   0 %)?C	  tCy! ;<	
  
	'     r$   r)   	condition	decoratorc                      dt           t          gt          f         dt           t          gt          f         f f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                 "    r |           S | S Nr1   )r   r   r   s    r"   actual_decoratorz/conditional_decorator.<locals>.actual_decorator   s     	#9T??"r$   )r   r   )r   r   r   s   `` r"   conditional_decoratorr      sS    xs
3 #8L       
 r$   rU   llmr9   rU   rV   c                    ddl t          | |          }t          ||          dt          dt          ffd            } |di |S )*Use tenacity to retry the completion call.r   Nr   rV   r   c                  2     j         j        j        di | S Nr1   rM   
CompletioncreaterV   rF   s    r"   _completion_with_retryz5completion_with_retry.<locals>._completion_with_retry   s-    1y*1 
 

 
 	
r$   r1   rJ   _create_retry_decoratorr   r   r   r9   rU   rV   retry_decoratorr   rF   s         @r"   r|   r|      s}     -c{KKKO9o66
 
 
 
 
 
 
 76

 "!++F+++r$   c                   K   ddl t          | |          }t          ||          dt          dt          ffd            } |di | d{V S )r   r   Nr   rV   r   c                  B   K    j         j        j        di |  d {V S r   rM   r   acreater   s    r"   r   z6acompletion_with_retry.<locals>._completion_with_retry  sO      8Y%08 
 

 
 
 
 
 
 
 
 	
r$   r1   r   r   s         @r"   acompletion_with_retryr            -c{KKKO9o66
s 
s 
 
 
 
 
 76

 ('11&111111111r$   c                    ddl d         d= t          | |          }t          ||          dt          dt          ffd            dt
          ffd} |            S )r   r   NrY   r   r   c                 :     j         j        j        di d| iS NrY   r1   r   rY   rF   rV   s    r"   r   z>completion_with_retry_batching.<locals>._completion_with_retry%  s-    1y*1JJFJJ6JJJJr$   c                      t                      5 } t          |                                         }d d d            n# 1 swxY w Y   |S r   )r   listmap)executorresultsr   rY   s     r"   batch_sync_runz6completion_with_retry_batching.<locals>.batch_sync_run)  s    !! 	IX8<<(>GGHHG	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   $A  AA)rJ   r   r   r   r   r	   )	r   r9   rU   rV   r   r   r   rF   rY   s	      `  @@@r"   r[   r[     s     HFx-c{KKKO9o66Ks Ks K K K K K K 76KD       
 >r$   c                $  	K   ddl d         d= t          | |          }t          ||          dt          dt          ffd            dt          dt
          d	t
          dt          fd
	dt          f	fd} |             d{V S )r   r   NrY   r   r   c                 J   K    j         j        j        di d| i d {V S r   r   r   s    r"   r   z?acompletion_with_retry_batching.<locals>._completion_with_retry@  sC      8Y%08QQ6QQ&QQQQQQQQQQr$   coroutine_funcargsrV   c                     t          j                    }	 t          j        |           |                     | |i |          |                                 S # |                                 w xY wr   )asyncionew_event_loopset_event_looprun_until_completeclose)r   r   rV   new_loops       r"   run_coroutine_in_new_loopzBacompletion_with_retry_batching.<locals>.run_coroutine_in_new_loopD  sq     )++	"8,,,..~~t/Nv/N/NOONNHNNs   .A A.c            
         K   t                      5 } t          |                     gt                    z                      }d d d            n# 1 swxY w Y   |S r   )r   r   r   rm   )r   r   r   rY   r   s     r"   r   z7acompletion_with_retry_batching.<locals>.batch_sync_runN  s      !! 	X-+,s6{{:  G	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   6AAA)rJ   r   r   r   r   r   r	   )
r   r9   rU   rV   r   r   r   rF   rY   r   s
      `  @@@@r"   re   re   1  s      HFx-c{KKKO9o66RS RS R R R R R R 76R$(48	   	$ 	 	 	 	 	 	 	 	  !!!!!!!!!r$   c                   K   ddl t          | |          }t          ||          dt          dt          ffd            } |di | d{V S )z8Use tenacity to retry the completion call for streaming.r   Nr   rV   r   c                  6   K    j         j        j        di | S r   r   r   s    r"   r   z@acompletion_with_retry_streaming.<locals>._completion_with_retryh  s1      2y*2 
 

 
 	
r$   r1   r   r   s         @r"   r   r   \  r   r$   c                    ddl }|j        j        j        |j        j        j        |j        j        j        |j        j        j        g}t          || j        |          S )zDefine retry mechanism.r   N)error_typesr7   rU   )	rJ   rM   errorRateLimitErrorInternalServerErrorBadGatewayErrorServiceUnavailableErrorr   r7   )r   rU   rF   errorss       r"   r   r   q  sk      	-2.6	F '[   r$   )*r   concurrent.futuresr   typingr   r   r   r   r   r	   r
   r   langchain_core._api.deprecationr   langchain_core.callbacksr   r   #langchain_core.language_models.llmsr   r   langchain_core.outputsr   r   r   langchain_core.utilsr   r   langchain_core.utils.envr   pydanticr   r   r#   r)   r   r   r|   r   r[   re   r   r   r1   r$   r"   <module>r      s    1 1 1 1 1 1 V V V V V V V V V V V V V V V V V V V V 6 6 6 6 6 6        U T T T T T T T I I I I I I I I I I @ @ @ @ @ @ @ @ 9 9 9 9 9 9 % % % % % % % %


 
 
 
 
6  
q q q q q q q 
qh (# 4seSj   2 7;	, , ,	,, 23	,
 , 	, , , ,2 <@	2 2 2	22 78	2
 2 	2 2 2 22 7;	  	 23	
  	   > <@	(" (" ("	("(" 78	("
 (" 	(" (" (" ("^ <@	2 2 2	22 78	2
 2 	2 2 2 24 	  	 +-EEF seSj     r$   