a
    •˜ëji  ã                   @  sî   d Z ddl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 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ ddlmZ edddd�G dd„ deƒƒZG dd„ dee e!e!f  ƒZ"dS )z+Base classes for LLM-powered router chains.é    )Úannotations)ÚAnyÚOptionalÚcast)Ú
deprecated)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRun)ÚOutputParserException)ÚBaseLanguageModel)ÚBaseOutputParser)ÚBasePromptTemplate)Úparse_and_check_json_markdown)Úmodel_validator)ÚSelfÚoverride©ÚLLMChain)ÚRouterChainz0.2.12z1.0zÃUse RunnableLambda to select from multiple prompt templates. See example in API reference: https://api.python.langchain.com/en/latest/chains/langchain.chains.router.llm_router.LLMRouterChain.html)ZsinceZremovalÚmessagec                      sœ   e Zd ZU dZded< edd�ddœdd	„ƒZed
dœdd„ƒZdddœ‡ fdd„Z	d ddddœdd„Z
d!ddddœdd„Zedddd dœdd„ƒZ‡  ZS )"ÚLLMRouterChaina	  A router chain that uses an LLM chain to perform routing.

    This class is deprecated. See below for a replacement, which offers several
    benefits, including streaming and batch support.

    Below is an example implementation:

        .. code-block:: python

            from operator import itemgetter
            from typing import Literal
            from typing_extensions import TypedDict

            from langchain_core.output_parsers import StrOutputParser
            from langchain_core.prompts import ChatPromptTemplate
            from langchain_core.runnables import RunnableLambda, RunnablePassthrough
            from langchain_openai import ChatOpenAI

            llm = ChatOpenAI(model="gpt-4o-mini")

            prompt_1 = ChatPromptTemplate.from_messages(
                [
                    ("system", "You are an expert on animals."),
                    ("human", "{query}"),
                ]
            )
            prompt_2 = ChatPromptTemplate.from_messages(
                [
                    ("system", "You are an expert on vegetables."),
                    ("human", "{query}"),
                ]
            )

            chain_1 = prompt_1 | llm | StrOutputParser()
            chain_2 = prompt_2 | llm | StrOutputParser()

            route_system = "Route the user's query to either the animal "
            "or vegetable expert."
            route_prompt = ChatPromptTemplate.from_messages(
                [
                    ("system", route_system),
                    ("human", "{query}"),
                ]
            )


            class RouteQuery(TypedDict):
                """Route query to destination."""
                destination: Literal["animal", "vegetable"]


            route_chain = (
                route_prompt
                | llm.with_structured_output(RouteQuery)
                | itemgetter("destination")
            )

            chain = {
                "destination": route_chain,  # "animal" or "vegetable"
                "query": lambda x: x["query"],  # pass through input query
            } | RunnableLambda(
                # if animal, chain_1. otherwise, chain_2.
                lambda x: chain_1 if x["destination"] == "animal" else chain_2,
            )

            chain.invoke({"query": "what color are carrots"})

    r   Ú	llm_chainÚafter)Úmoder   )Úreturnc                 C  s"   | j j}|jd u rd}t|ƒ‚| S )NzÈLLMRouterChain requires base llm_chain prompt to have an output parser that converts LLM text output to a dictionary with keys 'destination' and 'next_inputs'. Received a prompt with no output parser.)r   ÚpromptÚoutput_parserÚ
ValueError)Úselfr   Úmsg© r   úa/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/chains/router/llm_router.pyÚ_validate_promptj   s    
ÿzLLMRouterChain._validate_promptz	list[str]c                 C  s   | j jS )zTWill be whatever keys the LLM chain prompt expects.

        :meta private:
        )r   Ú
input_keys)r   r   r   r    r"   w   s    zLLMRouterChain.input_keysúdict[str, Any]ÚNone)Úoutputsr   c                   s"   t ƒ  |¡ t|d tƒst‚d S )NÚnext_inputs)ÚsuperÚ_validate_outputsÚ
isinstanceÚdictr   )r   r%   ©Ú	__class__r   r    r(      s    z LLMRouterChain._validate_outputsNz$Optional[CallbackManagerForChainRun])ÚinputsÚrun_managerr   c                 C  s@   |p
t  ¡ }| ¡ }| jjf d|i|¤Ž}td| jjj |¡ƒS )NÚ	callbacksr#   )	r   Úget_noop_managerÚ	get_childr   Zpredictr   r   r   Úparse)r   r-   r.   Ú_run_managerr/   Z
predictionr   r   r    Ú_call„   s    þzLLMRouterChain._callz)Optional[AsyncCallbackManagerForChainRun]c                 Ã  s6   |p
t  ¡ }| ¡ }td| jjf d|i|¤ŽI d H ƒS )Nr#   r/   )r   r0   r1   r   r   Zapredict_and_parse)r   r-   r.   r3   r/   r   r   r    Ú_acall’   s    þzLLMRouterChain._acallr
   r   r   )Úllmr   Úkwargsr   c                 K  s   t ||d�}| f d|i|¤ŽS )zConvenience constructor.)r6   r   r   r   )Úclsr6   r   r7   r   r   r   r    Úfrom_llmž   s    zLLMRouterChain.from_llm)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r!   Úpropertyr"   r(   r4   r5   Úclassmethodr9   Ú__classcell__r   r   r+   r    r      s   

E ý ýr   c                   @  sJ   e Zd ZU dZdZded< eZded< dZded< e	dd	d
œdd„ƒZ
dS )ÚRouterOutputParserz<Parser for output of router chain in the multi-prompt chain.ÚDEFAULTÚstrÚdefault_destinationÚtypeÚnext_inputs_typeÚinputÚnext_inputs_inner_keyr#   )Útextr   c              
   C  sà   zœddg}t ||ƒ}t|d tƒs.d}t|ƒ‚t|d | jƒsTd| j› d�}t|ƒ‚| j|d i|d< |d  ¡  ¡ | j ¡ krŠd |d< n|d  ¡ |d< W n> t	yÚ } z&d|› d|› �}t
|ƒ|‚W Y d }~n
d }~0 0 |S )NZdestinationr&   z&Expected 'destination' to be a string.zExpected 'next_inputs' to be Ú.zParsing text
z
 raised following error:
)r   r)   rD   Ú	TypeErrorrG   rI   ÚstripÚlowerrE   Ú	Exceptionr	   )r   rJ   Zexpected_keysÚparsedr   Úer   r   r    r2   ±   s(    
ÿÿ
 zRouterOutputParser.parseN)r:   r;   r<   r=   rE   r>   rD   rG   rI   r   r2   r   r   r   r    rB   ª   s   
rB   N)#r=   Ú
__future__r   Útypingr   r   r   Zlangchain_core._apir   Zlangchain_core.callbacksr   r   Zlangchain_core.exceptionsr	   Zlangchain_core.language_modelsr
   Zlangchain_core.output_parsersr   Zlangchain_core.promptsr   Zlangchain_core.utils.jsonr   Zpydanticr   Ztyping_extensionsr   r   Zlangchain.chainsr   Zlangchain.chains.router.baser   r   r*   rD   rB   r   r   r   r    Ú<module>   s*   ü	 
