-
Notifications
You must be signed in to change notification settings - Fork 307
Python code execution tool (System/Venv/Docker) #1371
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
base: main
Are you sure you want to change the base?
Conversation
Sample code: from autogen import ConversableAgent, register_function
from autogen.tools.experimental import PythonLocalExecutionTool
# Initialize the tool
python_executor = PythonLocalExecutionTool(
use_venv=True,
venv_path="/app/ag2/ms_code/.venv_ms_testing", # Use an existing virtual environment (it will create one on each call otherwise, destroying it upon completion)
timeout=60,
)
llm_config = {"model": "gpt-4o-mini", "api_type": "openai"}
# Create an agent that can use the tool
code_runner = ConversableAgent(
name="code_runner",
system_message="You are a code executor agent, when you don't execute code write the message 'TERMINATE' by itself.",
llm_config=llm_config,
)
question_agent = ConversableAgent(
name="question_agent",
system_message="You are a developer AI agent. Send all your code suggestions to the python_executor tool where it will be executed and result returned to you. Keep refining the code until it works.",
llm_config=llm_config,
)
register_function(
python_executor,
caller=question_agent,
executor=code_runner,
description="Run Python code",
)
result = code_runner.initiate_chat(
recipient=question_agent,
message="""
Write a Python code with incorrect syntax. Always install numpy and pandas.
""",
max_turns=5,
) |
We can use from asyncer import asyncify
def read_file(name: str) -> str:
with open(name) as f:
return f.read()
async def a_read_file(name: str):
content = await asyncify(read_file)(name=name)
print(content) |
Great, thanks! |
I think we should use context managers to define things like python environment and working directories. They need to have lifecycle management and that should be made explicit, not hidden in the implementational details inside This is how it could look like: from autogen import ConversableAgent, LLMConfig
from autogen.environments import PythonEnvironment, WorkingDirectory
from autogen.tools.experimental import PythonLocalExecutionTool
# create virtual env and manage its lifecycle
with PythonEnvironment(python_version="3.11", dependancies=["numpy>2.1,<3", "pandas"]):
# create working directories
with WorkingDirectory.create_tmp() as wd1, WorkingDirectory.create_tmp() as wd2:
# Initialize the tool
python_executor = PythonLocalExecutionTool(
use_venv=True,
timeout=60,
# the default working directory is from the outer scope (wd2), but you can change it explicitely
working_directory=wd1,
# it is using the python environment from the outer scope, but we could change it
# python_environment=...
)
with LLMConfig(model="gpt-4o-mini", api_type= "openai"):
# Create an agent that can use the tool
code_runner = ConversableAgent(
name="code_runner",
system_message="You are a code executor agent, when you don't execute code write the message 'TERMINATE' by itself.",
tools=python_executor,
)
question_agent = ConversableAgent(
name="question_agent",
system_message="You are a developer AI agent. Send all your code suggestions to the python_executor tool where it will be executed and result returned to you. Keep refining the code until it works.",
)
# this will be done automatically in the global run function
python_executor.register_for_execution(question_agent)
result = code_runner.initiate_chat(
recipient=question_agent,
message="""
Write a Python code with incorrect syntax. Always install numpy and pandas.
""",
max_turns=5,
) |
Updated, now have VenvPythonEnvironment and SystemPythonEnvironment for Python environments. And have WorkingDirectory for folder. SystemPythonEnvironment example with context managers:
Venv environment example:
|
@davorrunje I've updated, incorporated environments. If you can have a look over please. Also missing are tests, would like some guidance on how to test this. |
DockerPythonEnvironment example:
Output
|
Codecov ReportAttention: Patch coverage is
... and 70 files with indirect coverage changes 🚀 New features to boost your workflow:
|
Note: I want to refactor the Docker configuration parameters into two new classes, DockerExistingContainerConfig and DockerNewContainerConfig, as the |
Why are these changes needed?
Tool for code execution that utilises an environment object to determine where execution takes place. This can be the local python environment (aka System), a virtual environment (aka Venv), or a Docker container.
This will replace the need for the code execution capabilities currently in ConversableAgent. They will be useful for building agents for things like code development, code testing, etc.
Related issue number
N/A
Checks