InternalPythonInterpreter
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. IfNone
, 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 byeval()
orexec()
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
run
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
)
update_action_space
supported_code_types
execute
- 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
)
clear_state
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
- command (str): The command to execute.