a
    ag<                     @  sB  d Z ddlmZ ddl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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/m0Z0m1Z1 ddl2m3Z3 eddddG dd de3Z4dddddZ5dS )z2Chain that just formats a prompt and calls an LLM.    )annotationsN)AnyDictListOptionalSequenceTuple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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dZeddddZd[ddddd d!Zd\d"dd#d$d%d&Zd]d"d'd#d$d(d)Zd^d"dd*d$d+d,Zd_d"d'd*d$d-d.Zd`d"d/d0d1d2d3Zdad"d/d0d1d4d5Zeddd6d7Zd#d"d8d9d:Zdbdd'ddd;d<Zdcd/d=dd>d?d@Zddd/d=dd>dAdBZded/d=dCd>dDdEZdfd/d=dFd>dGdHZ dgd"d/dId1dJdKZ!d0dIdLdMdNZ"dhd"d/dId1dOdPZ#edddQdRZ$edSdd dTdUdVZ%ddWdXdYdZZ&dS )i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)
    bool)returnc                 C  s   dS )NT selfr)   r)   b/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain/chains/llm.pyis_lc_serializableM   s    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extraz	List[str]c                 C  s   | j jS )zJWill be whatever keys the prompt expects.

        :meta private:
        )r.   input_variablesr*   r)   r)   r,   
input_keysf   s    zLLMChain.input_keysc                 C  s   | j r| jgS | jdgS dS )z=Will always return text key.

        :meta private:
        full_generationN)r5   r2   r*   r)   r)   r,   output_keysn   s    zLLMChain.output_keysNzDict[str, Any]z$Optional[CallbackManagerForChainRun]zDict[str, str])inputsrun_managerr(   c                 C  s   | j |g|d}| |d S Nr>   r   )generatecreate_outputsr+   r=   r>   responser)   r)   r,   _cally   s    zLLMChain._callzList[Dict[str, Any]]r   )
input_listr>   r(   c           	      C  s   | j ||d\}}|r| nd}t| jtrJ| jj||fd|i| jS | jjf d|i| jt	t
|d|i}g }|D ]4}t|tr|t|dg q||t|dg q|t|dS dS z Generate LLM result from inputs.r@   N	callbacksstop)message)r0   )generations)prep_prompts	get_child
isinstancer/   r   Zgenerate_promptr7   bindbatchr
   r   r   appendr   r   r   	r+   rF   r>   promptsrI   rH   resultsrK   resr)   r)   r,   rA      s(    
zLLMChain.generatez)Optional[AsyncCallbackManagerForChainRun]c           	        s   | j ||dI dH \}}|r$| nd}t| jtrV| jj||fd|i| jI dH S | jjf d|i| jt	t
|d|iI dH }g }|D ]4}t|tr|t|dg q|t|dg qt|dS dS rG   )aprep_promptsrM   rN   r/   r   Zagenerate_promptr7   rO   Zabatchr
   r   r   rQ   r   r   r   rR   r)   r)   r,   	agenerate   s(    

zLLMChain.ageneratez-Tuple[List[PromptValue], Optional[List[str]]]c           	        s   d}t |dkrg |fS d|d v r0|d d }g }|D ]~  fdd| jjD }| jjf i |}t| d}d| }|r|j|d| jd	 d v r d |krtd
|	| q8||fS )Prepare prompts from inputs.Nr   rI   c                   s   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.
lenr.   r9   Zformat_promptr!   Z	to_stringZon_textrd   
ValueErrorrQ   	r+   rF   r>   rI   rS   Zselected_inputsr.   Z_colored_text_textr)   r\   r,   rL      s&    zLLMChain.prep_promptsc           	        s   d}t |dkrg |fS d|d v r0|d d }g }|D ]  fdd| jjD }| jjf i |}t| d}d| }|r|j|d| jd	I dH  d v r d |krtd
|	| q8||fS )rX   Nr   rI   c                   s   i | ]}| | qS r)   r)   rY   r\   r)   r,   r]      r^   z*LLMChain.aprep_prompts.<locals>.<dictcomp>r_   r`   ra   rb   re   rf   ri   r)   r\   r,   rV      s&    zLLMChain.aprep_promptsr   zList[Dict[str, str]])rF   rH   r(   c              
   C  s   t || j| j}|jdd|i|  d}z| j||d}W n2 tyn } z|| |W Y d}~n
d}~0 0 | 	|}|
d|i |S z0Utilize the LLM generate method for speed gains.NrF   )namer@   outputs)r   	configurerH   rd   on_chain_startget_namerA   BaseExceptionon_chain_errorrB   on_chain_endr+   rF   rH   Zcallback_managerr>   rD   erm   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 n8 ty } z ||I dH  |W Y d}~n
d}~0 0 | 	|}|
d|iI dH  |S rk   )r   rn   rH   rd   ro   rp   rW   rq   rr   rB   rs   rt   r)   r)   r,   aapply   s     

zLLMChain.aapplyc                 C  s   | j S Nr2   r*   r)   r)   r,   _run_output_key  s    zLLMChain._run_output_key)
llm_resultr(   c                   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;   )r2   r4   Zparse_result)rZ   
generationr*   r)   r,   
<listcomp>  s   z+LLMChain.create_outputs.<locals>.<listcomp>c                   s   g | ]} j | j  iqS r)   ry   )rZ   rr*   r)   r,   r}   $  r^   )rK   r5   )r+   r{   resultr)   r*   r,   rB     s    
zLLMChain.create_outputsc                   s$   | j |g|dI d H }| |d S r?   )rW   rB   rC   r)   r)   r,   _acall'  s    zLLMChain._acallr   )rH   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")
        rH   ry   r+   rH   r   r)   r)   r,   predict/  s    zLLMChain.predictc                   s   | j ||dI dH | j S )r   r   N)Zacallr2   r   r)   r)   r,   apredict@  s    zLLMChain.apredictz%Union[str, List[str], Dict[str, Any]]c                 K  s@   t d | jf d|i|}| jjdur8| jj|S |S d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.rH   N)warningswarnr   r.   r4   parser+   rH   r   r   r)   r)   r,   predict_and_parseQ  s    zLLMChain.predict_and_parsez%Union[str, List[str], Dict[str, str]]c                   sF   t d | jf d|i|I dH }| jjdur>| jj|S |S d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.rH   N)r   r   r   r.   r4   r   r   r)   r)   r,   apredict_and_parse_  s    zLLMChain.apredict_and_parsez/Sequence[Union[str, List[str], Dict[str, str]]]c                 C  s"   t 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   rv   _parse_generationr+   rF   rH   r   r)   r)   r,   apply_and_parsem  s
    zLLMChain.apply_and_parse)r|   r(   c                   s&    j jd ur fdd|D S |S d S )Nc                   s    g | ]} j j| j qS r)   )r.   r4   r   r2   )rZ   rU   r*   r)   r,   r}   |  s   z.LLMChain._parse_generation.<locals>.<listcomp>)r.   r4   )r+   r|   r)   r*   r,   r   x  s
    
zLLMChain._parse_generationc                   s(   t 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   N)r   r   rw   r   r   r)   r)   r,   aapply_and_parse  s
    zLLMChain.aapply_and_parsec                 C  s   dS )NZ	llm_chainr)   r*   r)   r)   r,   _chain_type  s    zLLMChain._chain_typer   )r/   templater(   c                 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int)r0   r(   c                 C  s   t | j|S rx   )_get_language_modelr/   Zget_num_tokens)r+   r0   r)   r)   r,   _get_num_tokens  s    zLLMChain._get_num_tokens)N)N)N)N)N)N)N)N)N)N)N)N)N)N)'__name__
__module____qualname____doc__classmethodr-   __annotations__r2   r#   r   r4   r5   r6   r7   r"   Zmodel_configpropertyr:   r<   rE   rA   rW   rL   rV   rv   rw   rz   rB   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r,   r&   &   sh   
!            r&   r   r   )llm_liker(   c                 C  sd   t | tr| S t | tr"t| jS t | tr6t| jS t | ttfrNt| j	S t
dt|  d S )NzAUnable to extract BaseLanguageModel from llm_like object of type )rN   r   r   r   boundr   Zrunnabler   r    defaultrh   type)r   r)   r)   r,   r     s    





r   )6r   
__future__r   r   typingr   r   r   r   r   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>   s2   (  s