Command Line Code Executor
Command line code execution is the simplest form of code execution. Generally speaking, it will save each code block to a file and the execute that file. This means that each code block is executed in a new process. There are two forms of this executor:
- Docker
(
DockerCommandLineCodeExecutor
) - this is where all commands are executed in a Docker container - Local
(
LocalCommandLineCodeExecutor
) - this is where all commands are executed on the host machine
This executor type is similar to the legacy code execution in AutoGen.
Docker
The
DockerCommandLineCodeExecutor
will create a Docker container and run all commands within that
container. The default image that is used is python:3-slim
, this can
be customized by passing the image
parameter to the constructor. If
the image is not found locally then the class will try to pull it.
Therefore, having built the image locally is enough. The only thing
required for this image to be compatible with the executor is to have
sh
and python
installed. Therefore, creating a custom image is a
simple and effective way to ensure required system dependencies are
available.
You can use the executor as a context manager to ensure the container is
cleaned up after use. Otherwise, the atexit
module will be used to
stop the container when the program exits.
Inspecting the container
If you wish to keep the container around after AutoGen is finished using
it for whatever reason (e.g. to inspect the container), then you can set
the auto_remove
parameter to False
when creating the executor.
stop_container
can also be set to False
to prevent the container
from being stopped at the end of the execution.
Example
Combining AutoGen in Docker with a Docker based executor
It is desirable to bundle your AutoGen application into a Docker image. But then, how do you allow your containerised application to execute code in a different container?
The recommended approach to this is called “Docker out of Docker”, where the Docker socket is mounted to the main AutoGen container, so that it can spawn and control “sibling” containers on the host. This is better than what is called “Docker in Docker”, where the main container runs a Docker daemon and spawns containers within itself. You can read more about this here.
To do this you would need to mount the Docker socket into the AutoGen
container. This can be done by adding the following to the docker run
command:
This will allow the AutoGen container to spawn and control sibling containers on the host.
If you need to bind a working directory to the AutoGen container but the
directory belongs to your host machine, use the bind_dir
parameter.
This will allow the main AutoGen container to bind the host directory
to the new spawned containers and allow it to access the files within
the said directory. If the bind_dir
is not specified, it will fallback
to work_dir
.
Local
To execute code on the host machine, as in the machine running AutoGen,
the
LocalCommandLineCodeExecutor
can be used.
Example
Using a Python virtual environment
By default, the LocalCommandLineCodeExecutor executes code and installs dependencies within the same Python environment as the AutoGen code. You have the option to specify a Python virtual environment to prevent polluting the base Python environment.
Example
Assigning to an agent
These executors can be used to facilitate the execution of agent written code.
When using code execution it is critical that you update the system
prompt of agents you expect to write code to be able to make use of the
executor. For example, for the
DockerCommandLineCodeExecutor
you might setup a code writing agent like so:
Then we can use these two agents to solve a problem:
Finally, stop the container. Or better yet use a context manager for it to be stopped automatically.