coding.local_commandline_code_executor
LocalCommandLineCodeExecutor
__init__
(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.
Arguments:
timeout
int - The timeout for code execution, default is 60 seconds.virtual_env_context
Optional[SimpleNamespace] - The virtual environment context to use.work_dir
Union[Path, str] - The working directory for code execution, defaults to the current directory.functions
List[Union[FunctionWithRequirements[Any, A], Callable[…, Any], FunctionWithRequirementsStr]] - A list of callable functions available to the executor.functions_module
str - The module name under which functions are accessible.execution_policies
Optional[Dict[str, bool]] - A dictionary mapping languages to execution policies (True for execution, False for saving only). Defaults to class-wide DEFAULT_EXECUTION_POLICY.
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.
Arguments:
prompt_template
str - The prompt template. Default is the class default.
Returns:
str
- The formatted prompt.
functions_module
(Experimental) The module name for the functions.
functions
(Experimental) The functions that are available to the code executor.
timeout
(Experimental) The timeout for code execution.
work_dir
(Experimental) The working directory for the code execution.
code_extractor
(Experimental) Export a code extractor that can be used by an agent.
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.
execute_code_blocks
(Experimental) Execute the code blocks and return the result.
Arguments:
code_blocks
List[CodeBlock] - The code blocks to execute.
Returns:
CommandLineCodeResult
- The result of the code execution.
restart
(Experimental) Restart the code executor.
LocalCommandlineCodeExecutor
LocalCommandlineCodeExecutor renamed to LocalCommandLineCodeExecutor
CommandlineCodeResult
CommandlineCodeResult renamed to CommandLineCodeResult