microsoft / autogen

A programming framework for agentic AI 🤖
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
29.94k stars 4.36k forks source link

[Bug]: docs/tutorial/code-executors Docker Execution Doesn't Work #2300

Open oldgithubman opened 4 months ago

oldgithubman commented 4 months ago

Describe the bug

ModuleNotFoundError: No module named 'numpy'

Steps to reproduce

Follow tutorial

Model Used

False

Expected Behavior

Work

Screenshots and logs

test.py:
from autogen import ConversableAgent
from autogen.coding import DockerCommandLineCodeExecutor
import tempfile

# Create a temporary directory to store the code files.
temp_dir = tempfile.TemporaryDirectory()

# Create a Docker command line code executor.
executor = DockerCommandLineCodeExecutor(
    image="python:3.12-slim",  # Execute code using the given docker image name.
    timeout=10,  # Timeout for each code execution in seconds.
    work_dir=temp_dir.name,  # 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": executor, "cache_seed": None},  # Use the docker command line code executor.
    human_input_mode="ALWAYS",  # Always take human input for this agent for safety.
)

# When the code executor is no longer used, stop it to release the resources.
# executor.stop()
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 = np.random.randint(0, 100, 100)
y = np.random.randint(0, 100, 100)
plt.scatter(x, y)
plt.savefig('scatter.png')
print('Scatter plot saved to scatter.png')

This is the end of the message. """

Generate a reply for the given code.

reply = code_executor_agent_using_docker.generate_reply(messages=[{"role": "user", "content": message_with_code_block}]) print(reply) $ python test.py Provide feedback to the sender. Press enter to skip and use auto-reply, or type 'exit' to end the conversation:

NO HUMAN INPUT RECEIVED.

USING AUTO REPLY...

EXECUTING CODE BLOCK (inferred language is python)... exitcode: 1 (execution failed) Code output: Traceback (most recent call last): File "/workspace/tmp_code_e24bf32d4a21990fb9e4b5eb889ebe5a.py", line 1, in import numpy as np ModuleNotFoundError: No module named 'numpy'

Additional Information

AutoGen Version: 0.2.21 Operating System: Linux Mint 21.3 Cinnamon Python Version: 3.10.14

jackgerrits commented 4 months ago

The LLM generated code and didn't specify to install the numpy dep prior to running it, so when executed the code resulted in the error you see. This is a common occurrence when LLMs write code. You can try updating the system prompt to nudge it towards specifying dependencies. If you let the conversation continue usually the LLM will also catch this mistake and then respond saying to install numpy and will fix its mistake.

In the tutorial, the code block using numpy is using the local executor (the tutorial instructs you to install numpy before), because of what you saw.

If you'd like to follow this example in docker you could execute a code block which installs numpy:

code_block = """
```sh
pip install numpy

"""

oldgithubman commented 4 months ago

Maybe the documentation can be improved? This was not obvious to me