autogen icon indicating copy to clipboard operation
autogen copied to clipboard

[Issue]: How to setup remote DockerCommandLineCodeExecutor for each user to execute code?

Open harrywang opened this issue 9 months ago • 1 comments

Hi, I was able to run the following code on M1 MacBook Pro with a local DockerCommandLineCodeExecutor. We are trying to build a tool that allows users to execute this using a Web APP.

For example, each user sets up the agent via Web UI and the API will execute the chat using a remote DockerCommandLineCodeExecutor (hosted on AWS or any other platform). Each user will have a separate Docker session, which will be automatically shut down after idling for some time.

I could not find any documentation on this and hope to get some insights and help here.

Thanks a lot!

import os
from dotenv import load_dotenv
from autogen import ConversableAgent
from autogen.coding import DockerCommandLineCodeExecutor

load_dotenv()  # take environment variables from .env.

llm_config={"config_list": [{
    "model": "gpt-4-turbo",
    "cache": None,
    "temperature": 0.9, 
    "api_key": os.environ.get("OPENAI_API_KEY")}]}


# Create a temporary directory to store the code files.
temp_dir = './tmp'

docker_container_name = 'autogen'

docker_executor = DockerCommandLineCodeExecutor(
    image="python:3.12-slim",  # Execute code using the given docker image name.
    container_name=docker_container_name,  # Name of the Docker container.
    timeout=180,  # Timeout for each code execution in seconds - 3 minutes
    work_dir=temp_dir,  # Use the temporary directory to store the code files.
)

# Create an agent with code executor configuration that uses docker.
code_executor_agent_using_docker = ConversableAgent(
    "code_executor_agent_docker",
    llm_config=False,  # Turn off LLM for this agent.
    code_execution_config={"executor": docker_executor},  # Use the docker command line code executor.
    human_input_mode="NEVER",  # Change to ALWAYS to take human input for this agent for safety.
)

message_with_code_block = """This is a message with code block.
The code block is below:
```shell
pip install matplotlib numpy
```
This is the end of the message.
"""

reply = code_executor_agent_using_docker.generate_reply(messages=[{"role": "user", "content": message_with_code_block}])
print(reply)


message_with_code_block = """This is a message with code block.
The code block is below:
```python
import numpy as np
import matplotlib.pyplot as plt
x = range(100)
y = np.random.randint(0, 100, 100)
plt.plot(x, y)
plt.savefig('line.png')
print('Scatter plot saved to line.png')
```
This is the end of the message.
"""

reply = code_executor_agent_using_docker.generate_reply(messages=[{"role": "user", "content": message_with_code_block}])
print(reply)

The result on local Mac is:

EXECUTING CODE BLOCK (inferred language is shell)...
exitcode: 0 (execution succeeded)
Code output: 

EXECUTING CODE BLOCK (inferred language is python)...
exitcode: 0 (execution succeeded)
Code output: Scatter plot saved to line.png

harrywang avatar May 08 '24 18:05 harrywang

So you're trying to execute an entire chat inside of a docker container? I don't think thats possible. The executor here only runs the generated code in docker, not the entire chat. In other words, the executor lives inside the agent, not the other way around.

Gr3atWh173 avatar May 09 '24 14:05 Gr3atWh173

So you're trying to execute an entire chat inside of a docker container? I don't think thats possible. The executor here only runs the generated code in docker, not the entire chat. In other words, the executor lives inside the agent, not the other way around.

@Gr3atWh173 thanks for the reply. But "execute an entire chat inside of a docker container" is not what I want to do. Instead, I asked how to setup a separate docker in the cloud for each user to run his/her generated code. Hope this can help.

harrywang avatar May 20 '24 23:05 harrywang

The autogen Docker command line executor uses docker.from_env() to get the client, so could you specify the DOCKER_HOST, DOCKER_TLS_VERIFY and DOCKER_CERT_PATH environment variables? Then you can point it at whichever client you want i.e. not just local. Or subclass it and write your own implementation instead of using the environment variables.

edjeffery avatar Jun 06 '24 08:06 edjeffery

The next question is how do you share the code between the user/coder agent and the remote docker container... Any help appreciated there.

edjeffery avatar Jun 06 '24 10:06 edjeffery