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

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

Open
harrywang opened this issue May 8, 2024 · 2 comments

Comments

@harrywang
Copy link
Collaborator

harrywang commented May 8, 2024

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
@Gr3atWh173
Copy link
Collaborator

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.

@harrywang
Copy link
Collaborator Author

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.

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

No branches or pull requests

2 participants