§
    ™Štj!*  ã                   ó¦  — d dl 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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m Z  d dl!m"Z" d dl#m$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. dZ/ eddd¬¦  «         G d„ de¦  «        ¦   «         Z0e.fddœded ee         d!ed"ed#e1e2e3         z  d$efd%„Z4dS )&é    N)ÚSequence)ÚAny)Ú
deprecated)ÚAgentAction)ÚBaseCallbackManager)ÚBaseLanguageModel)ÚBasePromptTemplate)ÚChatPromptTemplateÚHumanMessagePromptTemplateÚSystemMessagePromptTemplate)ÚRunnableÚRunnablePassthrough)ÚBaseTool)ÚToolsRenderer)ÚField)Úoverride)ÚAgentÚAgentOutputParser©Úformat_log_to_str)ÚJSONAgentOutputParser)Ú%StructuredChatOutputParserWithRetries)ÚFORMAT_INSTRUCTIONSÚPREFIXÚSUFFIX)ÚLLMChain)Ú render_text_description_and_argsz{input}

{agent_scratchpad}z0.1.0Úcreate_structured_chat_agentz2.0.0)ÚalternativeÚremovalc                   ó€  ‡ — e Zd ZU dZ ee¬¦  «        Zeed<   	 e	de
fd„¦   «         Ze	de
fd„¦   «         Zdeeee
f                  de
fˆ fd„Zed	ee         dd
fd„¦   «         Zee	 dded
z  dedefd„¦   «         ¦   «         Ze	edee
         fd„¦   «         ¦   «         Zeeeeeed
d
fd	ee         de
de
de
de
dee
         d
z  dee         d
z  defd„¦   «         ¦   «         Zed
d
eeeed
d
fded	ee         de d
z  ded
z  de
de
de
de
dee
         d
z  dee         d
z  dede!fd„¦   «         Z"e	de
fd„¦   «         Z#ˆ xZ$S )ÚStructuredChatAgentzStructured Chat Agent.)Údefault_factoryÚoutput_parserÚreturnc                 ó   — dS )z&Prefix to append the observation with.zObservation: © ©Úselfs    úk/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_classic/agents/structured_chat/base.pyÚobservation_prefixz&StructuredChatAgent.observation_prefix/   s	   € ð ˆó    c                 ó   — dS )z#Prefix to append the llm call with.zThought:r'   r(   s    r*   Ú
llm_prefixzStructuredChatAgent.llm_prefix4   s	   € ð ˆzr,   Úintermediate_stepsc                 ó¤   •— t          ¦   «                              |¦  «        }t          |t          ¦  «        sd}t	          |¦  «        ‚|rd|› �S |S )Nz*agent_scratchpad should be of type string.zhThis was your previous work (but I haven't seen any of it! I only see what you return as final answer):
)ÚsuperÚ_construct_scratchpadÚ
isinstanceÚstrÚ
ValueError)r)   r/   Úagent_scratchpadÚmsgÚ	__class__s       €r*   r2   z)StructuredChatAgent._construct_scratchpad9   sl   ø€ õ !™7œ7×8Ò8Ð9KÑLÔLÐÝÐ*­CÑ0Ô0ð 	"Ø>ˆCÝ˜S‘/”/Ð!Øð 	ðDà1AðDð Dðð
  Ðr,   ÚtoolsNc                 ó   — d S ©Nr'   )Úclsr9   s     r*   Ú_validate_toolsz#StructuredChatAgent._validate_toolsI   s   € àˆr,   ÚllmÚkwargsc                 ó,   — t          j        |¬¦  «        S )N©r>   )r   Úfrom_llm)r<   r>   r?   s      r*   Ú_get_default_output_parserz.StructuredChatAgent._get_default_output_parserM   s   € õ 5Ô=À#ÐFÑFÔFÐFr,   c                 ó   — dgS )NzObservation:r'   r(   s    r*   Ú_stopzStructuredChatAgent._stopV   s   € ð ÐÐr,   ÚprefixÚsuffixÚhuman_message_templateÚformat_instructionsÚinput_variablesÚmemory_promptsc                 ó  — g }|D ]e}	t          j        ddt          j        ddt          |	j        ¦  «        ¦  «        ¦  «        }
|                     |	j        › d|	j        › d|
› �¦  «         Œfd                     |¦  «        }d                     d	„ |D ¦   «         ¦  «        }|                     |¬
¦  «        }|› d|› d|› d|› �}|€ddg}|pg }t          j
        |¦  «        g|¢t          j
        |¦  «        ‘}t          ||¬¦  «        S )NÚ}z}}Ú{z{{z: z, args: ú
ú, c                 ó   — g | ]	}|j         ‘Œ
S r'   ©Úname©Ú.0Útools     r*   ú
<listcomp>z5StructuredChatAgent.create_prompt.<locals>.<listcomp>l   s   € Ð<Ð<Ð<¨d ¤	Ð<Ð<Ð<r,   )Ú
tool_namesz

Úinputr6   )rJ   Úmessages)ÚreÚsubr4   ÚargsÚappendrS   ÚdescriptionÚjoinÚformatr   Úfrom_templater   r
   )r<   r9   rF   rG   rH   rI   rJ   rK   Útool_stringsrV   Úargs_schemaÚformatted_toolsrX   ÚtemplateÚ_memory_promptsrZ   s                   r*   Úcreate_promptz!StructuredChatAgent.create_prompt[   sE  € ð ˆØð 	Yð 	YˆDÝœ&  d­B¬F°3¸½cÀ$Ä)¹n¼nÑ,MÔ,MÑNÔNˆKØ×Ò 4¤9Ð WÐ W°Ô0@Ð WÐ WÈ+Ð WÐ WÑXÔXÐXÐXØŸ)š) LÑ1Ô1ˆØ—Y’YÐ<Ð<°eÐ<Ñ<Ô<Ñ=Ô=ˆ
Ø1×8Ò8ÀJÐ8ÑOÔOÐØÐXÐX /ÐXÐXÐ7JÐXÐXÐPVÐXÐXˆØÐ"Ø&Ð(:Ð;ˆOØ(Ð.¨Bˆå'Ô5°hÑ?Ô?ð
àð
õ 'Ô4Ð5KÑLÔLð
ˆõ
 "°/ÈHÐUÑUÔUÐUr,   Úcallback_managerc           	      óè   — |                       |¦  «         |                      ||||||	|
¬¦  «        }t          |||¬¦  «        }d„ |D ¦   «         }|p|                      |¬¦  «        } | d|||dœ|¤ŽS )z)Construct an agent from an LLM and tools.)rF   rG   rH   rI   rJ   rK   )r>   Úpromptri   c                 ó   — g | ]	}|j         ‘Œ
S r'   rR   rT   s     r*   rW   z:StructuredChatAgent.from_llm_and_tools.<locals>.<listcomp>˜   s   € Ð2Ð2Ð2 D�d”iÐ2Ð2Ð2r,   rA   )Ú	llm_chainÚallowed_toolsr$   r'   )r=   rh   r   rC   )r<   r>   r9   ri   r$   rF   rG   rH   rI   rJ   rK   r?   rk   rm   rX   Ú_output_parsers                   r*   Úfrom_llm_and_toolsz&StructuredChatAgent.from_llm_and_toolsy   sÇ   € ð  	×Ò˜EÑ"Ô"Ð"Ø×"Ò"ØØØØ#9Ø 3Ø+Ø)ð #ñ 
ô 
ˆõ ØØØ-ð
ñ 
ô 
ˆ	ð
 3Ð2¨EÐ2Ñ2Ô2ˆ
Ø&ÐQ¨#×*HÒ*HÈSÐ*HÑ*QÔ*QˆØˆsð 
ØØ$Ø(ð
ð 
ð ð	
ð 
ð 	
r,   c                 ó   — t           ‚r;   )r5   r(   s    r*   Ú_agent_typezStructuredChatAgent._agent_type¡   s   € åÐr,   r;   )%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   r   Ú__annotations__Úpropertyr4   r+   r.   ÚlistÚtupler   r2   Úclassmethodr   r   r=   r   r   r   rC   rE   r   r   ÚHUMAN_MESSAGE_TEMPLATEr   r	   rh   r   r   rp   rr   Ú__classcell__)r8   s   @r*   r"   r"   &   s^  ø€ € € € € € à Ð à', uØ=ð(ñ (ô (€MÐ$ð ð ñ ð 'àð Cð ð ð ñ „Xðð ð˜Cð ð ð ñ „Xðð à   {°CÐ'7Ô!8Ô9ð ð 
ð ð  ð  ð  ð  ð  ð  ð H¨XÔ$6ð ¸4ð ð ð ñ „[ðð Øð )-ðGð Gà Ñ%ðGð ðGð 
ð	Gð Gð Gñ „Xñ „[ðGð Øð �t˜C”yð  ð  ð  ñ „Xñ „Xð ð Øð ØØ&<Ø#6Ø,0Ø:>ðVð Và˜Ô!ðVð ðVð ð	Vð
 !$ðVð !ðVð ˜cœ TÑ)ðVð Ð/Ô0°4Ñ7ðVð 
ðVð Vð Vñ „Xñ „[ðVð8 ð
 8<Ø26ØØØ&<Ø#6Ø,0Ø:>ð%
ð %
àð%
ð ˜Ô!ð%
ð .°Ñ4ð	%
ð
 )¨4Ñ/ð%
ð ð%
ð ð%
ð !$ð%
ð !ð%
ð ˜cœ TÑ)ð%
ð Ð/Ô0°4Ñ7ð%
ð ð%
ð 
ð%
ð %
ð %
ñ „[ð%
ðN ð˜Sð ð ð ñ „Xðð ð ð ð r,   r"   T)Ústop_sequencer>   r9   rk   Útools_rendererr~   r%   c                óÂ  — h d£                      |j        t          |j        ¦  «        z   ¦  «        }|rd|› �}t	          |¦  «        ‚|                      |t          |¦  «        ¦  «        d                     d„ |D ¦   «         ¦  «        ¬¦  «        }|r |du rdgn|}|                      |¬¦  «        }n| }t          j	        d	„ ¬
¦  «        |z  |z  t          ¦   «         z  S )a	  Create an agent aimed at supporting tools with multiple inputs.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more.
        stop_sequence: bool or list of str.
            If `True`, adds a stop token of "Observation:" to avoid hallucinates.
            If `False`, does not add a stop token.
            If a list of str, uses the provided list as the stop tokens.

            You may to set this to False if the LLM you are using
            does not support stop sequences.
        tools_renderer: This controls how the tools are converted into a string and
            then passed into the LLM.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.

    Examples:
        ```python
        from langchain_classic import hub
        from langchain_openai import ChatOpenAI
        from langchain_classic.agents import (
            AgentExecutor,
            create_structured_chat_agent,
        )

        prompt = hub.pull("hwchase17/structured-chat-agent")
        model = ChatOpenAI()
        tools = ...

        agent = create_structured_chat_agent(model, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools)

        agent_executor.invoke({"input": "hi"})

        # Using with chat history
        from langchain_core.messages import AIMessage, HumanMessage

        agent_executor.invoke(
            {
                "input": "what's my name?",
                "chat_history": [
                    HumanMessage(content="hi! my name is bob"),
                    AIMessage(content="Hello Bob! How can I assist you today?"),
                ],
            }
        )
        ```

    Prompt:

        The prompt must have input keys:
            * `tools`: contains descriptions and arguments for each tool.
            * `tool_names`: contains all tool names.
            * `agent_scratchpad`: contains previous agent actions and tool outputs as a
                string.

        Here's an example:

        ```python
        from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

        system = '''Respond to the human as helpfully and accurately as possible. You have access to the following tools:

        {tools}

        Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).

        Valid "action" values: "Final Answer" or {tool_names}

        Provide only ONE action per $JSON_BLOB, as shown:

        ```txt
        {{
            "action": $TOOL_NAME,
            "action_input": $INPUT
        }}
        ```

        Follow this format:

        Question: input question to answer
        Thought: consider previous and subsequent steps
        Action:
        ```
        $JSON_BLOB
        ```
        Observation: action result
        ... (repeat Thought/Action/Observation N times)
        Thought: I know what to respond
        Action:
        ```txt
        {{
            "action": "Final Answer",
            "action_input": "Final response to human"
        }}

        Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:```$JSON_BLOB```then Observation'''

        human = '''{input}

        {agent_scratchpad}

        (reminder to respond in a JSON blob no matter what)'''

        prompt = ChatPromptTemplate.from_messages(
            [
                ("system", system),
                MessagesPlaceholder("chat_history", optional=True),
                ("human", human),
            ]
        )

        ```
    >   r9   rX   r6   z#Prompt missing required variables: rP   c                 ó   — g | ]	}|j         ‘Œ
S r'   rR   )rU   Úts     r*   rW   z0create_structured_chat_agent.<locals>.<listcomp>.  s   € Ð4Ð4Ð4¨˜aœfÐ4Ð4Ð4r,   )r9   rX   Tz
Observation)Ústopc                 ó,   — t          | d         ¦  «        S )Nr/   r   )Úxs    r*   ú<lambda>z.create_structured_chat_agent.<locals>.<lambda>8  s   € Õ'8¸Ð;OÔ9PÑ'QÔ'Q€ r,   )r6   )Ú
differencerJ   ry   Úpartial_variablesr5   Úpartialr`   Úbindr   Úassignr   )	r>   r9   rk   r   r~   Úmissing_varsr7   rƒ   Úllm_with_stops	            r*   r   r   ¦   s#  € ð~ ?Ð>Ð>×IÒIØÔ¥ fÔ&>Ñ!?Ô!?Ñ?ñô €Lð ð ØB°LÐBÐBˆÝ˜‰oŒoÐà�^Š^Øˆn�T %™[œ[Ñ)Ô)Ø—9’9Ð4Ð4¨eÐ4Ñ4Ô4Ñ5Ô5ð ñ ô €Fð ð Ø$1°TÐ$9Ð$9�Ð Ð ¸}ˆØŸš d˜Ñ+Ô+ˆˆàˆõ 	Ô"ØQÐQð	
ñ 	
ô 	
ð ñ	ð ñ		õ
  Ñ
!Ô
!ñ	"ðr,   )5r[   Úcollections.abcr   Útypingr   Úlangchain_core._apir   Úlangchain_core.agentsr   Úlangchain_core.callbacksr   Úlangchain_core.language_modelsr   Úlangchain_core.promptsr	   Úlangchain_core.prompts.chatr
   r   r   Úlangchain_core.runnablesr   r   Úlangchain_core.toolsr   Úlangchain_core.tools.renderr   Úpydanticr   Útyping_extensionsr   Úlangchain_classic.agents.agentr   r   Ú*langchain_classic.agents.format_scratchpadr   Ú'langchain_classic.agents.output_parsersr   Ú6langchain_classic.agents.structured_chat.output_parserr   Ú/langchain_classic.agents.structured_chat.promptr   r   r   Úlangchain_classic.chains.llmr   Úlangchain_classic.tools.renderr   r|   r"   Úboolry   r4   r   r'   r,   r*   ú<module>r£      s¹  ðØ 	€	€	€	Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð à *Ð *Ð *Ð *Ð *Ð *Ø -Ð -Ð -Ð -Ð -Ð -Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø <Ð <Ð <Ð <Ð <Ð <Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5ðð ð ð ð ð ð ð ð ð ð
 CÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ )Ð )Ð )Ð )Ð )Ð )Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &à CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ HÐ HÐ HÐ HÐ HÐ HØ IÐ IÐ IÐ IÐ IÐ Iðð ð ð ð ð ðð ð ð ð ð ð ð ð ð ð
 2Ð 1Ð 1Ð 1Ð 1Ð 1Ø KÐ KÐ KÐ KÐ KÐ Kà8Ð ð €ˆGÐ!?ÈÐQÑQÔQð|ð |ð |ð |ð |˜%ñ |ô |ñ RÔQð|ðF %Eð	Wð '+ðWð Wð WØ	ðWà�HÔðWð ðWð "ð	Wð ˜$˜sœ)Ñ#ðWð ðWð Wð Wð Wð Wð Wr,   