LocalCommandLineCodeExecutor

LocalCommandLineCodeExecutor(
    timeout: int = 60,
    virtual_env_context: types.SimpleNamespace | None = None,
    work_dir: pathlib.Path | str = PosixPath('.'),
    functions: list[autogen.coding.func_with_reqs.FunctionWithRequirements[typing.Any, ~A] | Callable[..., Any] | autogen.coding.func_with_reqs.FunctionWithRequirementsStr] = [],
    functions_module: str = 'functions',
    execution_policies: dict[str, bool] | None = None
)

(Experimental) A code executor class that executes code blocks and returns the result.

(Experimental) A code executor class that executes or saves LLM generated code a local command line environment.

This will execute or save LLM generated code on the local machine.

Each code block is saved as a file in the working directory. Depending on the execution policy, the code may be executed in a separate process. The code blocks are executed or save in the order they are received. Command line code is sanitized against a list of dangerous commands to prevent self-destructive commands from being executed, which could potentially affect the user’s environment. Supported languages include Python, shell scripts (bash, shell, sh), PowerShell (pwsh, powershell, ps1), HTML, CSS, and JavaScript. Execution policies determine whether each language’s code blocks are executed or saved only.

Execution with a Python virtual environment

A python virtual env can be used to execute code and install dependencies. This has the added benefit of not polluting the base environment with unwanted modules.

from autogen.code_utils import create_virtual_env
from autogen.coding import LocalCommandLineCodeExecutor

venv_dir = ".venv"
venv_context = create_virtual_env(venv_dir)

executor = LocalCommandLineCodeExecutor(virtual_env_context=venv_context)
Parameters:
NameDescription
timeoutThe timeout for code execution, default is 60 seconds.

Type: int

Default: 60
virtual_env_contextThe virtual environment context to use.

Type: types.SimpleNamespace | None

Default: None
work_dirThe working directory for code execution, defaults to the current directory.

Type: pathlib.Path | str

Default: PosixPath(’.’)
functionsA list of callable functions available to the executor.

Type: list[autogen.coding.func_with_reqs.FunctionWithRequirements[typing.Any, ~A] | Callable[..., Any] | autogen.coding.func_with_reqs.FunctionWithRequirementsStr]

Default: []
functions_moduleThe module name under which functions are accessible.

Type: str

Default: ‘functions’
execution_policiesA dictionary mapping languages to execution policies (True for execution, False for saving only).

Defaults to class-wide DEFAULT_EXECUTION_POLICY.

Type: dict[str, bool] | None

Default: None

Class Attributes

DEFAULT_EXECUTION_POLICY



FUNCTION_PROMPT_TEMPLATE



SUPPORTED_LANGUAGES



Static Methods

sanitize_command

sanitize_command(lang: str, code: str) -> None

Sanitize the code block to prevent dangerous commands. This approach acknowledges that while Docker or similar containerization/sandboxing technologies provide a robust layer of security, not all users may have Docker installed or may choose not to use it. Therefore, having a baseline level of protection helps mitigate risks for users who, either out of choice or necessity, run code outside of a sandboxed environment.

Parameters:
NameDescription
langType: str
codeType: str

Instance Attributes

code_extractor


(Experimental) Export a code extractor that can be used by an agent.

functions


(Experimental) The functions that are available to the code executor.

functions_module


(Experimental) The module name for the functions.

timeout


(Experimental) The timeout for code execution.

work_dir


(Experimental) The working directory for the code execution.

Instance Methods

execute_code_blocks

execute_code_blocks(self, code_blocks: list[autogen.coding.CodeBlock]) -> autogen.coding.base.CommandLineCodeResult

(Experimental) Execute the code blocks and return the result.

Parameters:
NameDescription
code_blocksThe code blocks to execute.

Type: list[autogen.coding.CodeBlock]
Returns:
TypeDescription
autogen.coding.base.CommandLineCodeResultCommandLineCodeResult: The result of the code execution.

format_functions_for_prompt

format_functions_for_prompt(self, 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

(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:
NameDescription
prompt_templateThe prompt template.

Default is the class default.

Type: str

Default: ‘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’
Returns:
TypeDescription
strstr: The formatted prompt.

restart

restart(self) -> None

(Experimental) Restart the code executor.