o
    {qi<                     @  s8  d Z ddlmZ ddlZddlmZ ddlmZmZm	Z	m
Z
 ddlmZ ddlmZmZ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dl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&m'Z'm(Z( ddl)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 eddddG dd de1Z2dddZ3dS ) 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)ZsincealternativeZremovalc                   @  s  e Zd ZU dZed[ddZded< 	 ded	< 	 d
Zded< ee	dZ
ded< 	 dZded< 	 eedZded< edddZed\ddZed\ddZ	d]d^d!d"Z	d]d_d&d'Z	d]d`d)d*Z	d]dad,d-Z	d]dbd.d/Z	d]dcd3d4Z	d]dcd5d6Zeddd7d8Zded:d;Z	d]dfd<d=Zd]dgd@dAZd]dgdBdCZ	d]dhdEdFZ	d]didHdIZ 	d]djdKdLZ!dkdNdOZ"	d]djdPdQZ#edddRdSZ$edldVdWZ%dmdYdZZ&dS )nLLMChaina^  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)
    returnboolc                 C     dS )NT selfr'   r'   [/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain/chains/llm.pyis_lc_serializableN      zLLMChain.is_lc_serializabler   promptzSUnion[Runnable[LanguageModelInput, str], Runnable[LanguageModelInput, BaseMessage]]llmtextstr
output_key)default_factoryr   output_parserTreturn_final_onlydict
llm_kwargsZforbid)Zarbitrary_types_allowedextra	list[str]c                 C  s   | j jS )zJWill be whatever keys the prompt expects.

        :meta private:
        )r-   input_variablesr(   r'   r'   r*   
input_keysh   s   zLLMChain.input_keysc                 C  s   | j r| jgS | jdgS )z=Will always return text key.

        :meta private:
        full_generation)r4   r1   r(   r'   r'   r*   output_keysp   s   
zLLMChain.output_keysNinputsdict[str, Any]run_manager$Optional[CallbackManagerForChainRun]dict[str, str]c                 C  s   | j |g|d}| |d S Nr?   r   )generatecreate_outputsr)   r=   r?   responser'   r'   r*   _callz   s   zLLMChain._call
input_listlist[dict[str, Any]]r   c           	      C  s   | j ||d\}}|r| nd}t| jtr%| jj||fd|i| jS | jjdd|i| jt	t
|d|i}g }|D ]}t|trO|t|dg q>|t|dg q>t|dS 	z Generate LLM result from inputs.rC   N	callbacksstop)message)r/   )generationsr'   )prep_prompts	get_child
isinstancer.   r   Zgenerate_promptr6   bindbatchr   listr   appendr   r   r   	r)   rI   r?   promptsrM   rL   resultsrO   resr'   r'   r*   rD      s*   

zLLMChain.generate)Optional[AsyncCallbackManagerForChainRun]c           	        s   | j ||dI dH \}}|r| nd}t| jtr,| jj||fd|i| jI dH S | jjdd|i| jt	t
|d|iI dH }g }|D ]}t|trY|t|dg qH|t|dg qHt|dS rK   )aprep_promptsrQ   rR   r.   r   Zagenerate_promptr6   rS   Zabatchr   rU   r   rV   r   r   r   rW   r'   r'   r*   	agenerate   s,   


zLLMChain.agenerate-tuple[list[PromptValue], Optional[list[str]]]c           
        s   d}t |dkrg |fS d|d v r|d d }g }|D ]A  fdd| jjD }| jjdi |}t| d}d| }|rH|j|d| jd	 d v rX d |krXd
}	t|	|	| q||fS )Prepare prompts from inputs.Nr   rM   c                      i | ]}| | qS r'   r'   .0kr=   r'   r*   
<dictcomp>       z)LLMChain.prep_prompts.<locals>.<dictcomp>greenPrompt after formatting:

endverbose=If `stop` is present in any inputs, should be present in all.r'   
lenr-   r9   Zformat_promptr   Z	to_stringZon_textrl   
ValueErrorrV   
r)   rI   r?   rM   rX   Zselected_inputsr-   Z_colored_text_textmsgr'   rd   r*   rP      s$   zLLMChain.prep_promptsc           
        s   d}t |dkrg |fS d|d v r|d d }g }|D ]D  fdd| jjD }| jjdi |}t| d}d| }|rL|j|d| jd	I dH  d v r\ d |kr\d
}	t|	|	| q||fS )r_   Nr   rM   c                   r`   r'   r'   ra   rd   r'   r*   re      rf   z*LLMChain.aprep_prompts.<locals>.<dictcomp>rg   rh   ri   rj   rm   r'   rn   rq   r'   rd   r*   r\      s&   zLLMChain.aprep_promptsrL   r   list[dict[str, str]]c              
   C  s~   t || j| j}|jdd|i|  d}z	| j||d}W n ty0 } z||  d}~ww | 	|}|
d|i |S z0Utilize the LLM generate method for speed gains.NrI   )namerC   outputs)r   	configurerL   rl   on_chain_startget_namerD   BaseExceptionon_chain_errorrE   on_chain_endr)   rI   rL   Zcallback_managerr?   rG   erw   r'   r'   r*   apply   s(   

zLLMChain.applyc              
     s   t || j| j}|jdd|i|  dI dH }z| j||dI dH }W n ty: } z	||I dH   d}~ww | 	|}|
d|iI dH  |S ru   )r	   rx   rL   rl   ry   rz   r]   r{   r|   rE   r}   r~   r'   r'   r*   aapply  s*   
zLLMChain.aapplyc                 C  s   | j S Nr1   r(   r'   r'   r*   _run_output_key  s   zLLMChain._run_output_key
llm_resultc                   s0    fdd|j D } jr fdd|D }|S )zCreate outputs from response.c                   s"   g | ]} j  j|d |iqS )r;   )r1   r3   Zparse_result)rb   
generationr(   r'   r*   
<listcomp>"  s    z+LLMChain.create_outputs.<locals>.<listcomp>c                   s   g | ]
} j | j  iqS r'   r   )rb   rr(   r'   r*   r   +  s    )rO   r4   )r)   r   resultr'   r(   r*   rE      s   
zLLMChain.create_outputsc                   s&   | j |g|dI d H }| |d S rB   )r]   rE   rF   r'   r'   r*   _acall.  s   zLLMChain._acallkwargsr   c                 K  s   | ||d| j  S )S  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")
        rL   r   r)   rL   r   r'   r'   r*   predict6  s   zLLMChain.predictc                   s   | j ||dI dH | j S )r   r   N)Zacallr1   r   r'   r'   r*   apredictG  s   zLLMChain.apredict%Union[str, list[str], dict[str, Any]]c                 K  s@   t jddd | jdd|i|}| jjdur| jj|S |S )z(Call predict and then parse the results.z_The predict_and_parse method is deprecated, instead pass an output parser directly to LLMChain.   
stacklevelrL   Nr'   )warningswarnr   r-   r3   parser)   rL   r   r   r'   r'   r*   predict_and_parseX  s   zLLMChain.predict_and_parse%Union[str, list[str], dict[str, str]]c                   sH   t jddd | jdd|i|I dH }| jjdur"| jj|S |S )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   rL   Nr'   )r   r   r   r-   r3   r   r   r'   r'   r*   apredict_and_parseh  s   zLLMChain.apredict_and_parse/Sequence[Union[str, list[str], dict[str, str]]]c                 C  s&   t jddd | j||d}| |S )&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)   rI   rL   r   r'   r'   r*   apply_and_parsex  s   
zLLMChain.apply_and_parser   c                   s"    j jd ur fdd|D S |S )Nc                   s    g | ]} j j| j qS r'   )r-   r3   r   r1   )rb   rZ   r(   r'   r*   r     s    z.LLMChain._parse_generation.<locals>.<listcomp>)r-   r3   )r)   r   r'   r(   r*   r     s
   
zLLMChain._parse_generationc                   s.   t jddd | j||dI dH }| |S )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   r'   r'   r*   aapply_and_parse  s   
zLLMChain.aapply_and_parsec                 C  r&   )NZ	llm_chainr'   r(   r'   r'   r*   _chain_type  r,   zLLMChain._chain_typer   templatec                 C  s   t |}| ||dS )z&Create LLMChain from LLM and template.)r.   r-   )r   Zfrom_template)clsr.   r   Zprompt_templater'   r'   r*   from_string  s   
zLLMChain.from_stringintc                 C  s   t | j|S r   )_get_language_modelr.   Zget_num_tokens)r)   r/   r'   r'   r*   _get_num_tokens  s   zLLMChain._get_num_tokens)r$   r%   )r$   r8   r   )r=   r>   r?   r@   r$   rA   )rI   rJ   r?   r@   r$   r   )rI   rJ   r?   r[   r$   r   )rI   rJ   r?   r@   r$   r^   )rI   rJ   r?   r[   r$   r^   )rI   rJ   rL   r   r$   rt   )r$   r0   )r   r   r$   rJ   )r=   r>   r?   r[   r$   rA   )rL   r   r   r   r$   r0   )rL   r   r   r   r$   r   )rL   r   r   r   r$   r   )rI   rJ   rL   r   r$   r   )r   rt   r$   r   )r.   r   r   r0   r$   r#   )r/   r0   r$   r   )'__name__
__module____qualname____doc__classmethodr+   __annotations__r1   r    r   r3   r4   r5   r6   r   Zmodel_configpropertyr:   r<   rH   rD   r]   rP   r\   r   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r'   r'   r'   r*   r#   '   sr   
 !

r#   llm_liker   r$   r   c                 C  sd   t | tr| S t | trt| jS t | trt| jS t | ttfr't| j	S dt
|  }t|)NzAUnable to extract BaseLanguageModel from llm_like object of type )rR   r   r   r   boundr   Zrunnabler   r   defaulttyperp   )r   rs   r'   r'   r*   r     s   





r   )r   r   r$   r   )4r   
__future__r   r   collections.abcr   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.callbacksr	   r
   r   r   r   Zlangchain_core.language_modelsr   r   Zlangchain_core.messagesr   Zlangchain_core.output_parsersr   r   Zlangchain_core.outputsr   r   r   Zlangchain_core.prompt_valuesr   Zlangchain_core.promptsr   r   Zlangchain_core.runnablesr   r   r   r   Z%langchain_core.runnables.configurabler   Zlangchain_core.utils.inputr   Zpydanticr   r    Zlangchain.chains.baser!   r#   r   r'   r'   r'   r*   <module>   s8       