Skip to main content

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.
Parameters:

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:

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: Returns:

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.
Parameters: Returns:

restart

(Experimental) Restart the code executor.