U
    h                     @  s   d 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 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 e	ddddG dd deZdS )zCUse a single chain to route an input to one of multiple llm chains.    )annotations)AnyDictListOptional)
deprecated)BaseLanguageModel)PromptTemplate)ConversationChain)Chain)LLMChain)MultiRouteChain)LLMRouterChainRouterOutputParser)MULTI_PROMPT_ROUTER_TEMPLATEz0.2.12z1.0zUse RunnableLambda to select from multiple prompt templates. See example in API reference: https://api.python.langchain.com/en/latest/chains/langchain.chains.router.multi_prompt.MultiPromptChain.html)ZsinceZremovalmessagec                   @  s>   e Zd ZdZeddddZedddd	d
d dddZdS )MultiPromptChaina	  A multi-route chain that uses an LLM router chain to choose amongst prompts.

    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"})
    z	List[str])returnc                 C  s   dgS )Ntext )selfr   r   H/tmp/pip-unpacked-wheel-bo69hh5q/langchain/chains/router/multi_prompt.pyoutput_keys`   s    zMultiPromptChain.output_keysNr   zList[Dict[str, str]]zOptional[Chain]r   )llmprompt_infosdefault_chainkwargsr   c                 K  s   dd |D }d |}tj|d}t|dgt d}t||}	i }
|D ]6}|d }|d }t|dgd	}t||d
}||
|< qJ|pt|dd}| f |	|
|d|S )zCConvenience constructor for instantiating from destination prompts.c                 S  s"   g | ]}|d   d|d  qS )namez: descriptionr   ).0pr   r   r   
<listcomp>m   s     z1MultiPromptChain.from_prompts.<locals>.<listcomp>
)destinationsinput)templateinput_variablesZoutput_parserr   prompt_template)r%   r&   )r   promptr   )r   Z
output_key)router_chaindestination_chainsr   )	joinr   formatr	   r   r   Zfrom_llmr   r
   )clsr   r   r   r   r#   Zdestinations_strZrouter_templateZrouter_promptr)   r*   Zp_infor   r'   r(   chainZ_default_chainr   r   r   from_promptsd   s4    	

zMultiPromptChain.from_prompts)N)__name__
__module____qualname____doc__propertyr   classmethodr/   r   r   r   r   r      s   
C r   N)r3   
__future__r   typingr   r   r   r   Zlangchain_core._apir   Zlangchain_core.language_modelsr   Zlangchain_core.promptsr	   Zlangchain.chainsr
   Zlangchain.chains.baser   Zlangchain.chains.llmr   Zlangchain.chains.router.baser   Z"langchain.chains.router.llm_routerr   r   Z+langchain.chains.router.multi_prompt_promptr   r   r   r   r   r   <module>   s"   	