autogen_ext.code_executors.local#
- class LocalCommandLineCodeExecutor(timeout: int = 60, work_dir: Path | str | None = None, functions: Sequence[FunctionWithRequirements[Any, A] | Callable[[...], Any] | FunctionWithRequirementsStr] = [], functions_module: str = 'functions', cleanup_temp_files: bool = True, virtual_env_context: SimpleNamespace | None = None)[source]#
基类:
CodeExecutor,Component[LocalCommandLineCodeExecutorConfig]一个代码执行器类,通过本地命令行环境执行代码。
危险
这将在本地机器上执行代码。如果与 LLM 生成的代码一起使用,应谨慎行事。
每个代码块都保存为文件并在工作目录中单独的进程中执行,并且为每个代码块在工作目录中生成并保存一个唯一的文件。代码块按接收顺序执行。命令行代码使用针对危险命令列表的正则表达式匹配进行清理,以防止执行可能影响用户环境的自毁命令。目前唯一支持的语言是 Python 和 shell 脚本。对于 Python 代码,代码块使用语言“python”。对于 shell 脚本,代码块使用语言“bash”、“shell”、“sh”、“pwsh”、“powershell”或“ps1”。
注意
在 Windows 上,必须将事件循环策略设置为 WindowsProactorEventLoopPolicy 以避免子进程问题。
import sys import asyncio if sys.platform == "win32": asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
- 参数:
timeout (int) – 任何单个代码块执行的超时时间。默认为 60。
work_dir (str) – 代码执行的工作目录。如果为 None,将使用默认工作目录。默认工作目录是一个临时目录。
functions (List[Union[FunctionWithRequirements[Any, A], Callable[..., Any]]]) – 可供代码执行器使用的函数列表。默认为空列表。
functions_module (str, 可选) – 将用于存储函数的模块名称。默认为“functions”。
cleanup_temp_files (bool, 可选) – 执行后是否自动清理临时文件。默认为 True。
virtual_env_context (可选[SimpleNamespace], 可选) – 虚拟环境上下文。默认为 None。
注意
将当前目录(“.”)用作工作目录已被弃用。使用它将引发弃用警告。
示例
如何将 LocalCommandLineCodeExecutor 与不同于运行 autogen 应用程序的虚拟环境一起使用:使用 venv 模块设置虚拟环境,并将其上下文传递给 LocalCommandLineCodeExecutor 的初始化程序。这样,执行器将在新环境中运行代码。
import venv from pathlib import Path import asyncio from autogen_core import CancellationToken from autogen_core.code_executor import CodeBlock from autogen_ext.code_executors.local import LocalCommandLineCodeExecutor async def example(): work_dir = Path("coding") work_dir.mkdir(exist_ok=True) venv_dir = work_dir / ".venv" venv_builder = venv.EnvBuilder(with_pip=True) venv_builder.create(venv_dir) venv_context = venv_builder.ensure_directories(venv_dir) local_executor = LocalCommandLineCodeExecutor(work_dir=work_dir, virtual_env_context=venv_context) await local_executor.execute_code_blocks( code_blocks=[ CodeBlock(language="bash", code="pip install matplotlib"), ], cancellation_token=CancellationToken(), ) asyncio.run(example())
- component_config_schema#
别名
LocalCommandLineCodeExecutorConfig
- component_provider_override: ClassVar[str | None] = 'autogen_ext.code_executors.local.LocalCommandLineCodeExecutor'#
覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- 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'#
- format_functions_for_prompt(prompt_template: str = FUNCTION_PROMPT_TEMPLATE) str[source]#
(实验性)为提示格式化函数。
模板包含两个变量:- $module_name:模块名称。- $functions:格式化为存根的函数,每个函数之间有两个换行符。
- 参数:
prompt_template (str) – 提示模板。默认为类默认值。
- 返回:
str – 格式化的提示。
- async execute_code_blocks(code_blocks: List[CodeBlock], cancellation_token: CancellationToken) CommandLineCodeResult[source]#
(实验性) 执行代码块并返回结果。
- 参数:
code_blocks (List[CodeBlock]) – 要执行的代码块。
cancellation_token (CancellationToken) – 用于取消操作的令牌
- 返回:
CommandLineCodeResult – 代码执行的结果。