Skip to content

Commit d473dee

Browse files
JoshTrimsonichiekzhu
authored
Add "py" as lang in conversable agent (#1062) (#2144)
* Add "py" as lang in conversable agent (#1062) * Add conditions to allow for python executable variants (#1062) * reverted import (#1062) * Parameterized tests, moved Python variants to a constant (#1062) * Moved Python variants to a constant (#1062) * Update autogen/code_utils.py (#1062) Co-authored-by: Eric Zhu <[email protected]> * Update autogen/coding/local_commandline_code_executor.py (#1062) Co-authored-by: Eric Zhu <[email protected]> * Added PYTHON_VARIANTS as imported constant (#1062) * ran pre-commit-check (#1062) --------- Co-authored-by: Chi Wang <[email protected]> Co-authored-by: Eric Zhu <[email protected]>
1 parent 812b7f9 commit d473dee

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

autogen/agentchat/conversable_agent.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .._pydantic import model_dump
1818
from ..cache.cache import AbstractCache
1919
from ..code_utils import (
20+
PYTHON_VARIANTS,
2021
UNKNOWN,
2122
check_can_use_docker_or_throw,
2223
content_str,
@@ -2079,7 +2080,7 @@ def execute_code_blocks(self, code_blocks):
20792080
)
20802081
if lang in ["bash", "shell", "sh"]:
20812082
exitcode, logs, image = self.run_code(code, lang=lang, **self._code_execution_config)
2082-
elif lang in ["python", "Python"]:
2083+
elif lang in PYTHON_VARIANTS:
20832084
if code.startswith("# filename: "):
20842085
filename = code[11 : code.find("\n")].strip()
20852086
else:

autogen/code_utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
DEFAULT_TIMEOUT = 600
3636
WIN32 = sys.platform == "win32"
3737
PATH_SEPARATOR = WIN32 and "\\" or "/"
38+
PYTHON_VARIANTS = ["python", "Python", "py"]
3839

3940
logger = logging.getLogger(__name__)
4041

@@ -244,6 +245,8 @@ def get_powershell_command():
244245

245246

246247
def _cmd(lang: str) -> str:
248+
if lang in PYTHON_VARIANTS:
249+
return "python"
247250
if lang.startswith("python") or lang in ["bash", "sh"]:
248251
return lang
249252
if lang in ["shell"]:

autogen/coding/local_commandline_code_executor.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
to_stub,
1818
)
1919

20-
from ..code_utils import TIMEOUT_MSG, WIN32, _cmd
20+
from ..code_utils import PYTHON_VARIANTS, TIMEOUT_MSG, WIN32, _cmd
2121
from .base import CodeBlock, CodeExecutor, CodeExtractor, CommandLineCodeResult
2222
from .markdown_code_extractor import MarkdownCodeExtractor
2323
from .utils import _get_file_name_from_content, silence_pip
@@ -217,6 +217,9 @@ def _execute_code_dont_check_setup(self, code_blocks: List[CodeBlock]) -> Comman
217217
LocalCommandLineCodeExecutor.sanitize_command(lang, code)
218218
code = silence_pip(code, lang)
219219

220+
if lang in PYTHON_VARIANTS:
221+
lang = "python"
222+
220223
if WIN32 and lang in ["sh", "shell"]:
221224
lang = "ps1"
222225

test/coding/test_commandline_code_executor.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
UNIX_SHELLS = ["bash", "sh", "shell"]
2525
WINDOWS_SHELLS = ["ps1", "pwsh", "powershell"]
26+
PYTHON_VARIANTS = ["python", "Python", "py"]
2627

2728

2829
@pytest.mark.parametrize("cls", classes_to_test)
@@ -60,23 +61,26 @@ def test_commandline_executor_init(cls) -> None:
6061
executor = cls(timeout=111, work_dir="/invalid/directory")
6162

6263

64+
@pytest.mark.parametrize("py_variant", PYTHON_VARIANTS)
6365
@pytest.mark.parametrize("cls", classes_to_test)
64-
def test_commandline_executor_execute_code(cls) -> None:
66+
def test_commandline_executor_execute_code(cls, py_variant) -> None:
6567
with tempfile.TemporaryDirectory() as temp_dir:
6668
executor = cls(work_dir=temp_dir)
67-
_test_execute_code(executor=executor)
69+
_test_execute_code(py_variant, executor=executor)
6870

6971

70-
def _test_execute_code(executor: CodeExecutor) -> None:
72+
@pytest.mark.parametrize("py_variant", PYTHON_VARIANTS)
73+
def _test_execute_code(py_variant, executor: CodeExecutor) -> None:
74+
7175
# Test single code block.
72-
code_blocks = [CodeBlock(code="import sys; print('hello world!')", language="python")]
76+
code_blocks = [CodeBlock(code="import sys; print('hello world!')", language=py_variant)]
7377
code_result = executor.execute_code_blocks(code_blocks)
7478
assert code_result.exit_code == 0 and "hello world!" in code_result.output and code_result.code_file is not None
7579

7680
# Test multiple code blocks.
7781
code_blocks = [
78-
CodeBlock(code="import sys; print('hello world!')", language="python"),
79-
CodeBlock(code="a = 100 + 100; print(a)", language="python"),
82+
CodeBlock(code="import sys; print('hello world!')", language=py_variant),
83+
CodeBlock(code="a = 100 + 100; print(a)", language=py_variant),
8084
]
8185
code_result = executor.execute_code_blocks(code_blocks)
8286
assert (
@@ -94,7 +98,7 @@ def _test_execute_code(executor: CodeExecutor) -> None:
9498

9599
# Test running code.
96100
file_lines = ["import sys", "print('hello world!')", "a = 100 + 100", "print(a)"]
97-
code_blocks = [CodeBlock(code="\n".join(file_lines), language="python")]
101+
code_blocks = [CodeBlock(code="\n".join(file_lines), language=py_variant)]
98102
code_result = executor.execute_code_blocks(code_blocks)
99103
assert (
100104
code_result.exit_code == 0

0 commit comments

Comments
 (0)