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: str | Path | 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)[源代码]#
基类:
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_, 可选) -- 用于代码执行的 Docker 镜像。 默认为 "python:3-slim"。
container_name (Optional[str], 可选) -- 创建的 Docker 容器名称。 如果为 None,将自动生成名称。默认为 None。
timeout (int, 可选) -- 代码执行的超时时间。默认为 60。
work_dir (Union[Path, str], 可选) -- 代码执行的工作目录。 默认为临时目录。
bind_dir (Union[Path, str], 可选) -- 将绑定到代码执行器容器的目录。 适用于需要从容器内部生成容器的情况。默认为 work_dir。
auto_remove (bool, 可选) -- 如果为 True,容器停止时将自动移除。默认为 True。
stop_container (bool, 可选) -- 如果为 True,在调用 stop 时、上下文管理器退出时或 Python 进程退出时,将自动停止容器。默认为 True。
device_requests (Optional[List[DeviceRequest]], 可选) -- 用于向容器添加设备请求的列表, 以暴露 GPU(例如 [docker.types.DeviceRequest(count=-1, capabilities=[['gpu']])])。默认为 None。
functions (List[Union[FunctionWithRequirements[Any, A], Callable[..., Any]]]) -- 可供代码执行器使用的函数列表。默认为空列表。
functions_module (str, 可选) -- 用于存储函数的模块名称。默认为 "functions"。
extra_volumes (Optional[Dict[str, Dict[str, str]]], 可选) -- 要挂载到容器的额外卷字典(除 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]], 可选) -- 要添加到容器的主机映射字典。(参见 Docker 文档中的 extra_hosts)默认为 None。 示例: extra_hosts = {"kubernetes.docker.internal": "host-gateway"}
init_command (Optional[str], 可选) -- 每次 shell 操作执行前运行的 shell 命令。默认为 None。 示例: init_command="kubectl config use-context docker-hub"
delete_tmp_files (bool, 可选) -- 如果为 True,执行后将删除临时文件。默认为 False。
备注
使用当前目录 (".") 作为工作目录已被弃用。使用它将引发弃用警告。
- 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']#
- component_config_schema#
DockerCommandLineCodeExecutorConfig
的别名
- component_provider_override: ClassVar[str | None] = 'autogen_ext.code_executors.docker.DockerCommandLineCodeExecutor'#
覆盖组件的provider字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- async execute_code_blocks(code_blocks: List[CodeBlock], cancellation_token: CancellationToken) CommandLineCodeResult [源代码]#
(实验性) 执行代码块并返回结果。
- 参数:
code_blocks (List[CodeBlock]) -- 要执行的代码块列表。
- Returns:
CommandlineCodeResult -- 代码执行的结果。
- async start() None [源代码]#
(实验性) 启动代码执行器。
该方法设置工作环境变量,连接 Docker 并启动代码执行器。 如果未向代码执行器提供工作目录,则会创建一个临时目录并将其设为代码执行器的工作目录。