Ë
    ´ŒjJ  ã                  ó  — 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 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ÚOptionalÚcast)Ú
deprecated)ÚAsyncCallbackManagerForChainRunÚCallbackManagerForChainRun)ÚOutputParserException)ÚBaseLanguageModel)ÚBaseOutputParser)ÚBasePromptTemplate)Úparse_and_check_json_markdown)Úmodel_validator)ÚSelf©Ú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)Ú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 )Ú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)Úmodec                ód   — | j                   j                  }|j                  €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   Úmsgs      úl/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/langchain/chains/router/llm_router.pyÚvalidate_promptzLLMRouterChain.validate_prompth   s8   € à—‘×&Ñ&ˆØ×ÑÐ'ðð ô ˜S“/Ð!Øˆó    c                ó.   — | j                   j                  S )zTWill be whatever keys the LLM chain prompt expects.

        :meta private:
        )r   Ú
input_keys)r    s    r"   r&   zLLMRouterChain.input_keysu   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        t        t        t        f   | j                  j                  j                  j                  |«      «      S ©NÚ	callbacks© )r	   Úget_noop_managerÚ	get_childr   Úpredictr   r,   Ústrr   r   r   Úparse)r    ÚinputsÚrun_managerÚ_run_managerr1   Ú
predictions         r"   Ú_callzLLMRouterChain._call‚   sz   € ð
 #ÒSÔ&@×&QÑ&QÓ&SˆØ ×*Ñ*Ó,ˆ	à+�T—^‘^×+Ñ+ÑJ°iÐJÀ6ÑJˆ
ÜÜ””c�‰NØ�N‰N×!Ñ!×/Ñ/×5Ñ5°jÓAó
ð 	
r$   c              ƒ  óà   K  — |xs t        j                  «       }|j                  «       }t        t        t
        t        f    | j                  j                  dd|i|¤Žƒ d {  –—† «      S 7 Œ­wr0   )	r	   r3   r4   r   r,   r6   r   r   Úapredict_and_parse)r    r8   r9   r:   r1   s        r"   Ú_acallzLLMRouterChain._acall�   sd   è ø€ ð
 #ÒSÔ&@×&QÑ&QÓ&SˆØ ×*Ñ*Ó,ˆ	ÜÜ””c�‰NØ3�$—.‘.×3Ñ3ÑR¸iÐRÈ6ÑR×Ró
ð 	
àRús   ‚A!A.Á#A,
Á$	A.c                ó0   — t        ||¬«      } | dd|i|¤ŽS )zConvenience constructor.)Úllmr   r   r2   r   )ÚclsrA   r   Úkwargsr   s        r"   Úfrom_llmzLLMRouterChain.from_llmœ   s#   € ô  ¨VÔ4ˆ	ÙÑ1˜YÐ1¨&Ñ1Ð1r$   )Úreturnr   )rE   z	list[str])r-   údict[str, Any]rE   ÚNone)N)r8   rF   r9   z$Optional[CallbackManagerForChainRun]rE   rF   )r8   rF   r9   z)Optional[AsyncCallbackManagerForChainRun]rE   rF   )rA   r   r   r   rC   r   rE   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r#   Úpropertyr&   r*   r<   r?   ÚclassmethodrD   Ú__classcell__)r.   s   @r"   r   r      sÎ   ø… ñAðF ÓØ+á˜'Ô"ò
ó #ð
ð ò)ó ð)õð =Að
àð
ð :ð
ð 
ó	
ð" BFð

àð

ð ?ð

ð 
ó	

ð ð2àð2ð #ð2ð ð	2ð
 
ò2ó ô2r$   r   c                  óD   — e Zd ZU dZdZded<   eZded<   dZded<   dd	„Z	y
)ÚRouterOutputParserz<Parser for output of router chain in the multi-prompt chain.ÚDEFAULTr6   Ú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+   r6   Ú	TypeErrorrU   rW   ÚstripÚlowerrS   Ú	Exceptionr
   )r    ÚtextÚexpected_keysÚparsedr!   Úes         r"   r7   zRouterOutputParser.parse¯   s   € ð	4Ø*¨MÐ:ˆMÜ2°4¸ÓGˆFÜ˜f ]Ñ3´SÔ9Ø>�Ü “nÐ$Ü˜f ]Ñ3°T×5JÑ5JÔKØ5°d×6KÑ6KÐ5LÈAÐN�Ü “nÐ$Ø%)×%?Ñ%?ÀÈÑAVÐ$WˆF�=Ñ!à�}Ñ%×+Ñ+Ó-×3Ñ3Ó5Ø×+Ñ+×1Ñ1Ó3ò4ð )-��}Ñ%ð ˆð	 )/¨}Ñ(=×(CÑ(CÓ(E��}Ò%ð ˆøô ò 	4Ø" 4 &Ð(DÀQÀCÐHˆCÜ'¨Ó,°!Ð3ûð	4ús   ‚B9C Â=C Ã	C8ÃC3Ã3C8N)r_   r6   rE   rF   )
rH   rI   rJ   rK   rS   rL   r6   rU   rW   r7   r2   r$   r"   rQ   rQ   ¨   s+   … ÙFà(Ð˜Ó(Ø Ð�dÓ Ø!(Ð˜3Ó(ôr$   rQ   N)"rK   Ú
__future__r   Útypingr   r   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   Úlangchain.chainsr   Úlangchain.chains.router.baser   r   r,   r6   rQ   r2   r$   r"   Ú<module>rp      s€   ðÙ 1å "ç &Ñ &å *÷õ <Ý <Ý :Ý 5Ý CÝ $Ý "å %Ý 4ñ Ø
Øð	sô	ôD2�[ó D2óðD2ôNÐ)¨$¨s°C¨x©.Ñ9õ r$   