
    Ig                        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       G d de             Zy)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)sinceremovalmessagec                  J    e Zd ZdZedd       Ze	 d	 	 	 	 	 	 	 	 	 dd       Zy)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"})
    c                    dgS )Ntext )selfs    a/var/www/html/answerous/venv/lib/python3.12/site-packages/langchain/chains/router/multi_prompt.pyoutput_keyszMultiPromptChain.output_keys`   s	    x    Nc                   |D cg c]  }|d    d|d     }}dj                  |      }t        j                  |      }t        |dgt	                     }	t        j                  ||	      }
i }|D ],  }|d   }|d   }t        |dg	      }t        ||
      }|||<   . |xs t        |d      } | d|
||d|S c c}w )zCConvenience constructor for instantiating from destination prompts.namez: description
)destinationsinput)templateinput_variablesoutput_parserprompt_template)r$   r%   )llmpromptr   )r(   
output_key)router_chaindestination_chainsdefault_chainr   )	joinr   formatr
   r   r   from_llmr   r   )clsr(   prompt_infosr-   kwargspr"   destinations_strrouter_templaterouter_promptr+   r,   p_infor   r'   r)   chain_default_chains                     r   from_promptszMultiPromptChain.from_promptsd   s    EQQq1V9+R-(8'9:QQ99\26==)
 '$$I,.

 &..sMB" 	-F&>D$%67O#_wiXFV4E',t$	- 'W*;PV*W 
%1(
 	
 	
' Rs   B?)returnz	List[str])N)
r(   r	   r2   zList[Dict[str, str]]r-   zOptional[Chain]r3   r   r<   r   )__name__
__module____qualname____doc__propertyr   classmethodr;   r   r   r   r   r      sf    AF   
 *.	 
 
 + 
 '	 

  
 
 
  
r   r   N)r@   
__future__r   typingr   r   r   r   langchain_core._apir   langchain_core.language_modelsr	   langchain_core.promptsr
   langchain.chainsr   langchain.chains.baser   langchain.chains.llmr   langchain.chains.router.baser   "langchain.chains.router.llm_routerr   r   +langchain.chains.router.multi_prompt_promptr   r   r   r   r   <module>rN      s[    I " , , * < 1 . ' ) 8 Q T 
	w	i
 i
i
r   