Home / File/ react_json_single_input.py — langchain Source File

react_json_single_input.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31["react_json_single_input.py"]
  d6fe6653_f36c_cb5d_8853_46c26a36510e["json.py"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> d6fe6653_f36c_cb5d_8853_46c26a36510e
  b7996424_637b_0b54_6edf_2e18e9c1a8bf["re"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> b7996424_637b_0b54_6edf_2e18e9c1a8bf
  59e0d3b0_0f8e_4b79_d442_e9b4821561c7["langchain_core.agents"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> 59e0d3b0_0f8e_4b79_d442_e9b4821561c7
  049d69ec_d53a_d170_b6fa_35c395793702["langchain_core.exceptions"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> 049d69ec_d53a_d170_b6fa_35c395793702
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> f85fae70_1011_eaec_151c_4083140ae9e5
  496466eb_d5c8_fece_1b1f_31541c641cdd["langchain_classic.agents.agent"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> 496466eb_d5c8_fece_1b1f_31541c641cdd
  72b7abf5_fb54_8584_1910_0b2731e05128["langchain_classic.agents.chat.prompt"]
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 --> 72b7abf5_fb54_8584_1910_0b2731e05128
  style 072586ec_28fa_a8e2_c30e_f5c4c2ff9a31 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import json
import re
from re import Pattern

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.chat.prompt import FORMAT_INSTRUCTIONS

FINAL_ANSWER_ACTION = "Final Answer:"


class ReActJsonSingleInputOutputParser(AgentOutputParser):
    """Parses ReAct-style LLM calls that have a single tool input in json format.

    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:
    ```
    {
        "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
    ```

    """

    pattern: Pattern = re.compile(r"^.*?`{3}(?:json)?\n?(.*?)`{3}.*?$", re.DOTALL)
    """Regex pattern to parse the output."""

    @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
        try:
            found = self.pattern.search(text)
            if not found:
                # Fast fail to parse Final Answer.
                msg = "action not found"
                raise ValueError(msg)
            action = found.group(1)
            response = json.loads(action.strip())
            includes_action = "action" in response
            if includes_answer and includes_action:
                msg = (
                    "Parsing LLM output produced a final answer "
                    f"and a parse-able action: {text}"
                )
                raise OutputParserException(msg)
            return AgentAction(
                response["action"],
                response.get("action_input", {}),
                text,
            )

        except Exception as e:
            if not includes_answer:
                msg = f"Could not parse LLM output: {text}"
                raise OutputParserException(msg) from e
            output = text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()
            return AgentFinish({"output": output}, text)

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

Subdomains

Dependencies

  • json.py
  • langchain_classic.agents.agent
  • langchain_classic.agents.chat.prompt
  • langchain_core.agents
  • langchain_core.exceptions
  • re
  • typing_extensions

Frequently Asked Questions

What does react_json_single_input.py do?
react_json_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_json_single_input.py depend on?
react_json_single_input.py imports 7 module(s): json.py, langchain_classic.agents.agent, langchain_classic.agents.chat.prompt, langchain_core.agents, langchain_core.exceptions, re, typing_extensions.
Where is react_json_single_input.py in the architecture?
react_json_single_input.py is located at libs/langchain/langchain_classic/agents/output_parsers/react_json_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