autogen_ext.code_executors.docker#

class DockerCommandLineCodeExecutor(image: str = 'python:3-slim', container_name: str | None = None, *, timeout: int = 60, work_dir: Path | str | None = None, bind_dir: Path | str | None = None, auto_remove: bool = True, stop_container: bool = True, functions: Sequence[FunctionWithRequirements[Any, A] | Callable[[...], Any] | FunctionWithRequirementsStr] = [], functions_module: str = 'functions', extra_volumes: Dict[str, Dict[str, str]] | None = None, extra_hosts: Dict[str, str] | None = None, init_command: str | None = None, delete_tmp_files: bool = False)[source]#

Bases: CodeExecutor, Component[DockerCommandLineCodeExecutorConfig]

Executes code through a command line environment in a Docker container.

Note

This class requires the docker extra for the autogen-ext package:

pip install "autogen-ext[docker]"

The executor first saves each code block in a file in the working directory, and then executes the code file in the container. The executor executes the code blocks in the order they are received. Currently, the executor only supports Python and shell scripts. For Python code, use the language “python” for the code block. For shell scripts, use the language “bash”, “shell”, “sh”, “pwsh”, “powershell”, or “ps1” for the code block.

Parameters:
  • image (_type_, optional) – Docker image to use for code execution. Defaults to “python:3-slim”.

  • container_name (Optional[str], optional) – Name of the Docker container which is created. If None, will autogenerate a name. Defaults to None.

  • timeout (int, optional) – The timeout for code execution. Defaults to 60.

  • work_dir (Union[Path, str], optional) – The working directory for the code execution. Defaults to temporary directory.

  • bind_dir (Union[Path, str], optional) – The directory that will be bound

  • spawn (to the code executor container. Useful for cases where you want to)

  • work_dir. (the container from within a container. Defaults to)

  • auto_remove (bool, optional) – If true, will automatically remove the Docker container when it is stopped. Defaults to True.

  • stop_container (bool, optional) – If true, will automatically stop the container when stop is called, when the context manager exits or when the Python process exits with atext. Defaults to True.

  • functions (List[Union[FunctionWithRequirements[Any, A], Callable[..., Any]]]) – A list of functions that are available to the code executor. Default is an empty list.

  • functions_module (str, optional) – The name of the module that will be created to store the functions. Defaults to “functions”.

  • extra_volumes (Optional[Dict[str, Dict[str, str]]], optional) – A dictionary of extra volumes (beyond the work_dir) to mount to the container; key is host source path and value ‘bind’ is the container path. See Defaults to None. Example: extra_volumes = {‘/home/user1/’: {‘bind’: ‘/mnt/vol2’, ‘mode’: ‘rw’}, ‘/var/www’: {‘bind’: ‘/mnt/vol1’, ‘mode’: ‘ro’}}

  • extra_hosts (Optional[Dict[str, str]], optional) – A dictionary of host mappings to add to the container. (See Docker docs on extra_hosts) Defaults to None. Example: extra_hosts = {“kubernetes.docker.internal”: “host-gateway”}

  • init_command (Optional[str], optional) – A shell command to run before each shell operation execution. Defaults to None. Example: init_command=”kubectl config use-context docker-hub”

  • delete_tmp_files (bool, optional) – If true, will delete temporary files after execution. Defaults to False.

Note

Using the current directory (“.”) as working directory is deprecated. Using it will raise a deprecation warning.

FUNCTION_PROMPT_TEMPLATE: ClassVar[str] = 'You have access to the following user defined functions. They can be accessed from the module called `$module_name` by their function names.\n\nFor example, if there was a function called `foo` you could import it by writing `from $module_name import foo`\n\n$functions'#
SUPPORTED_LANGUAGES: ClassVar[List[str]] = ['bash', 'shell', 'sh', 'pwsh', 'powershell', 'ps1', 'python']#
property bind_dir: Path#
component_config_schema#

alias of DockerCommandLineCodeExecutorConfig

component_provider_override: ClassVar[str | None] = 'autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor'#

Override the provider string for the component. This should be used to prevent internal module names being a part of the module name.

async execute_code_blocks(code_blocks: List[CodeBlock], cancellation_token: CancellationToken) CommandLineCodeResult[source]#

(Experimental) Execute the code blocks and return the result.

Parameters:

code_blocks (List[CodeBlock]) – The code blocks to execute.

Returns:

CommandlineCodeResult – The result of the code execution.

async restart() None[source]#

(Experimental) Restart the Docker container code executor.

async start() None[source]#

(Experimental) Start the code executor.

This method sets the working environment variables, connects to Docker and starts the code executor. If no working directory was provided to the code executor, it creates a temporary directory and sets it as the code executor working directory.

async stop() None[source]#

(Experimental) Stop the code executor.

Stops the Docker container and cleans up any temporary files (if they were created), along with the temporary directory. The method first waits for all cancellation tasks to finish before stopping the container. Finally it marks the executor as not running. If the container is not running, the method does nothing.

property timeout: int#

(Experimental) The timeout for code execution.

property work_dir: Path#