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, device_requests: List[DeviceRequest] | None = None, 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]#

基类:CodeExecutor, Component[DockerCommandLineCodeExecutorConfig]

通过 Docker 容器中的命令行环境执行代码。

注意

此类别需要 `autogen-ext` 包的 `docker` 额外组件

pip install "autogen-ext[docker]"

执行器首先将每个代码块保存在工作目录的文件中,然后在容器中执行代码文件。执行器按照接收到的顺序执行代码块。目前,执行器仅支持 Python 和 shell 脚本。对于 Python 代码,代码块请使用语言“python”。对于 shell 脚本,代码块请使用语言“bash”、“shell”、“sh”、“pwsh”、“powershell”或“ps1”。

参数:
  • image (_type_, optional) – 用于代码执行的 Docker 镜像。默认为“python:3-slim”。

  • container_name (Optional[str], optional) – 创建的 Docker 容器的名称。如果为 None,将自动生成一个名称。默认为 None。

  • timeout (int, optional) – 代码执行的超时时间。默认为 60。

  • work_dir (Union[Path, str], optional) – 代码执行的工作目录。默认为临时目录。

  • bind_dir (Union[Path, str], optional) – 将绑定到代码执行器容器的目录。适用于您想在容器内从容器中

  • spawn (创建容器的情况。)

  • work_dir. (默认为工作目录。)

  • auto_remove (bool, optional) – 如果为 true,将在 Docker 容器停止时自动删除。默认为 True。

  • stop_container (bool, optional) – 如果为 true,将在调用 stop 时、上下文管理器退出时或 Python 进程以文本退出时自动停止容器。默认为 True。

  • device_requests (Optional[List[DeviceRequest]], optional) – 要添加到容器中以暴露 GPU 的设备请求实例列表(例如,[docker.types.DeviceRequest(count=-1, capabilities=[[‘gpu’]])])。默认为 None。

  • functions (List[Union[FunctionWithRequirements[Any, A], Callable[..., Any]]]) – 可供代码执行器使用的函数列表。默认为空列表。

  • functions_module (str, optional) – 将创建以存储函数的模块名称。默认为“functions”。

  • extra_volumes (Optional[Dict[str, Dict[str, str]]], optional) – 要挂载到容器的额外卷(超出 work_dir)的字典;键是宿主机源路径,值“bind”是容器路径。默认为 None。示例:extra_volumes = {'/home/user1/': {'bind': '/mnt/vol2', 'mode': 'rw'}, '/var/www': {'bind': '/mnt/vol1', 'mode': 'ro'}}

  • extra_hosts (Optional[Dict[str, str]], optional) – 要添加到容器的主机映射字典。(参见 Docker 文档中关于 extra_hosts 的内容)默认为 None。示例:extra_hosts = {"kubernetes.docker.internal": "host-gateway"}

  • init_command (Optional[str], optional) – 在每个 shell 操作执行之前运行的 shell 命令。默认为 None。示例:init_command="kubectl config use-context docker-hub"

  • delete_tmp_files (bool, optional) – 如果为 true,将在执行后删除临时文件。默认为 False。

注意

将当前目录(“.”)用作工作目录已被弃用。使用它将引发弃用警告。

component_config_schema#

别名 DockerCommandLineCodeExecutorConfig

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

覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。

SUPPORTED_LANGUAGES: ClassVar[List[str]] = ['bash', 'shell', 'sh', 'pwsh', 'powershell', 'ps1', 'python']#
FUNCTION_PROMPT_TEMPLATE: ClassVar[str] = '您可以使用以下用户定义的函数。它们可以通过函数名从名为“$module_name”的模块访问。\n\n例如,如果有一个名为“foo”的函数,您可以通过编写“from $module_name import foo”来导入它。\n\n$functions'#
property timeout: int#

(实验性) 代码执行的超时时间。

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

(实验性) 执行代码块并返回结果。

参数:

code_blocks (List[CodeBlock]) – 要执行的代码块。

返回:

CommandlineCodeResult – 代码执行的结果。

async restart() None[source]#

(实验性) 重启 Docker 容器代码执行器。

async stop() None[source]#

(实验性) 停止代码执行器。

停止 Docker 容器并清除所有临时文件(如果已创建),以及临时目录。该方法首先等待所有取消任务完成后再停止容器。最后,它将执行器标记为未运行。如果容器未运行,该方法不执行任何操作。

async start() None[source]#

(实验性) 启动代码执行器。

此方法设置工作环境变量,连接到 Docker 并启动代码执行器。如果未向代码执行器提供工作目录,它将创建一个临时目录并将其设置为代码执行器的工作目录。