Home / File/ test_mrkl.py — langchain Source File

test_mrkl.py — langchain Source File

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

File python CoreAbstractions MessageSchema 9 imports 13 functions

Entity Profile

Dependency Diagram

graph LR
  2d159b76_af99_d16d_9ec7_df6711f598a8["test_mrkl.py"]
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 120e2591_3e15_b895_72b6_cb26195e40a6
  80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b
  75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 75137834_4ba7_dc43_7ec5_182c05eceedf
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 43d88577_548b_2248_b01b_7987bae85dcc
  659d6546_9a06_6dc3_b830_ac7baac67a08["langchain_classic.agents.mrkl.base"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 659d6546_9a06_6dc3_b830_ac7baac67a08
  57c94080_afd4_14d9_92df_eefcc194be4b["langchain_classic.agents.mrkl.output_parser"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 57c94080_afd4_14d9_92df_eefcc194be4b
  eb534928_e458_f4bd_11f5_98615ecb3ea7["langchain_classic.agents.mrkl.prompt"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> eb534928_e458_f4bd_11f5_98615ecb3ea7
  7e88f5ce_ff41_7d87_8fb2_f355489a149e["tests.unit_tests.llms.fake_llm"]
  2d159b76_af99_d16d_9ec7_df6711f598a8 --> 7e88f5ce_ff41_7d87_8fb2_f355489a149e
  style 2d159b76_af99_d16d_9ec7_df6711f598a8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test MRKL functionality."""

import pytest
from langchain_core.agents import AgentAction
from langchain_core.exceptions import OutputParserException
from langchain_core.prompts import PromptTemplate
from langchain_core.tools import Tool

from langchain_classic.agents.mrkl.base import ZeroShotAgent
from langchain_classic.agents.mrkl.output_parser import MRKLOutputParser
from langchain_classic.agents.mrkl.prompt import FORMAT_INSTRUCTIONS, PREFIX, SUFFIX
from tests.unit_tests.llms.fake_llm import FakeLLM


def get_action_and_input(text: str) -> tuple[str, str]:
    output = MRKLOutputParser().parse(text)
    if isinstance(output, AgentAction):
        return output.tool, str(output.tool_input)
    return "Final Answer", output.return_values["output"]


def test_get_action_and_input() -> None:
    """Test getting an action from text."""
    llm_output = "Thought: I need to search for NBA\nAction: Search\nAction Input: NBA"
    action, action_input = get_action_and_input(llm_output)
    assert action == "Search"
    assert action_input == "NBA"


def test_get_action_and_input_whitespace() -> None:
    """Test getting an action from text."""
    llm_output = "Thought: I need to search for NBA\nAction: Search \nAction Input: NBA"
    action, action_input = get_action_and_input(llm_output)
    assert action == "Search"
    assert action_input == "NBA"


def test_get_action_and_input_newline() -> None:
    """Test getting an action from text where Action Input is a code snippet."""
    llm_output = (
        "Now I need to write a unittest for the function.\n\n"
        "Action: Python\nAction Input:\n```\nimport unittest\n\nunittest.main()\n```"
    )
    action, action_input = get_action_and_input(llm_output)
    assert action == "Python"
    assert action_input == "```\nimport unittest\n\nunittest.main()\n```"


def test_get_action_and_input_newline_after_keyword() -> None:
    """Test when there is a new line before the action.

    Test getting an action and action input from the text
    when there is a new line before the action
    (after the keywords "Action:" and "Action Input:").
    """
    llm_output = """
    I can use the `ls` command to list the contents of the directory \
    and `grep` to search for the specific file.

    Action:
// ... (105 more lines)

Subdomains

Dependencies

  • langchain_classic.agents.mrkl.base
  • langchain_classic.agents.mrkl.output_parser
  • langchain_classic.agents.mrkl.prompt
  • langchain_core.agents
  • langchain_core.exceptions
  • langchain_core.prompts
  • langchain_core.tools
  • pytest
  • tests.unit_tests.llms.fake_llm

Frequently Asked Questions

What does test_mrkl.py do?
test_mrkl.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in test_mrkl.py?
test_mrkl.py defines 13 function(s): get_action_and_input, test_bad_action_input_line, test_bad_action_line, test_from_chains, test_get_action_and_input, test_get_action_and_input_newline, test_get_action_and_input_newline_after_keyword, test_get_action_and_input_sql_query, test_get_action_and_input_whitespace, test_get_final_answer, and 3 more.
What does test_mrkl.py depend on?
test_mrkl.py imports 9 module(s): langchain_classic.agents.mrkl.base, langchain_classic.agents.mrkl.output_parser, langchain_classic.agents.mrkl.prompt, langchain_core.agents, langchain_core.exceptions, langchain_core.prompts, langchain_core.tools, pytest, and 1 more.
Where is test_mrkl.py in the architecture?
test_mrkl.py is located at libs/langchain/tests/unit_tests/agents/test_mrkl.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/langchain/tests/unit_tests/agents).

Analyze Your Own Codebase

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

Try Supermodel Free