autogen_ext.code_executors.docker#

class DockerCommandLineCodeExecutor(image: str = 'python:3-slim', container_name: str | None = None, *, timeout: int = 60, work_dir: Path | str = PosixPath('.'), 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')[source]#

Bases: CodeExecutor

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]==0.4.0.dev11'

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”, or “sh” 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 Path(“.”).

  • 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”.

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#

(Experimental) The binding directory for the code execution container.

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]#

Restart the code executor.

This method should be implemented by the code executor.

This method is called when the agent is reset.

async start() None[source]#
async stop() None[source]#

(Experimental) Stop the code executor.

property timeout: int#

(Experimental) The timeout for code execution.

property work_dir: Path#

(Experimental) The working directory for the code execution.