autogen_ext.code_executors.local#

class LocalCommandLineCodeExecutor(timeout: int = 60, work_dir: 路径 | str = Path('.'), functions: Sequence[FunctionWithRequirements[任何, A] | Callable[[...], 任何] | FunctionWithRequirementsStr] = [], functions_module: str = 'functions', virtual_env_context: SimpleNamespace | = None)[源代码]#

基类:CodeExecutor, Component[LocalCommandLineCodeExecutorConfig]

一个通过本地命令行环境执行代码的代码执行器类。

危险

这将在本地机器上执行代码。如果与LLM生成的代码一起使用,应谨慎操作。

每个代码块都会作为一个文件保存,并在工作目录中的一个单独进程中执行,每个代码块会生成并保存一个唯一的文件到工作目录中。 代码块按照接收的顺序执行。 命令行代码通过正则表达式与一系列危险命令进行匹配来确保安全,以防止执行可能影响用户环境的自毁命令。 目前唯一支持的语言是Python和shell脚本。 对于Python代码,请在代码块中使用“python”作为语言。 对于shell脚本,请在代码块中使用“bash”、“shell”或“sh”作为语言。

注意

在Windows上,事件循环策略必须设置为WindowsProactorEventLoopPolicy,以避免子进程出现问题。

import sys
import asyncio

if sys.platform == "win32":
    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
Parameters:
  • timeout (int) – 任何单个代码块的执行超时时间。默认值为60。

  • work_dir (str) – 代码执行的工作目录。如果为 None,将使用默认的工作目录。默认工作目录是当前目录“.”。

  • functions (列表[联合[FunctionWithRequirements[任何, A], 可调用[..., 任何]]]) – 可供代码执行器使用的函数列表。默认值为空列表。

  • functions_module (str, 可选) – 用于存储函数的模块名称。默认为“functions”。

  • virtual_env_context (Optional[SimpleNamespace], optional) – 虚拟环境上下文。默认为 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())
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[列表[str]] = ['bash', 'shell', 'sh', 'pwsh', 'powershell', 'ps1', 'python']#
classmethod _from_config(config: LocalCommandLineCodeExecutorConfig) 自我[源代码]#

从配置对象创建组件的新实例。

Parameters:

config (T) – 配置对象。

Returns:

Self – 组件的新实例。

_to_config() LocalCommandLineCodeExecutorConfig[源代码]#

导出配置,该配置将用于创建一个与此实例配置相匹配的组件新实例。

Returns:

T – 组件的配置。

component_config_schema#

LocalCommandLineCodeExecutorConfig的别名

component_provider_override: ClassVar[str | ] = 'autogen_ext.code_executors.local.LocalCommandLineCodeExecutor'#

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

async execute_code_blocks(code_blocks: 列表[CodeBlock], cancellation_token: CancellationToken) CommandLineCodeResult[源代码]#

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

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

  • cancellation_token (CancellationToken) – 用于取消操作的令牌

Returns:

CommandLineCodeResult – 代码执行的结果。

format_functions_for_prompt(prompt_template: str = FUNCTION_PROMPT_TEMPLATE) str[源代码]#

(实验性)格式化提示的函数。

模板包括两个变量: - $module_name: 模块名称。 - $functions: 作为存根格式化的函数,每个函数之间有两个换行符。

Parameters:

prompt_template (str) – 提示模板。默认为类的默认值。

Returns:

str – 格式化的提示信息。

property functions: 列表[str]#
property functions_module: str#

(实验性)函数的模块名称。

async restart() [源代码]#

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

property timeout: int#

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

property work_dir: 路径#

(实验性)代码执行的工作目录。