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

Python variable reloading #1637

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

orange-crow
Copy link
Contributor

@orange-crow orange-crow commented Dec 10, 2024

Features

  • 设计新的python代码执行器AsyncPyExecutor,支持python代码中object的序列化保存和载入,可以实现代码断点执行;
  • 支持bash解释器(没有进行单测)
  • 基于AsyncCodeExecutor(metagpt/tools/code_executor/async_executor.py) 可以很容易开发对R、powershell、Scala等语言解释器的支持;
  • AsyncCodeExecutor支持交互历史记录的保存(executor.json),方便跨时空的智能体间信息传递。

Result

  • 新的python代码执行器示例说明如下:
@pytest.mark.asyncio
async def test_save_code():
    executor = ExecutePyCode(True, "tests/data/code_executor/python_example")
    output, is_success = await executor.run("a=1\nb=2\nc=3\nprint(f'a={a}')")
    assert is_success
    assert "a=1" in output
    await executor.terminate()


@pytest.mark.asyncio
async def test_load_code():
    executor = ExecutePyCode(False, "tests/data/code_executor/python_example")
    executor.executor = executor.executor.load()
    output, is_success = await executor.run("d=a+b+c\nprint(f'd={d}')")  
    # 对象a,b,c是直接从tests/data/code_executor/python_example中载入,而不需要重新执行历史代码。
    assert is_success
    assert "d=6" in output
    await executor.terminate()

测试结果:单测通过
image
image

  • AsyncCodeExecutor的交互历史记录executor.json内容示例如下:
{
    "_cmd_space": {
        "0": {
            "cmd": "a=1\nb=2\nc=3\nprint(f'a={a}')\n\n",
            "stderr": [],
            "stdout": [
                "\na=1"
            ]
        }
    },
    "_executor_save_path": "tests/data/code_executor/python_example/executor.json",
    "load_obj_cmd": "load_object('{}')\n",
    "print_cmd": "print(\"{}\")\n",
    "save_obj_cmd": "save_object('{}')\n",
    "start_subprocess": [
        "python3",
        "-i",
        "-q",
        "-u",
        "-c",
        "\nimport numpy as np\nimport pandas as pd\nimport dill\nimport matplotlib.pyplot as plt\n\ndef save_object(filename):\n    variables = globals().copy()\n    filtered_variables = {name: value for name, value in variables.items() if not name.startswith('__')}\n    with open(filename, 'wb') as f:\n        dill.dump(filtered_variables, f)\n\ndef load_object(filename):\n    with open(filename, \"rb\") as f:\n        vars = dill.load(f)\n        globals().update(vars)\n"
    ],
    "work_dir": "tests/data/code_executor/python_example"
}

@orange-crow orange-crow changed the base branch from code_interpreter to main December 11, 2024 02:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants