
    3fi                       d Z ddl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 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       G d de             Z G d deee e f            Z!y)z+Base classes for LLM-powered router chains.    )annotations)Anycast)
deprecated)AsyncCallbackManagerForChainRunCallbackManagerForChainRun)OutputParserException)BaseLanguageModel)BaseOutputParser)BasePromptTemplate)parse_and_check_json_markdown)model_validator)SelfoverrideLLMChain)RouterChainz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.llm_router.LLMRouterChain.html)sinceremovalmessagec                       e Zd ZU dZded<   	  ed      dd       Zedd       Zd fdZ		 d	 	 	 	 	 dd	Z
	 d	 	 	 	 	 dd
Ze	 	 	 	 	 	 	 	 dd       Z xZS )LLMRouterChainah  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:

        ```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

        model = 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 | model | StrOutputParser()
        chain_2 = prompt_2 | model | 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
            | model.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)modec                d    | j                   j                  }|j                  d}t        |      | S )NzLLMRouterChain 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   msgs      h/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_classic/chains/router/llm_router.py_validate_promptzLLMRouterChain._validate_promptj   s8    &&'  S/!    c                .    | j                   j                  S )z3Will be whatever keys the LLM chain prompt expects.)r   
input_keys)r    s    r"   r&   zLLMRouterChain.input_keysw   s     ~~(((r$   c                V    t         |   |       t        |d   t              st        y )Nnext_inputs)super_validate_outputs
isinstancedictr   )r    outputs	__class__s     r"   r*   z LLMRouterChain._validate_outputs|   s*    !'*'-0$7 8r$   c                    |xs t        j                         }|j                         } | j                  j                  dd|i|}t        d| j                  j                  j                  j                  |            S )N	callbacksdict[str, Any] )	r   get_noop_manager	get_childr   predictr   r   r   parse)r    inputsrun_manager_run_managerr0   
predictions         r"   _callzLLMRouterChain._call   sr    
 #S&@&Q&Q&S **,	+T^^++JiJ6J
NN!!//55jA
 	
r$   c                   K   |xs t        j                         }|j                         }t        d | j                  j
                  dd|i| d {         S 7 w)Nr1   r0   r2   )r   r3   r4   r   r   apredict_and_parse)r    r7   r8   r9   r0   s        r"   _acallzLLMRouterChain._acall   s\     
 #S&@&Q&Q&S **,	3$..33RiR6RR
 	
Rs   AAA
	Ac                0    t        ||      } | dd|i|S )zConvenience constructor.)llmr   r   r2   r   )clsr@   r   kwargsr   s        r"   from_llmzLLMRouterChain.from_llm   s#     V4	1Y1&11r$   )returnr   )rD   z	list[str])r-   r1   rD   None)N)r7   r1   r8   z!CallbackManagerForChainRun | NonerD   r1   )r7   r1   r8   z&AsyncCallbackManagerForChainRun | NonerD   r1   )r@   r
   r   r   rB   r   rD   r   )__name__
__module____qualname____doc____annotations__r   r#   propertyr&   r*   r;   r>   classmethodrC   __classcell__)r.   s   @r"   r   r      s    CJ +'"
 #
 ) ) :>

 7
 
	
" ?C



 <

 
	

 22 #2 	2
 
2 2r$   r   c                  N    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	       Z
y
)RouterOutputParserz<Parser for output of router chain in the multi-prompt chain.DEFAULTstrdefault_destinationtypenext_inputs_typeinputnext_inputs_inner_keyc                   	 ddg}t        ||      }t        |d   t              sd}t        |      t        |d   | j                        sd| j                   d}t        |      | j
                  |d   i|d<   |d   j                         j                         | j                  j                         k(  rd |d<   |S |d   j                         |d<   	 |S # t        $ r}d| d| }t        |      |d }~ww xY w)Ndestinationr(   z&Expected 'destination' to be a string.zExpected 'next_inputs' to be .zParsing text
z
 raised following error:
)r   r+   rQ   	TypeErrorrT   rV   striplowerrR   	Exceptionr	   )r    textexpected_keysparsedr!   es         r"   r6   zRouterOutputParser.parse   s    	4*M:M24GFf]3S9>n$f]3T5J5JK5d6K6K5LANn$%)%?%?AV$WF=!}%++-335++1134 )-}% 	 )/}(=(C(C(E}%   	4"4&(DQCHC',!3	4s   B9C =C 	C8C33C8N)r^   rQ   rD   r1   )rF   rG   rH   rI   rR   rJ   rQ   rT   rV   r   r6   r2   r$   r"   rO   rO      s8    F(( d !(3( r$   rO   N)"rI   
__future__r   typingr   r   langchain_core._apir   langchain_core.callbacksr   r   langchain_core.exceptionsr	   langchain_core.language_modelsr
   langchain_core.output_parsersr   langchain_core.promptsr   langchain_core.utils.jsonr   pydanticr   typing_extensionsr   r   langchain_classic.chainsr   $langchain_classic.chains.router.baser   r   r,   rQ   rO   r2   r$   r"   <module>ro      s}    1 "  * < < : 5 C $ , - < 
	s	C2[ C2C2L)$sCx.9 r$   