camel.interpreters 包

本页内容

camel.interpreters 包#

子模块#

camel.interpreters.base 模块#

class camel.interpreters.base.BaseInterpreter[来源]#

基类: ABC

代码解释器的抽象基类。

abstract run(code: str, code_type: str) str[来源]#

根据代码类型执行给定的代码。

Parameters:
  • code (str) – 要执行的代码。

  • code_type (str) - 代码类型,必须是supported_code_types()返回的类型之一。

Returns:

代码执行的结果。如果执行失败,此结果

应包含足够的信息以便诊断和纠正问题。

Return type:

字符串

Raises:

InterpreterError – 如果代码执行遇到可以通过修改或重新生成代码来解决的错误。

abstract supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

abstract update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

camel.interpreters.docker_interpreter 模块#

class camel.interpreters.docker_interpreter.DockerInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True)[来源]#

基类: BaseInterpreter

一个用于在docker容器中执行代码文件或代码字符串的类。

该类负责在Docker容器中执行不同脚本语言(目前支持Python和Bash)的代码,捕获它们的标准输出和错误流,并允许用户在执行代码字符串前进行检查。

Parameters:
  • require_confirm (bool, optional) – 如果为True,则在运行代码字符串前会提示用户确认以确保安全。默认为True

  • print_stdout (bool, optional) – 如果为True,则打印执行代码的标准输出。默认为False

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。默认为True

run(code: str, code_type: str) str[来源]#

在连接到解释器的容器中执行给定代码,并捕获标准输出和标准错误流。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

Raises:

InterpreterError – 如果用户拒绝运行代码,或代码类型不受支持,或docker API/容器中存在错误

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

camel.interpreters.internal_python_interpreter 模块#

class camel.interpreters.internal_python_interpreter.InternalPythonInterpreter(action_space: Dict[str, Any] | None = None, import_white_list: List[str] | None = None, unsafe_mode: bool = False, raise_error: bool = False)[来源]#

基类: BaseInterpreter

一个定制化的Python解释器,用于控制LLM生成代码的执行。该解释器确保代码只能执行动作空间中给定的函数和导入白名单中的模块。它还支持模糊变量匹配,以检索不确定的输入变量名。

该类改编自hugging face的实现 python_interpreter.py。原始许可证适用:

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.

我们已对原始代码进行了修改以满足需求。我们将原有函数封装在类中,并在执行后保存了解释器状态。我们新增了对"import"语句、"for"循环语句以及多个二元和一元运算符的支持。我们添加了导入白名单机制来确保import语句的安全性。此外,我们还修改了变量匹配逻辑,并引入了fuzz_state来实现模糊匹配。

修改版权 (C) 2023 CAMEL-AI.org

Parameters:
  • action_space (Dict[str, Any], optional) – 一个将动作名称映射到对应函数或对象的字典。解释器只能执行直接列在此字典中或是此字典中列出对象的成员函数。action_space的概念源自EmbodiedAgent,表示代理能够执行的动作。如果为None,则设置为空字典。(默认值: None)

  • import_white_list (List[str], optional) – 一个存储可在代码中导入的Python模块或函数的列表。该列表中列出的模块的所有子模块和函数都是可导入的。任何其他导入语句都将被拒绝。模块与其子模块或函数名之间用点号()分隔。(默认值: None)

  • unsafe_mode (bool, optional) – 如果为True,解释器将通过eval()exec()运行代码而不进行任何安全检查。 (默认值: False)

  • raise_error (bool, optional) – 如果解释器失败则抛出错误。 (默认: False)

clear_state() None[来源]#

初始化 statefuzz_state

execute(code: str, state: Dict[str, Any] | None = None, fuzz_state: Dict[str, Any] | None = None, keep_state: bool = True) Any[来源]#

在安全环境中执行输入的Python代码。

Parameters:
  • code (str) – 要执行的生成python代码。

  • state (Optional[Dict[str, Any]], optional) – 可能在生成代码中使用的外部变量。(默认值: None)

  • fuzz_state (可选[Dict[str, Any]], 可选) - 外部变量 这些变量没有特定的变量名称。解释器将 使用模糊匹配来访问这些变量。例如,如果 fuzz_state 有一个变量 image,生成的 代码可以使用 input_image 来访问它。(默认值: None)

  • keep_state (bool, optional) – 如果设为True,则statefuzz_state会被保留以供后续执行。否则, 它们将被清除。(默认值: True)

Returns:

代码中最后一条语句的值(不包括"import")

对于这个解释器来说,表达式的值就是它的值,"assign"语句的值是被赋的值,"if"和"for"块语句的值是该块中最后一条语句的值。

Return type:

任何

run(code: str, code_type: str) str[来源]#

在解释器中执行指定代码类型的给定代码。

该方法接收一段代码字符串及其类型,检查代码类型是否受支持,然后执行代码。如果unsafe_mode设置为False,代码将在受控环境中使用execute方法执行。如果unsafe_modeTrue,则使用eval()exec()以动作空间作为全局上下文执行代码。如果代码类型不受支持或执行过程中发生任何运行时错误,将引发InterpreterError异常。

Parameters:
  • code (str) – 要执行的python代码。

  • code_type (str) – 代码类型,应为以下之一

  • types (支持的代码)

Returns:

执行代码输出的字符串表示形式。

Return type:

字符串

Raises:

InterpreterError – 如果code_type不被支持,或者在代码执行过程中发生任何运行时错误。

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间。

camel.interpreters.interpreter_error 模块#

exception camel.interpreters.interpreter_error.InterpreterError[来源]#

基类: Exception

因可通过重新生成代码解决的错误而引发的异常

camel.interpreters.ipython_interpreter 模块#

class camel.interpreters.ipython_interpreter.JupyterKernelInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True)[来源]#

基类: BaseInterpreter

一个用于在Jupyter内核中执行代码字符串的类。

Parameters:
  • require_confirm (bool, optional) – 如果为True,则在运行代码字符串前会提示用户确认以确保安全。默认为True

  • print_stdout (bool, optional) – 如果为True,则打印执行代码的标准输出。默认为False

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。默认为True

run(code: str, code_type: str) str[来源]#

在Jupyter内核中执行给定的代码。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个包含捕获结果的字符串

已执行的代码。

Return type:

字符串

Raises:

InterpreterError – 执行代码时发生错误。

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

Returns:

支持的代码类型。

Return type:

List[str]

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新解释器的动作空间。

Parameters:

action_space (Dict[str, Any]) – 一个字典,表示 新的或更新后的动作空间。

Raises:

RuntimeError – 总是会抛出,因为JupyterKernelInterpreter不支持更新动作空间。

camel.interpreters.subprocess_interpreter 模块#

class camel.interpreters.subprocess_interpreter.SubprocessInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True, execution_timeout: int = 60)[来源]#

基类: BaseInterpreter

SubprocessInterpreter 是一个用于在子进程中执行代码文件或代码字符串的类。

该类负责处理在不同脚本语言(当前支持Python和Bash)中子进程的代码执行,捕获它们的标准输出和错误流,并允许用户在执行代码字符串前进行检查。

Parameters:
  • require_confirm (bool, optional) – 如果为True,出于安全考虑会在运行代码字符串前提示用户确认。(默认值: True)

  • print_stdout (bool, optional) – 如果为True,则打印执行代码的标准输出。(默认: False)

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。(默认: True)

  • execution_timeout (int, optional) – 等待代码执行完成的最大时间,单位为秒。(默认值: 60)

run(code: str, code_type: str) str[来源]#
Generates a temporary file with the given code, executes it, and

随后删除该文件。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

Raises:

InterpreterError – 如果用户拒绝运行代码或代码类型不受支持时。

run_file(file: Path, code_type: str) str[来源]#

在子进程中执行代码文件并捕获其输出。

Parameters:
  • file (Path) - 要运行的文件路径对象。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

模块内容#

class camel.interpreters.BaseInterpreter[来源]#

基类: ABC

代码解释器的抽象基类。

abstract run(code: str, code_type: str) str[来源]#

根据代码类型执行给定的代码。

Parameters:
  • code (str) – 要执行的代码。

  • code_type (str) - 代码类型,必须是supported_code_types()返回的类型之一。

Returns:

代码执行的结果。如果执行失败,此结果

应包含足够的信息以便诊断和纠正问题。

Return type:

字符串

Raises:

InterpreterError – 如果代码执行遇到可以通过修改或重新生成代码来解决的错误。

abstract supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

abstract update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

class camel.interpreters.DockerInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True)[来源]#

基类: BaseInterpreter

一个用于在docker容器中执行代码文件或代码字符串的类。

该类负责在Docker容器中执行不同脚本语言(目前支持Python和Bash)的代码,捕获它们的标准输出和错误流,并允许用户在执行代码字符串前进行检查。

Parameters:
  • require_confirm (bool, optional) – 如果为True,则在运行代码字符串前会提示用户确认以确保安全。默认为True

  • print_stdout (bool, optional) – 如果为True,则打印执行代码的标准输出。默认为False

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。默认为True

run(code: str, code_type: str) str[来源]#

在连接到解释器的容器中执行给定代码,并捕获标准输出和标准错误流。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

Raises:

InterpreterError – 如果用户拒绝运行代码,或代码类型不受支持,或docker API/容器中存在错误

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

class camel.interpreters.E2BInterpreter(require_confirm: bool = True)[来源]#

基类: BaseInterpreter

E2B代码解释器实现。

Parameters:

require_confirm (bool, optional) – 如果为True,则在运行代码字符串前会提示用户确认以确保安全性。(默认值: True)

run(code: str, code_type: str) str[来源]#

在e2b沙盒中执行给定的代码。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

执行代码输出的字符串表示形式。

Return type:

字符串

Raises:

InterpreterError – 如果code_type不被支持,或者在代码执行过程中发生任何运行时错误。

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间

class camel.interpreters.InternalPythonInterpreter(action_space: Dict[str, Any] | None = None, import_white_list: List[str] | None = None, unsafe_mode: bool = False, raise_error: bool = False)[来源]#

基类: BaseInterpreter

一个定制化的Python解释器,用于控制LLM生成代码的执行。该解释器确保代码只能执行动作空间中给定的函数和导入白名单中的模块。它还支持模糊变量匹配,以检索不确定的输入变量名。

该类改编自hugging face的实现 python_interpreter.py。原始许可证适用:

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.

我们已对原始代码进行了修改以满足需求。我们将原有函数封装在类中,并在执行后保存了解释器状态。我们新增了对"import"语句、"for"循环语句以及多个二元和一元运算符的支持。我们添加了导入白名单机制来确保import语句的安全性。此外,我们还修改了变量匹配逻辑,并引入了fuzz_state来实现模糊匹配。

修改版权 (C) 2023 CAMEL-AI.org

Parameters:
  • action_space (Dict[str, Any], optional) – 一个将动作名称映射到其对应函数或对象的字典。解释器只能执行直接列在此字典中或是此字典中列出对象的成员函数的函数。action_space的概念源自EmbodiedAgent,表示一个代理能够执行的动作。如果为None,则设置为空字典。(默认值: None)

  • import_white_list (List[str], optional) – 一个存储可在代码中导入的Python模块或函数的列表。该列表中列出的模块的所有子模块和函数都是可导入的。任何其他导入语句都将被拒绝。模块与其子模块或函数名之间用点号()分隔。(默认值: None)

  • unsafe_mode (bool, optional) – 如果为True,解释器将通过eval()exec()运行代码而不进行任何安全检查。 (默认值: False)

  • raise_error (bool, optional) – 如果解释器失败则抛出错误。 (默认: False)

clear_state() None[来源]#

初始化 statefuzz_state

execute(code: str, state: Dict[str, Any] | None = None, fuzz_state: Dict[str, Any] | None = None, keep_state: bool = True) Any[来源]#

在安全环境中执行输入的Python代码。

Parameters:
  • code (str) – 要执行的生成python代码。

  • state (Optional[Dict[str, Any]], optional) - 可在生成代码中使用的外部变量。(默认: None)

  • fuzz_state (Optional[Dict[str, Any]], optional) - 不具有特定变量名的外部变量。解释器将使用模糊匹配来访问这些变量。例如,如果fuzz_state有一个变量image,生成的代码可以使用input_image来访问它。(默认值:None)

  • keep_state (bool, optional) – 如果设为Truestatefuzz_state将被保留以供后续执行。否则, 它们将被清除。(默认值: True)

Returns:

代码中最后一条语句的值(不包括"import")

对于这个解释器来说,表达式的值就是它的值,"assign"语句的值是被赋的值,"if"和"for"块语句的值是该块中最后一条语句的值。

Return type:

任何

run(code: str, code_type: str) str[来源]#

在解释器中执行指定代码类型的给定代码。

该方法接收一段代码字符串及其类型,检查代码类型是否受支持,然后执行代码。如果unsafe_mode设置为False,代码将在受控环境中使用execute方法执行。如果unsafe_modeTrue,则使用eval()exec()以动作空间作为全局上下文执行代码。如果代码类型不受支持或执行过程中发生任何运行时错误,将引发InterpreterError异常。

Parameters:
  • code (str) – 要执行的python代码。

  • code_type (str) – 代码类型,应为以下之一

  • types (支持的代码)

Returns:

执行代码输出的字符串表示形式。

Return type:

字符串

Raises:

InterpreterError – 如果code_type不被支持,或者在代码执行过程中发生任何运行时错误。

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间。

exception camel.interpreters.InterpreterError[来源]#

基类: Exception

因可通过重新生成代码解决的错误而引发的异常

class camel.interpreters.JupyterKernelInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True)[来源]#

基类: BaseInterpreter

一个用于在Jupyter内核中执行代码字符串的类。

Parameters:
  • require_confirm (bool, optional) – 如果为True,则在运行代码字符串前会提示用户确认以确保安全。默认为True

  • print_stdout (bool, optional) – 如果为True,则打印执行代码的标准输出。默认为False

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。默认为True

run(code: str, code_type: str) str[来源]#

在Jupyter内核中执行给定的代码。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个包含捕获结果的字符串

已执行的代码。

Return type:

字符串

Raises:

InterpreterError – 执行代码时发生错误。

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

Returns:

支持的代码类型。

Return type:

List[str]

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新解释器的动作空间。

Parameters:

action_space (Dict[str, Any]) – 一个字典,表示 新的或更新后的动作空间。

Raises:

RuntimeError – 总是会抛出,因为JupyterKernelInterpreter不支持更新动作空间。

class camel.interpreters.SubprocessInterpreter(require_confirm: bool = True, print_stdout: bool = False, print_stderr: bool = True, execution_timeout: int = 60)[来源]#

基类: BaseInterpreter

SubprocessInterpreter 是一个用于在子进程中执行代码文件或代码字符串的类。

该类负责处理在不同脚本语言(当前支持Python和Bash)中子进程的代码执行,捕获它们的标准输出和错误流,并允许用户在执行代码字符串前进行检查。

Parameters:
  • require_confirm (bool, optional) – 如果为True,出于安全考虑会在运行代码字符串前提示用户确认。(默认值: True)

  • print_stdout (bool, optional) - 如果为True,则打印执行代码的标准输出。(默认: False)

  • print_stderr (bool, optional) – 如果为True,则打印执行代码的标准错误。(默认: True)

  • execution_timeout (int, optional) – 等待代码执行完成的最大时间,单位为秒。(默认值: 60)

run(code: str, code_type: str) str[来源]#
Generates a temporary file with the given code, executes it, and

随后删除该文件。

Parameters:
  • code (str) - 要执行的代码字符串。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

Raises:

InterpreterError – 如果用户拒绝运行代码或代码类型不受支持。

run_file(file: Path, code_type: str) str[来源]#

在子进程中执行代码文件并捕获其输出。

Parameters:
  • file (Path) - 要运行的文件路径对象。

  • code_type (str) – 要执行的代码类型(例如:'python'、'bash')。

Returns:

一个字符串,包含已执行代码捕获的标准输出和标准错误

执行代码的输出。

Return type:

字符串

supported_code_types() List[str][来源]#

提供解释器支持的代码类型。

update_action_space(action_space: Dict[str, Any]) None[来源]#

更新python解释器的动作空间