Skip to main content

InternalPythonInterpreter

A customized python interpreter to control the execution of LLM-generated codes. The interpreter makes sure the code can only execute functions given in action space and import white list. It also supports fuzzy variable matching to retrieve uncertain input variable name. .. highlight:: none This class is adapted from the hugging face implementation [python_interpreter.py](https://github.com/huggingface/transformers/blob/8f 093fb799246f7dd9104ff44728da0c53a9f67a/src/transformers/tools/python_interp reter.py). The original license applies:: Copyright 2023 The HuggingFace Inc. team. All rights reserved. Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. We have modified the original code to suit our requirements. We have encapsulated the original functions within a class and saved the interpreter state after execution. We have added support for “import” statements, “for” statements, and several binary and unary operators. We have added import white list to keep import statement safe. Additionally, we have modified the variable matching logic and introduced the :obj:fuzz_state for fuzzy matching. Modifications copyright (C) 2023 CAMEL-AI.org Parameters:
  • action_space (Dict[str, Any], optional): A dictionary that maps action names to their corresponding functions or objects. The interpreter can only execute functions that are either directly listed in this dictionary or are member functions of objects listed in this dictionary. The concept of :obj:action_space is derived from EmbodiedAgent, representing the actions that an agent is capable of performing. If None, set to empty dict. (default: :obj:None)
  • import_white_list (List[str], optional): A list that stores the Python modules or functions that can be imported in the code. All submodules and functions of the modules listed in this list are importable. Any other import statements will be rejected. The module and its submodule or function name are separated by a period (:obj:.). (default: :obj:None)
  • unsafe_mode (bool, optional): If True, the interpreter runs the code by eval() or exec() without any security check. (default: :obj:False)
  • raise_error (bool, optional): Raise error if the interpreter fails. (default: :obj:False)
  • allow_builtins (bool, optional): If True, safe built-in functions like print, len, str, etc. are added to the action space. (default: :obj:True)

init

_add_safe_builtins

Add safe built-in functions to the action space.

run

Executes the given code with specified code type in the interpreter. This method takes a string of code and its type, checks if the code type is supported, and then executes the code. If unsafe_mode is set to False, the code is executed in a controlled environment using the execute method. If unsafe_mode is True, the code is executed using eval() or exec() with the action space as the global context. An InterpreterError is raised if the code type is unsupported or if any runtime error occurs during execution. Parameters:
  • code (str): The python code to be executed.
  • code_type (str): The type of the code, which should be one of the supported code types (python, py, python3, python2). (default: obj:python)
Returns: str: The string representation of the output of the executed code.

update_action_space

Updates action space for python interpreter.

supported_code_types

Provides supported code types by the interpreter.

execute

Execute the input python codes in a security environment. Parameters:
  • code (str): Generated python code to be executed.
  • state (Optional[Dict[str, Any]], optional): External variables that may be used in the generated code. (default: :obj:None)
  • fuzz_state (Optional[Dict[str, Any]], optional): External variables that do not have certain variable names. The interpreter will use fuzzy matching to access these variables. For example, if :obj:fuzz_state has a variable :obj:image, the generated code can use :obj:input_image to access it. (default: :obj:None)
  • keep_state (bool, optional): If :obj:True, :obj:state and :obj:fuzz_state will be kept for later execution. Otherwise, they will be cleared. (default: :obj:True)
Returns: Any: The value of the last statement (excluding “import”) in the code. For this interpreter, the value of an expression is its value, the value of an “assign” statement is the assigned value, and the value of an “if” and “for” block statement is the value of the last statement in the block.

clear_state

Initialize :obj:state and :obj:fuzz_state.

_execute_ast

_execute_assign

_assign

_execute_call

_execute_subscript

_execute_name

_execute_condition

_execute_if

_execute_for

_execute_import

_execute_import_from

_validate_import

_execute_binop

_execute_unaryop

_get_value_from_state

execute_command

Execute a command in the internal python interpreter. Parameters:
  • command (str): The command to execute.
Returns: tuple: A tuple containing the stdout and stderr of the command.