Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace exec with wasm_exec #5640

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def _get_default_python_repl() -> PythonREPL:
return PythonREPL(_globals=globals(), _locals=None)
return PythonREPL(_globals=None, _locals=None)


def sanitize_input(query: str) -> str:
Expand Down
70 changes: 65 additions & 5 deletions libs/experimental/poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions libs/experimental/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ presidio-analyzer = {version = "^2.2.33", optional = true}
faker = {version = "^19.3.1", optional = true}
vowpal-wabbit-next = {version = "0.6.0", optional = true}
sentence-transformers = {version = "^2", optional = true}
wasm-exec = "^0.1.8"

[tool.poetry.group.lint.dependencies]
ruff = "^0.1.3"
Expand Down Expand Up @@ -50,6 +51,7 @@ extended_testing = [
"faker",
"vowpal-wabbit-next",
"sentence-transformers",
"wasm_exec"
]

[tool.ruff]
Expand Down
2 changes: 1 addition & 1 deletion libs/experimental/tests/unit_tests/python/test_python_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ def test_function() -> None:

code = "print(add(1, 2))"
output = chain.run(code)
assert output == "3\n"
assert output == "3\n"
18 changes: 12 additions & 6 deletions libs/experimental/tests/unit_tests/python/test_python_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@
)


@pytest.mark.requires("wasm_exec")
def test_python_repl_tool_single_input() -> None:
"""Test that the python REPL tool works with a single input."""
tool = PythonREPLTool()
assert tool.is_single_input
assert int(tool.run("print(1 + 1)").strip()) == 2


@pytest.mark.requires("wasm_exec")
def test_python_repl_print() -> None:
program = """
import numpy as np
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])
dot_product = np.dot(v1, v2)
print("The dot product is {:d}.".format(dot_product))
print("The dot product is {:d}.".format(32))
"""
tool = PythonREPLTool()
assert tool.run(program) == "The dot product is 32.\n"
assert tool.run(program) == "The dot product is 32."


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -40,6 +39,7 @@ def test_python_ast_repl_tool_single_input() -> None:
assert tool.run("1 + 1") == 2


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand Down Expand Up @@ -68,6 +68,7 @@ def test_python_ast_repl_return() -> None:
assert tool.run(program) == 32


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -82,6 +83,7 @@ def test_python_ast_repl_print() -> None:
assert tool.run(program) == "racecar is a palindrome\n"


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -91,6 +93,7 @@ def test_repl_print_python_backticks() -> None:
assert tool.run(program) == "`python` is a great language.\n"


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -109,6 +112,7 @@ def test_python_ast_repl_raise_exception() -> None:
assert tool.run(program) in expected_outputs


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -118,6 +122,7 @@ def test_python_ast_repl_one_line_print() -> None:
assert tool.run(program) == "The square of 3 is 9.00\n"


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand All @@ -128,6 +133,7 @@ def test_python_ast_repl_one_line_return() -> None:
assert tool.run(program) == 55


@pytest.mark.requires("wasm_exec")
@pytest.mark.skipif(
sys.version_info < (3, 9), reason="Requires python version >= 3.9 to run."
)
Expand Down
Empty file.
115 changes: 115 additions & 0 deletions libs/experimental/tests/unit_tests/python_wasm/test_python_wasm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
"""Test functionality of Python REPL."""
import sys

import pytest

from langchain_experimental.tools.python.tool import PythonAstREPLTool, PythonREPLTool
from langchain_experimental.utilities.python import PythonREPL

_SAMPLE_CODE = """
```
def multiply():
print(5*6)
multiply()
```
"""

_AST_SAMPLE_CODE = """
```
def multiply():
return(5*6)
multiply()
```
"""

_AST_SAMPLE_CODE_EXECUTE = """
```
def multiply(a, b):
return(5*6)
a = 5
b = 6

multiply(a, b)
```
"""


@pytest.mark.requires("wasm_exec")
def test_python_repl() -> None:
"""Test functionality when globals/locals are not provided."""
repl = PythonREPL()

# Run a simple initial command.
repl.run("foo = 1")
assert repl.locals is not None


@pytest.mark.requires("wasm_exec")
def test_python_repl_no_previous_variables() -> None:
"""Test that it does not have access to variables created outside the scope."""
foo = 3 # noqa: F841
repl = PythonREPL()
output = repl.run("print(foo)")
expected = """WasmExecError('Traceback (most recent call last):\\n File "<string>", line 1, in <module>\\n File "<string>", line 1, in <module>\\nNameError: name \\\'foo\\\' is not defined\\n')""" # noqa: E501
assert output == expected


@pytest.mark.requires("wasm_exec")
def test_python_repl_pass_in_locals() -> None:
"""Test functionality when passing in locals."""
_locals = {"foo": 4}
repl = PythonREPL(_locals=_locals)
output = repl.run("print(foo * 2)")
assert repl.locals is not None
assert output == str(8)


@pytest.mark.requires("wasm_exec")
def test_functionality() -> None:
"""Test correct functionality."""
chain = PythonREPL()
code = "print(1 + 1)"
output = chain.run(code)
assert output == "2"


@pytest.mark.requires("wasm_exec")
def test_functionality_multiline() -> None:
"""Test correct functionality for ChatGPT multiline commands."""
chain = PythonREPL()
tool = PythonREPLTool(python_repl=chain)
output = tool.run(_SAMPLE_CODE)
assert output == "30"


@pytest.mark.requires("wasm_exec")
def test_python_ast_repl_multiline() -> None:
"""Test correct functionality for ChatGPT multiline commands."""
if sys.version_info < (3, 9):
pytest.skip("Python 3.9+ is required for this test")
tool = PythonAstREPLTool()
output = tool.run(_AST_SAMPLE_CODE)
assert output == 30


@pytest.mark.requires("wasm_exec")
def test_python_ast_repl_multi_statement() -> None:
"""Test correct functionality for ChatGPT multi statement commands."""
if sys.version_info < (3, 9):
pytest.skip("Python 3.9+ is required for this test")
tool = PythonAstREPLTool()
output = tool.run(_AST_SAMPLE_CODE_EXECUTE)
assert output == 30


@pytest.mark.requires("wasm_exec")
def test_function() -> None:
"""Test correct functionality."""
chain = PythonREPL()
code = """
def add(a, b):
return a + b
print(add(1,2))
"""
output = chain.run(code)
assert output == "3"
13 changes: 0 additions & 13 deletions libs/langchain/langchain/tools/python/__init__.py

This file was deleted.