From b526232d489c4c8f76388b525f6c89c901087032 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Mon, 25 Mar 2024 01:30:22 -0700 Subject: [PATCH 1/9] Add "py" as lang in conversable agent (#1062) --- autogen/agentchat/conversable_agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index ef350f7d9e2..f54b8c81660 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -2037,7 +2037,7 @@ def execute_code_blocks(self, code_blocks): ) if lang in ["bash", "shell", "sh"]: exitcode, logs, image = self.run_code(code, lang=lang, **self._code_execution_config) - elif lang in ["python", "Python"]: + elif lang in ["python", "Python", "py"]: if code.startswith("# filename: "): filename = code[11 : code.find("\n")].strip() else: From 99a2a11426b84b169f5cc71a32106e787bd0c96f Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Tue, 26 Mar 2024 02:37:39 -0700 Subject: [PATCH 2/9] Add conditions to allow for python executable variants (#1062) --- autogen/code_utils.py | 4 ++ .../coding/local_commandline_code_executor.py | 5 +- test/coding/test_commandline_code_executor.py | 53 +++++++++++-------- 3 files changed, 38 insertions(+), 24 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 57a817855f7..ec801ccc9f3 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -10,6 +10,8 @@ from hashlib import md5 from typing import Any, Callable, Dict, List, Optional, Tuple, Union +from _pytest import python + from autogen import oai import docker @@ -244,6 +246,8 @@ def get_powershell_command(): def _cmd(lang: str) -> str: + if lang in ["Python", "py"]: + return "python" if lang.startswith("python") or lang in ["bash", "sh"]: return lang if lang in ["shell"]: diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index b927638865b..d6588a58205 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -116,6 +116,9 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe LocalCommandLineCodeExecutor.sanitize_command(lang, code) code = silence_pip(code, lang) + if lang in ["Python", "py"]: + lang = "python" + if WIN32 and lang in ["sh", "shell"]: lang = "ps1" @@ -141,7 +144,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe f.write(code) file_names.append(written_file) - program = sys.executable if lang.startswith("python") else _cmd(lang) + program = sys.executable if _cmd(lang).startswith("python") else _cmd(lang) cmd = [program, str(written_file.absolute())] try: diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index aeb62349b38..901331f7778 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -64,23 +64,29 @@ def test_commandline_executor_execute_code(cls) -> None: def _test_execute_code(executor: CodeExecutor) -> None: + + # Python executable variants + python_variants = ["python", "Python", "py"] + # Test single code block. - code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")] - code_result = executor.execute_code_blocks(code_blocks) - assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None + for py_variant in python_variants: + code_blocks = [CodeBlock(code="import sys; print('hello world!')", language=py_variant)] + code_result = executor.execute_code_blocks(code_blocks) + assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None # Test multiple code blocks. - code_blocks = [ - CodeBlock(code="import sys; print('hello world!')", language="python"), - CodeBlock(code="a = 100 + 100; print(a)", language="python"), - ] - code_result = executor.execute_code_blocks(code_blocks) - assert ( - code_result.exit_code == 0 - and "hello world!" in code_result.output - and "200" in code_result.output - and code_result.code_file is not None - ) + for py_variant in python_variants: + code_blocks = [ + CodeBlock(code="import sys; print('hello world!')", language=py_variant), + CodeBlock(code="a = 100 + 100; print(a)", language=py_variant), + ] + code_result = executor.execute_code_blocks(code_blocks) + assert ( + code_result.exit_code == 0 + and "hello world!" in code_result.output + and "200" in code_result.output + and code_result.code_file is not None + ) # Test bash script. if sys.platform not in ["win32"]: @@ -89,15 +95,16 @@ def _test_execute_code(executor: CodeExecutor) -> None: assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None # Test running code. - file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"] - code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")] - code_result = executor.execute_code_blocks(code_blocks) - assert ( - code_result.exit_code == 0 - and "hello world!" in code_result.output - and "200" in code_result.output - and code_result.code_file is not None - ) + for py_variant in python_variants: + file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"] + code_blocks = [CodeBlock(code="\n".join(file_lines), language=py_variant)] + code_result = executor.execute_code_blocks(code_blocks) + assert ( + code_result.exit_code == 0 + and "hello world!" in code_result.output + and "200" in code_result.output + and code_result.code_file is not None + ) # Check saved code file. with open(code_result.code_file) as f: From e69f9b885c868be9eeb3a1242f7ead4e17ba1c11 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Tue, 26 Mar 2024 02:45:36 -0700 Subject: [PATCH 3/9] reverted import (#1062) --- autogen/code_utils.py | 2 -- autogen/coding/local_commandline_code_executor.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index ec801ccc9f3..d59a283bedb 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -10,8 +10,6 @@ from hashlib import md5 from typing import Any, Callable, Dict, List, Optional, Tuple, Union -from _pytest import python - from autogen import oai import docker diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index d6588a58205..fc3f4d5b29f 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -144,7 +144,7 @@ def execute_code_blocks(self, code_blocks: List[CodeBlock]) -> CommandLineCodeRe f.write(code) file_names.append(written_file) - program = sys.executable if _cmd(lang).startswith("python") else _cmd(lang) + program = sys.executable if lang.startswith("python") else _cmd(lang) cmd = [program, str(written_file.absolute())] try: From 90bc393bf888e3e7553ebb8db57a9b1a5e7d0201 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Thu, 11 Apr 2024 02:08:22 -0700 Subject: [PATCH 4/9] Parameterized tests, moved Python variants to a constant (#1062) --- test/coding/test_commandline_code_executor.py | 63 +++++++++---------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index 072cb4c18ae..6085ca2d742 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -23,7 +23,7 @@ UNIX_SHELLS = ["bash", "sh", "shell"] WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"] - +PYTHON_VARIANTS = ["python", "Python", "py"] @pytest.mark.parametrize("cls", classes_to_test) def test_is_code_executor(cls) -> None: @@ -60,37 +60,33 @@ def test_commandline_executor_init(cls) -> None: executor = cls(timeout=111, work_dir="/invalid/directory") +@pytest.mark.parametrize("py_variant", PYTHON_VARIANTS) @pytest.mark.parametrize("cls", classes_to_test) -def test_commandline_executor_execute_code(cls) -> None: +def test_commandline_executor_execute_code(cls, py_variant) -> None: with tempfile.TemporaryDirectory() as temp_dir: executor = cls(work_dir=temp_dir) - _test_execute_code(executor=executor) - - -def _test_execute_code(executor: CodeExecutor) -> None: + _test_execute_code(py_variant, executor=executor) - # Python executable variants - python_variants = ["python", "Python", "py"] +@pytest.mark.parametrize("py_variant", PYTHON_VARIANTS) +def _test_execute_code(py_variant, executor: CodeExecutor) -> None: # Test single code block. - for py_variant in python_variants: - code_blocks = [CodeBlock(code="import sys; print('hello world!')", language=py_variant)] - code_result = executor.execute_code_blocks(code_blocks) - assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None + code_blocks = [CodeBlock(code="import sys; print('hello world!')", language=py_variant)] + code_result = executor.execute_code_blocks(code_blocks) + assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None # Test multiple code blocks. - for py_variant in python_variants: - code_blocks = [ - CodeBlock(code="import sys; print('hello world!')", language=py_variant), - CodeBlock(code="a = 100 + 100; print(a)", language=py_variant), - ] - code_result = executor.execute_code_blocks(code_blocks) - assert ( - code_result.exit_code == 0 - and "hello world!" in code_result.output - and "200" in code_result.output - and code_result.code_file is not None - ) + code_blocks = [ + CodeBlock(code="import sys; print('hello world!')", language=py_variant), + CodeBlock(code="a = 100 + 100; print(a)", language=py_variant), + ] + code_result = executor.execute_code_blocks(code_blocks) + assert ( + code_result.exit_code == 0 + and "hello world!" in code_result.output + and "200" in code_result.output + and code_result.code_file is not None + ) # Test bash script. if sys.platform not in ["win32"]: @@ -99,16 +95,15 @@ def _test_execute_code(executor: CodeExecutor) -> None: assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None # Test running code. - for py_variant in python_variants: - file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"] - code_blocks = [CodeBlock(code="\n".join(file_lines), language=py_variant)] - code_result = executor.execute_code_blocks(code_blocks) - assert ( - code_result.exit_code == 0 - and "hello world!" in code_result.output - and "200" in code_result.output - and code_result.code_file is not None - ) + file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"] + code_blocks = [CodeBlock(code="\n".join(file_lines), language=py_variant)] + code_result = executor.execute_code_blocks(code_blocks) + assert ( + code_result.exit_code == 0 + and "hello world!" in code_result.output + and "200" in code_result.output + and code_result.code_file is not None + ) # Check saved code file. with open(code_result.code_file) as f: From e8b3e5e6aa4ee8a6ba4fa90ab68a89f35860e001 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Thu, 11 Apr 2024 02:12:09 -0700 Subject: [PATCH 5/9] Moved Python variants to a constant (#1062) --- autogen/agentchat/conversable_agent.py | 3 ++- autogen/code_utils.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 204e57ab689..454cedfe49e 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -18,6 +18,7 @@ from ..cache.cache import AbstractCache from ..code_utils import ( UNKNOWN, + PYTHON_VARIANTS, check_can_use_docker_or_throw, content_str, decide_use_docker, @@ -2079,7 +2080,7 @@ def execute_code_blocks(self, code_blocks): ) if lang in ["bash", "shell", "sh"]: exitcode, logs, image = self.run_code(code, lang=lang, **self._code_execution_config) - elif lang in ["python", "Python", "py"]: + elif lang in PYTHON_VARIANTS: if code.startswith("# filename: "): filename = code[11 : code.find("\n")].strip() else: diff --git a/autogen/code_utils.py b/autogen/code_utils.py index 71b91d2165a..f1f0dbd247a 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -35,6 +35,7 @@ DEFAULT_TIMEOUT = 600 WIN32 = sys.platform == "win32" PATH_SEPARATOR = WIN32 and "\\" or "/" +PYTHON_VARIANTS = ["python", "Python", "py"] logger = logging.getLogger(__name__) From 2deff97938064c2ade3eeb409023522bf86804c0 Mon Sep 17 00:00:00 2001 From: Josh Trim <131500612+JoshTrim@users.noreply.github.com> Date: Thu, 11 Apr 2024 21:42:57 +0000 Subject: [PATCH 6/9] Update autogen/code_utils.py (#1062) Co-authored-by: Eric Zhu --- autogen/code_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/code_utils.py b/autogen/code_utils.py index f1f0dbd247a..aa75756e04a 100644 --- a/autogen/code_utils.py +++ b/autogen/code_utils.py @@ -245,7 +245,7 @@ def get_powershell_command(): def _cmd(lang: str) -> str: - if lang in ["Python", "py"]: + if lang in PYTHON_VARIANTS: return "python" if lang.startswith("python") or lang in ["bash", "sh"]: return lang From 0d061996f8e2c4a6bf4a48cefe4dd5b62428288b Mon Sep 17 00:00:00 2001 From: Josh Trim <131500612+JoshTrim@users.noreply.github.com> Date: Thu, 11 Apr 2024 21:43:11 +0000 Subject: [PATCH 7/9] Update autogen/coding/local_commandline_code_executor.py (#1062) Co-authored-by: Eric Zhu --- autogen/coding/local_commandline_code_executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index acdf7ff0b9d..63daab7c5a5 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -217,7 +217,7 @@ def _execute_code_dont_check_setup(self, code_blocks: List[CodeBlock]) -> Comman LocalCommandLineCodeExecutor.sanitize_command(lang, code) code = silence_pip(code, lang) - if lang in ["Python", "py"]: + if lang in PYTHON_VARIANTS: lang = "python" if WIN32 and lang in ["sh", "shell"]: From d9c0ee0d2e9faa63dd982c04db02f9c97335ef88 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Thu, 11 Apr 2024 14:47:01 -0700 Subject: [PATCH 8/9] Added PYTHON_VARIANTS as imported constant (#1062) --- autogen/coding/local_commandline_code_executor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index 63daab7c5a5..81195b246c7 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -17,7 +17,7 @@ to_stub, ) -from ..code_utils import TIMEOUT_MSG, WIN32, _cmd +from ..code_utils import TIMEOUT_MSG, WIN32, _cmd, PYTHON_VARIANTS from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult from .markdown_code_extractor import MarkdownCodeExtractor from .utils import _get_file_name_from_content, silence_pip From 0a2e67df7f4a081abbac3d4b7d1f0be0d71dc7e6 Mon Sep 17 00:00:00 2001 From: Josh Trim Date: Fri, 12 Apr 2024 01:46:50 -0700 Subject: [PATCH 9/9] ran pre-commit-check (#1062) --- autogen/agentchat/conversable_agent.py | 2 +- autogen/coding/local_commandline_code_executor.py | 2 +- test/coding/test_commandline_code_executor.py | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 454cedfe49e..c7351d4287c 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -17,8 +17,8 @@ from .._pydantic import model_dump from ..cache.cache import AbstractCache from ..code_utils import ( - UNKNOWN, PYTHON_VARIANTS, + UNKNOWN, check_can_use_docker_or_throw, content_str, decide_use_docker, diff --git a/autogen/coding/local_commandline_code_executor.py b/autogen/coding/local_commandline_code_executor.py index 81195b246c7..68ef76b7e7f 100644 --- a/autogen/coding/local_commandline_code_executor.py +++ b/autogen/coding/local_commandline_code_executor.py @@ -17,7 +17,7 @@ to_stub, ) -from ..code_utils import TIMEOUT_MSG, WIN32, _cmd, PYTHON_VARIANTS +from ..code_utils import PYTHON_VARIANTS, TIMEOUT_MSG, WIN32, _cmd from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult from .markdown_code_extractor import MarkdownCodeExtractor from .utils import _get_file_name_from_content, silence_pip diff --git a/test/coding/test_commandline_code_executor.py b/test/coding/test_commandline_code_executor.py index 6085ca2d742..a83282dec78 100644 --- a/test/coding/test_commandline_code_executor.py +++ b/test/coding/test_commandline_code_executor.py @@ -25,6 +25,7 @@ WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"] PYTHON_VARIANTS = ["python", "Python", "py"] + @pytest.mark.parametrize("cls", classes_to_test) def test_is_code_executor(cls) -> None: assert isinstance(cls, CodeExecutor) @@ -67,6 +68,7 @@ def test_commandline_executor_execute_code(cls, py_variant) -> None: executor = cls(work_dir=temp_dir) _test_execute_code(py_variant, executor=executor) + @pytest.mark.parametrize("py_variant", PYTHON_VARIANTS) def _test_execute_code(py_variant, executor: CodeExecutor) -> None: