autogen_ext.code_executors.local#
- class LocalCommandLineCodeExecutor(timeout: int = 60, work_dir: Path | str = PosixPath('.'), functions: Sequence[FunctionWithRequirements[Any, A] | Callable[[...], Any] | FunctionWithRequirementsStr] = [], functions_module: str = 'functions', virtual_env_context: SimpleNamespace | None = None)[source]#
Bases:
CodeExecutor
A code executor class that executes code through a local command line environment.
Danger
This will execute code on the local machine. If being used with LLM generated code, caution should be used.
Each code block is saved as a file and executed in a separate process in the working directory, and a unique file is generated and saved in the working directory for each code block. The code blocks are executed in the order they are received. Command line code is sanitized using regular expression match against a list of dangerous commands in order to prevent self-destructive commands from being executed which may potentially affect the users environment. Currently the only supported languages is 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:
timeout (int) – The timeout for the execution of any single code block. Default is 60.
work_dir (str) – The working directory for the code execution. If None, a default working directory will be used. The default working directory is the current directory “.”.
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”.
virtual_env_context (Optional[SimpleNamespace], optional) – The virtual environment context. Defaults to None.
Example:
How to use LocalCommandLineCodeExecutor with a virtual environment different from the one used to run the autogen application: Set up a virtual environment using the venv module, and pass its context to the initializer of LocalCommandLineCodeExecutor. This way, the executor will run code within the new environment.
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[List[str]] = ['bash', 'shell', 'sh', 'pwsh', 'powershell', 'ps1', 'python']#
- 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.
cancellation_token (CancellationToken) – a token to cancel the operation
- Returns:
CommandLineCodeResult – The result of the code execution.
- format_functions_for_prompt(prompt_template: 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') str [source]#
(Experimental) Format the functions for a prompt.
The template includes two variables: - $module_name: The module name. - $functions: The functions formatted as stubs with two newlines between each function.
- Parameters:
prompt_template (str) – The prompt template. Default is the class default.
- Returns:
str – The formatted prompt.