Home / File/ react_single_input.py — langchain Source File

react_single_input.py — langchain Source File

Architecture documentation for react_single_input.py, a python file in the langchain codebase. 6 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4["react_single_input.py"]
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7
  049d69ec_d53a_d170_b6fa_35c395793702["langchain_core.exceptions"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> 049d69ec_d53a_d170_b6fa_35c395793702
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> f85fae70_1011_eaec_151c_4083140ae9e5
  496466eb_d5c8_fece_1b1f_31541c641cdd["langchain_classic.agents.agent"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> 496466eb_d5c8_fece_1b1f_31541c641cdd
  dd9c254b_3578_9c6f_93c4_7ecff8a03657["langchain_classic.agents.mrkl.prompt"]
  6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 --> dd9c254b_3578_9c6f_93c4_7ecff8a03657
  style 6f95aa69_05b0_b1f7_29cf_f2fc2f4a69d4 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import re

from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from typing_extensions import override

from langchain_classic.agents.agent import AgentOutputParser
from langchain_classic.agents.mrkl.prompt import FORMAT_INSTRUCTIONS

FINAL_ANSWER_ACTION = "Final Answer:"
MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE = (
    "Invalid Format: Missing 'Action:' after 'Thought:'"
)
MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE = (
    "Invalid Format: Missing 'Action Input:' after 'Action:'"
)
FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE = (
    "Parsing LLM output produced both a final answer and a parse-able action:"
)


class ReActSingleInputOutputParser(AgentOutputParser):
    """Parses ReAct-style LLM calls that have a single tool input.

    Expects output to be in one of two formats.

    If the output signals that an action should be taken,
    should be in the below format. This will result in an AgentAction
    being returned.

    ```
    Thought: agent thought here
    Action: search
    Action Input: what is the temperature in SF?
    ```

    If the output signals that a final answer should be given,
    should be in the below format. This will result in an AgentFinish
    being returned.

    ```
    Thought: agent thought here
    Final Answer: The temperature is 100 degrees
    ```

    """

    @override
    def get_format_instructions(self) -> str:
        return FORMAT_INSTRUCTIONS

    @override
    def parse(self, text: str) -> AgentAction | AgentFinish:
        includes_answer = FINAL_ANSWER_ACTION in text
        regex = (
            r"Action\s*\d*\s*:[\s]*(.*?)[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)"
        )
        action_match = re.search(regex, text, re.DOTALL)
        if action_match:
            if includes_answer:
                msg = f"{FINAL_ANSWER_AND_PARSABLE_ACTION_ERROR_MESSAGE}: {text}"
                raise OutputParserException(msg)
            action = action_match.group(1).strip()
            action_input = action_match.group(2)
            tool_input = action_input.strip(" ")
            tool_input = tool_input.strip('"')

            return AgentAction(action, tool_input, text)

        if includes_answer:
            return AgentFinish(
                {"output": text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()},
                text,
            )

        if not re.search(r"Action\s*\d*\s*:[\s]*(.*?)", text, re.DOTALL):
            msg = f"Could not parse LLM output: `{text}`"
            raise OutputParserException(
                msg,
                observation=MISSING_ACTION_AFTER_THOUGHT_ERROR_MESSAGE,
                llm_output=text,
                send_to_llm=True,
            )
        if not re.search(
            r"[\s]*Action\s*\d*\s*Input\s*\d*\s*:[\s]*(.*)",
            text,
            re.DOTALL,
        ):
            msg = f"Could not parse LLM output: `{text}`"
            raise OutputParserException(
                msg,
                observation=MISSING_ACTION_INPUT_AFTER_ACTION_ERROR_MESSAGE,
                llm_output=text,
                send_to_llm=True,
            )
        msg = f"Could not parse LLM output: `{text}`"
        raise OutputParserException(msg)

    @property
    def _type(self) -> str:
        return "react-single-input"

Subdomains

Dependencies

  • langchain_classic.agents.agent
  • langchain_classic.agents.mrkl.prompt
  • langchain_core.agents
  • langchain_core.exceptions
  • re
  • typing_extensions

Frequently Asked Questions

What does react_single_input.py do?
react_single_input.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ClassicChains subdomain.
What does react_single_input.py depend on?
react_single_input.py imports 6 module(s): langchain_classic.agents.agent, langchain_classic.agents.mrkl.prompt, langchain_core.agents, langchain_core.exceptions, re, typing_extensions.
Where is react_single_input.py in the architecture?
react_single_input.py is located at libs/langchain/langchain_classic/agents/output_parsers/react_single_input.py (domain: AgentOrchestration, subdomain: ClassicChains, directory: libs/langchain/langchain_classic/agents/output_parsers).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free