Home / File/ test_llm_math.py — langchain Source File

test_llm_math.py — langchain Source File

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

Entity Profile

Dependency Diagram

graph LR
  577ff46a_eb9e_bca2_4f2b_01f091f6304b["test_llm_math.py"]
  120e2591_3e15_b895_72b6_cb26195e40a6["pytest"]
  577ff46a_eb9e_bca2_4f2b_01f091f6304b --> 120e2591_3e15_b895_72b6_cb26195e40a6
  8398c36b_4ebe_8366_d107_b25604da9f7a["langchain_classic.chains.llm_math.base"]
  577ff46a_eb9e_bca2_4f2b_01f091f6304b --> 8398c36b_4ebe_8366_d107_b25604da9f7a
  2da0aba2_cc3c_3722_a654_b96319e29c26["langchain_classic.chains.llm_math.prompt"]
  577ff46a_eb9e_bca2_4f2b_01f091f6304b --> 2da0aba2_cc3c_3722_a654_b96319e29c26
  7e88f5ce_ff41_7d87_8fb2_f355489a149e["tests.unit_tests.llms.fake_llm"]
  577ff46a_eb9e_bca2_4f2b_01f091f6304b --> 7e88f5ce_ff41_7d87_8fb2_f355489a149e
  style 577ff46a_eb9e_bca2_4f2b_01f091f6304b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test LLM Math functionality."""

import pytest

from langchain_classic.chains.llm_math.base import LLMMathChain
from langchain_classic.chains.llm_math.prompt import _PROMPT_TEMPLATE
from tests.unit_tests.llms.fake_llm import FakeLLM


@pytest.fixture
def fake_llm_math_chain() -> LLMMathChain:
    """Fake LLM Math chain for testing."""
    complex_question = _PROMPT_TEMPLATE.format(question="What is the square root of 2?")
    queries = {
        _PROMPT_TEMPLATE.format(question="What is 1 plus 1?"): "Answer: 2",
        complex_question: "```text\n2**.5\n```",
        _PROMPT_TEMPLATE.format(question="foo"): "foo",
    }
    fake_llm = FakeLLM(queries=queries)
    return LLMMathChain.from_llm(fake_llm, input_key="q", output_key="a")


@pytest.mark.requires("numexpr")
def test_simple_question(fake_llm_math_chain: LLMMathChain) -> None:
    """Test simple question that should not need python."""
    question = "What is 1 plus 1?"
    output = fake_llm_math_chain.run(question)
    assert output == "Answer: 2"


@pytest.mark.requires("numexpr")
def test_complex_question(fake_llm_math_chain: LLMMathChain) -> None:
    """Test complex question that should need python."""
    question = "What is the square root of 2?"
    output = fake_llm_math_chain.run(question)
    assert output == f"Answer: {2**0.5}"


@pytest.mark.requires("numexpr")
def test_error(fake_llm_math_chain: LLMMathChain) -> None:
    """Test question that raises error."""
    with pytest.raises(ValueError, match="unknown format from LLM: foo"):
        fake_llm_math_chain.run("foo")

Subdomains

Dependencies

  • langchain_classic.chains.llm_math.base
  • langchain_classic.chains.llm_math.prompt
  • pytest
  • tests.unit_tests.llms.fake_llm

Frequently Asked Questions

What does test_llm_math.py do?
test_llm_math.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in test_llm_math.py?
test_llm_math.py defines 4 function(s): fake_llm_math_chain, test_complex_question, test_error, test_simple_question.
What does test_llm_math.py depend on?
test_llm_math.py imports 4 module(s): langchain_classic.chains.llm_math.base, langchain_classic.chains.llm_math.prompt, pytest, tests.unit_tests.llms.fake_llm.
Where is test_llm_math.py in the architecture?
test_llm_math.py is located at libs/langchain/tests/unit_tests/chains/test_llm_math.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/tests/unit_tests/chains).

Analyze Your Own Codebase

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

Try Supermodel Free