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

Add "py" as lang in conversable agent (#1062) #2144

Merged
merged 10 commits into from
Apr 12, 2024
2 changes: 1 addition & 1 deletion autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,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"]:
ekzhu marked this conversation as resolved.
Show resolved Hide resolved
if code.startswith("# filename: "):
filename = code[11 : code.find("\n")].strip()
else:
Expand Down
2 changes: 2 additions & 0 deletions autogen/code_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def get_powershell_command():


def _cmd(lang: str) -> str:
if lang in ["Python", "py"]:
JoshTrim marked this conversation as resolved.
Show resolved Hide resolved
return "python"
if lang.startswith("python") or lang in ["bash", "sh"]:
return lang
if lang in ["shell"]:
Expand Down
3 changes: 3 additions & 0 deletions autogen/coding/local_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ 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"]:
JoshTrim marked this conversation as resolved.
Show resolved Hide resolved
lang = "python"

if WIN32 and lang in ["sh", "shell"]:
lang = "ps1"

Expand Down
53 changes: 30 additions & 23 deletions test/coding/test_commandline_code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,29 @@ def test_commandline_executor_execute_code(cls) -> None:


def _test_execute_code(executor: CodeExecutor) -> None:
ekzhu marked this conversation as resolved.
Show resolved Hide resolved

# 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"]:
Expand All @@ -93,15 +99,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:
Expand Down
Loading