
    @h<                    F   S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
  SSKJr  SSKJrJrJrJrJr  SSKJrJr  SS	KJr  SS
KJrJr  SSKJrJrJr  SSKJ r   SSK!J"r"J#r#  SSK$J%r%J&r&J'r'J(r(  SSK)J*r*  SSK+J,r,  SSK-J.r.J/r/  SSK0J1r1  \" SSSS9 " S S\15      5       r2SS jr3g)z2Chain that just formats a prompt and calls an LLM.    )annotationsN)Sequence)AnyOptionalUnioncast)
deprecated)AsyncCallbackManagerAsyncCallbackManagerForChainRunCallbackManagerCallbackManagerForChainRun	Callbacks)BaseLanguageModelLanguageModelInput)BaseMessage)BaseLLMOutputParserStrOutputParser)ChatGeneration
Generation	LLMResult)PromptValue)BasePromptTemplatePromptTemplate)RunnableRunnableBindingRunnableBranchRunnableWithFallbacks)DynamicRunnable)get_colored_text)
ConfigDictField)Chainz0.1.17z&RunnableSequence, e.g., `prompt | llm`z1.0)sincealternativeremovalc                  ~   \ rS rSr% Sr\S-S j5       rS\S'    S\S'    S	rS
\S'   \	" \
S9rS\S'    SrS\S'    \	" \S9rS\S'   \" SSS9r\S.S j5       r\S.S j5       r S/     S0S jjr S/     S1S jjr S/     S2S jjr S/     S3S jjr S/     S4S jjr S/     S5S jjr S/     S5S jjr\S6S j5       rS7S  jr S/     S8S! jjrS/S9S" jjrS/S9S# jjr S/     S:S$ jjr  S/     S;S% jjr! S/     S<S& jjr"    S=S' jr# S/     S<S( jjr$\S6S) j5       r%\S>S* j5       r&S?S+ jr'S,r(g)@LLMChain'   a  Chain to run queries against LLMs.

This class is deprecated. See below for an example implementation using
LangChain runnables:

    .. code-block:: python

        from langchain_core.output_parsers import StrOutputParser
        from langchain_core.prompts import PromptTemplate
        from langchain_openai import OpenAI

        prompt_template = "Tell me a {adjective} joke"
        prompt = PromptTemplate(
            input_variables=["adjective"], template=prompt_template
        )
        llm = OpenAI()
        chain = prompt | llm | StrOutputParser()

        chain.invoke("your adjective here")

Example:
    .. code-block:: python

        from langchain.chains import LLMChain
        from langchain_community.llms import OpenAI
        from langchain_core.prompts import PromptTemplate
        prompt_template = "Tell me a {adjective} joke"
        prompt = PromptTemplate(
            input_variables=["adjective"], template=prompt_template
        )
        llm = LLMChain(llm=OpenAI(), prompt=prompt)
boolc                    g)NT selfs    L/var/www/html/shao/venv/lib/python3.13/site-packages/langchain/chains/llm.pyis_lc_serializableLLMChain.is_lc_serializableN   s        r   promptzSUnion[Runnable[LanguageModelInput, str], Runnable[LanguageModelInput, BaseMessage]]llmtextstr
output_key)default_factoryr   output_parserTreturn_final_onlydict
llm_kwargsforbid)arbitrary_types_allowedextrac                .    U R                   R                  $ )z:Will be whatever keys the prompt expects.

:meta private:
)r2   input_variablesr,   s    r.   
input_keysLLMChain.input_keysh   s     {{***r1   c                Z    U R                   (       a  U R                  /$ U R                  S/$ )z-Will always return text key.

:meta private:
full_generation)r9   r6   r,   s    r.   output_keysLLMChain.output_keysp   s*     !!OO$$!233r1   Nc                L    U R                  U/US9nU R                  U5      S   $ Nrun_managerr   )generatecreate_outputsr-   inputsrJ   responses       r.   _callLLMChain._callz   s.    
 ==&{=C""8,Q//r1   c                2   U R                  XS9u  p4U(       a  UR                  5       OSn[        U R                  [        5      (       a*  U R                  R
                  " UU4SU0U R                  D6$ U R                  R                  " SSU0U R                  D6R                  [        [        U5      SU05      n/ nU HL  n[        U[        5      (       a  UR                  [        US9/5        M3  UR                  [        US9/5        MN     [        US9$ 	z Generate LLM result from inputs.rI   N	callbacksstop)message)r4   )generationsr+   )prep_prompts	get_child
isinstancer3   r   generate_promptr;   bindbatchr   listr   appendr   r   r   	r-   
input_listrJ   promptsrU   rT   resultsrW   ress	            r.   rK   LLMChain.generate   s    ))*)N/:K))+	dhh 12288++ $ //	  ((--=T=T__=CCw)$
 /1C#{++""N3$?#@A""JC$8#9:	 
 [11r1   c                x  #    U R                  XS9I Sh  vN u  p4U(       a  UR                  5       OSn[        U R                  [        5      (       a2  U R                  R
                  " UU4SU0U R                  D6I Sh  vN $ U R                  R                  " SSU0U R                  D6R                  [        [        U5      SU05      I Sh  vN n/ nU HL  n[        U[        5      (       a  UR                  [        US9/5        M3  UR                  [        US9/5        MN     [        US9$  GN N Nf7frS   )aprep_promptsrY   rZ   r3   r   agenerate_promptr;   r\   abatchr   r^   r   r_   r   r   r   r`   s	            r.   	agenerateLLMChain.agenerate   s&     #000UU/:K))+	dhh 12222 $ //	   C4C4??CJJw)$
 
 /1C#{++""N3$?#@A""JC$8#9:	 
 [11' V
s6   D:D3A+D:D6AD:D8A"D:6D:8D:c                   Sn[        U5      S:X  a  / U4$ SUS   ;   a  US   S   n/ nU H  nU R                  R                   Vs0 sH  ofXV   _M	     nnU R                  R                  " S	0 UD6n[	        UR                  5       S5      n	SU	-   n
U(       a  UR                  U
SU R                  S9  SU;   a  US   U:w  a  Sn[        U5      eUR                  U5        M     XC4$ s  snf 
zPrepare prompts from inputs.Nr   rU   greenzPrompt after formatting:

)endverbosez=If `stop` is present in any inputs, should be present in all.r+   
lenr2   r@   format_promptr   	to_stringon_textrq   
ValueErrorr_   r-   ra   rJ   rU   rb   rN   kselected_inputsr2   _colored_text_textmsgs               r.   rX   LLMChain.prep_prompts   s     z?at8OZ]"a=(D F59[[5P5PQ5P&)|5POQ[[..AAF,V-=-=-?IM0=@E##EtT\\#JF6Nd$:U o%NN6" ! } Rs   C&c                  #    Sn[        U5      S:X  a  / U4$ SUS   ;   a  US   S   n/ nU H  nU R                  R                   Vs0 sH  ofXV   _M	     nnU R                  R                  " S	0 UD6n[	        UR                  5       S5      n	SU	-   n
U(       a#  UR                  U
SU R                  S9I Sh  vN   SU;   a  US   U:w  a  Sn[        U5      eUR                  U5        M     XC4$ s  snf  N=7frm   rr   rx   s               r.   rg   LLMChain.aprep_prompts   s
     z?at8OZ]"a=(D F59[[5P5PQ5P&)|5POQ[[..AAF,V-=-=-?IM0=@E!))%T4<<)PPPF6Nd$:U o%NN6" ! } R
 Qs   AC7C0A"C77C58>C7c                P   [         R                  " UU R                  U R                  5      nUR	                  SSU0U R                  5       S9n U R                  XS9nU R                  U5      nUR                  SU05        U$ ! [         a  nUR                  U5        e SnAff = fz0Utilize the LLM generate method for speed gains.Nra   )namerI   outputs)r   	configurerT   rq   on_chain_startget_namerK   BaseExceptionon_chain_errorrL   on_chain_endr-   ra   rT   callback_managerrJ   rO   er   s           r.   applyLLMChain.apply   s     +44NNLL

 '55:& 6 

	}}Z}IH %%h/  )W!56  	&&q)	s   B 
B%B  B%c                  #    [         R                  " UU R                  U R                  5      nUR	                  SSU0U R                  5       S9I Sh  vN n U R                  XS9I Sh  vN nU R                  U5      nUR                  SU05      I Sh  vN   U$  NJ N4! [         a   nUR                  U5      I Sh  vN    e SnAff = f N77fr   )r
   r   rT   rq   r   r   rj   r   r   rL   r   r   s           r.   aapplyLLMChain.aapply  s      099NNLL

 -;;:& < 
 

	!^^J^PPH %%h/&&	7';<<<
 Q 	,,Q///	 	=s`   ACBCB" +B ,B" 0'CCC B" "
C,C CCCCc                    U R                   $ Nr6   r,   s    r.   _run_output_keyLLMChain._run_output_key  s    r1   c                   UR                    Vs/ sH,  nU R                  U R                  R                  U5      SU0PM.     nnU R                  (       a(  U Vs/ sH  o@R                  X@R                     0PM     nnU$ s  snf s  snf )zCreate outputs from response.rD   )rW   r6   r8   parse_resultr9   )r-   
llm_result
generationresultrs        r.   rL   LLMChain.create_outputs   s     )44
 5
 !3!3!@!@!L!: 5 	 
 !!EKLV//(:;VFL
 Ms   2A>"Bc                h   #    U R                  U/US9I S h  vN nU R                  U5      S   $  N7frH   )rj   rL   rM   s       r.   _acallLLMChain._acall.  s:     
 kJJ""8,Q// Ks   202c                (    U " X!S9U R                      $ )  Format prompt with kwargs and pass to LLM.

Args:
    callbacks: Callbacks to pass to LLMChain
    **kwargs: Keys to pass to prompt template.

Returns:
    Completion from LLM.

Example:
    .. code-block:: python

        completion = llm.predict(adjective="funny")
rT   r   r-   rT   kwargss      r.   predictLLMChain.predict6  s     F0AAr1   c                V   #    U R                  X!S9I Sh  vN U R                     $  N7f)r   r   N)acallr6   r   s      r.   apredictLLMChain.apredictG  s'      jjj==tOO=s   )')c                    [         R                  " SSS9  U R                  " SSU0UD6nU R                  R                  b%  U R                  R                  R                  U5      $ U$ )z(Call predict and then parse the results.z_The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.   
stacklevelrT   r+   )warningswarnr   r2   r8   parser-   rT   r   r   s       r.   predict_and_parseLLMChain.predict_and_parseX  sa     	B	

 <	<V<;;$$0;;,,226::r1   c                   #    [         R                  " SSS9  U R                  " SSU0UD6I Sh  vN nU R                  R                  b%  U R                  R                  R                  U5      $ U$  NB7f)z)Call apredict and then parse the results.z`The apredict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.r   r   rT   Nr+   )r   r   r   r2   r8   r   r   s       r.   apredict_and_parseLLMChain.apredict_and_parseh  sm      	B	

 }}CyCFCC;;$$0;;,,226:: Ds   -A4A2AA4c                l    [         R                  " SSS9  U R                  XS9nU R                  U5      $ )&Call apply and then parse the results.z]The apply_and_parse method is deprecated, instead pass an output parser directly to LLMChain.r   r   r   )r   r   r   _parse_generationr-   ra   rT   r   s       r.   apply_and_parseLLMChain.apply_and_parsex  s<     	B	

 J<%%f--r1   c                    U R                   R                  bA  U Vs/ sH4  nU R                   R                  R                  X R                     5      PM6     sn$ U$ s  snf r   )r2   r8   r   r6   )r-   r   rd   s      r.   r   LLMChain._parse_generation  s_     ;;$$0 &%C ))//OO0DE%  	s   :Ac                   #    [         R                  " SSS9  U R                  XS9I Sh  vN nU R                  U5      $  N7f)r   z^The aapply_and_parse method is deprecated, instead pass an output parser directly to LLMChain.r   r   r   N)r   r   r   r   r   s       r.   aapply_and_parseLLMChain.aapply_and_parse  sH      	B	

 {{:{CC%%f-- Ds   (AA Ac                    g)N	llm_chainr+   r,   s    r.   _chain_typeLLMChain._chain_type  s    r1   c                :    [         R                  " U5      nU " XS9$ )z&Create LLMChain from LLM and template.)r3   r2   )r   from_template)clsr3   templateprompt_templates       r.   from_stringLLMChain.from_string  s     )66x@s33r1   c                J    [        U R                  5      R                  U5      $ r   )_get_language_modelr3   get_num_tokens)r-   r4   s     r.   _get_num_tokensLLMChain._get_num_tokens  s    "488,;;DAAr1   r+   )returnr)   )r   z	list[str]r   )rN   dict[str, Any]rJ   $Optional[CallbackManagerForChainRun]r   dict[str, str])ra   list[dict[str, Any]]rJ   r   r   r   )ra   r   rJ   )Optional[AsyncCallbackManagerForChainRun]r   r   )ra   r   rJ   r   r   -tuple[list[PromptValue], Optional[list[str]]])ra   r   rJ   r   r   r   )ra   r   rT   r   r   list[dict[str, str]])r   r5   )r   r   r   r   )rN   r   rJ   r   r   r   )rT   r   r   r   r   r5   )rT   r   r   r   r   z%Union[str, list[str], dict[str, Any]])rT   r   r   r   r   z%Union[str, list[str], dict[str, str]])ra   r   rT   r   r   /Sequence[Union[str, list[str], dict[str, str]]])r   r   r   r   )r3   r   r   r5   r   r'   )r4   r5   r   int))__name__
__module____qualname____firstlineno____doc__classmethodr/   __annotations__r6   r!   r   r8   r9   r:   r;   r    model_configpropertyrA   rE   rP   rK   rj   rX   rg   r   r   r   rL   r   r   r   r   r   r   r   r   r   r   r   __static_attributes__r+   r1   r.   r'   r'   '   s   B   
  "J).)OM&O #t"PT2J2 $L
 + + 4 4 =A00 :0 
	0 =A2(2 :2 
	2< BF2(2 ?2 
	2< =A( : 
7	8 BF( ? 
7	8  $(  
	8  $(  
	2  " BF00 ?0 
	0B"P&  $  
/	$  $  
/	&  $.(. . 
9	.	(	 
9	  $.(. . 
9	.   4 4
Br1   r'   c                j   [        U [        5      (       a  U $ [        U [        5      (       a  [        U R                  5      $ [        U [
        5      (       a  [        U R                  5      $ [        U [        [        45      (       a  [        U R                  5      $ S[        U 5       3n[        U5      e)NzAUnable to extract BaseLanguageModel from llm_like object of type )rZ   r   r   r   boundr   runnabler   r   defaulttyperw   )llm_liker}   s     r.   r   r     s    (-..(O,,"8>>22(122"8#4#455(^_=>>"8#3#344
K>
	  S/r1   )r   r   r   r   )4r   
__future__r   r   collections.abcr   typingr   r   r   r   langchain_core._apir	   langchain_core.callbacksr
   r   r   r   r   langchain_core.language_modelsr   r   langchain_core.messagesr   langchain_core.output_parsersr   r   langchain_core.outputsr   r   r   langchain_core.prompt_valuesr   langchain_core.promptsr   r   langchain_core.runnablesr   r   r   r   %langchain_core.runnables.configurabler   langchain_core.utils.inputr   pydanticr    r!   langchain.chains.baser"   r'   r   r+   r1   r.   <module>r      s    8 "  $ - - *  0 N H H 4 E  B 7 & ' 
8
~Bu ~B
~BBr1   