autogen.coding.LocalCommandLineCodeExecutor
LocalCommandLineCodeExecutor
(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.
Name | Description |
---|---|
timeout | The timeout for code execution, default is 60 seconds. Type: int Default: 60 |
virtual_env_context | The virtual environment context to use. Type: types.SimpleNamespace | None Default: None |
work_dir | The working directory for code execution, defaults to the current directory. Type: pathlib.Path | str Default: PosixPath(’.’) |
functions | A 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_module | The module name under which functions are accessible. Type: str Default: ‘functions’ |
execution_policies | A 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 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:Name | Description |
---|---|
lang | Type: str |
code | Type: 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
(Experimental) Execute the code blocks and return the result.
Parameters:Name | Description |
---|---|
code_blocks | The code blocks to execute. Type: list[autogen.coding.CodeBlock] |
Type | Description |
---|---|
autogen.coding.base.CommandLineCodeResult | CommandLineCodeResult: The result of the code execution. |
format_functions_for_prompt
(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.
Name | Description |
---|---|
prompt_template | The 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’ |
Type | Description |
---|---|
str | str: The formatted prompt. |
restart
(Experimental) Restart the code executor.