跳到内容

代码解释器

init.py.

CodeInterpreterToolSpec #

基类: BaseToolSpec

代码解释器工具规范。

警告:此工具为智能体提供 subprocess.run 命令的访问权限。在运行此工具的机器上可能发生任意代码执行。不建议在生产环境中使用此工具,并且需要严格的沙箱或虚拟机隔离。

源码位于 llama-index-integrations/tools/llama-index-tools-code-interpreter/llama_index/tools/code_interpreter/base.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class CodeInterpreterToolSpec(BaseToolSpec):
    """
    Code Interpreter tool spec.

    WARNING: This tool provides the Agent access to the `subprocess.run` command.
    Arbitrary code execution is possible on the machine running this tool.
    This tool is not recommended to be used in a production setting, and would require heavy sandboxing or virtual machines

    """

    spec_functions = ["code_interpreter"]

    def code_interpreter(self, code: str):
        """
        A function to execute python code, and return the stdout and stderr.

        You should import any libraries that you wish to use. You have access to any libraries the user has installed.

        The code passed to this function is executed in isolation. It should be complete at the time it is passed to this function.

        You should interpret the output and errors returned from this function, and attempt to fix any problems.
        If you cannot fix the error, show the code to the user and ask for help

        It is not possible to return graphics or other complicated data from this function. If the user cannot see the output, save it to a file and tell the user.
        """
        result = subprocess.run([sys.executable, "-c", code], capture_output=True)
        return f"StdOut:\n{result.stdout}\nStdErr:\n{result.stderr}"

code_interpreter #

code_interpreter(code: str)

一个函数,用于执行 Python 代码并返回标准输出和标准错误。

你应该导入你想使用的任何库。你可以访问用户已安装的任何库。

传递给此函数的代码是在隔离环境中执行的。在传递给此函数时,代码应是完整的。

你应该解释此函数返回的输出和错误,并尝试修复任何问题。如果无法修复错误,请将代码展示给用户并寻求帮助

此函数无法返回图形或其他复杂数据。如果用户看不到输出,请将其保存到文件中并告知用户。

源码位于 llama-index-integrations/tools/llama-index-tools-code-interpreter/llama_index/tools/code_interpreter/base.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def code_interpreter(self, code: str):
    """
    A function to execute python code, and return the stdout and stderr.

    You should import any libraries that you wish to use. You have access to any libraries the user has installed.

    The code passed to this function is executed in isolation. It should be complete at the time it is passed to this function.

    You should interpret the output and errors returned from this function, and attempt to fix any problems.
    If you cannot fix the error, show the code to the user and ask for help

    It is not possible to return graphics or other complicated data from this function. If the user cannot see the output, save it to a file and tell the user.
    """
    result = subprocess.run([sys.executable, "-c", code], capture_output=True)
    return f"StdOut:\n{result.stdout}\nStdErr:\n{result.stderr}"