跳至内容

代码解释器

init.py.

CodeInterpreterToolSpec #

基类: BaseToolSpec

代码解释器工具规范。

警告:此工具允许智能体访问subprocess.run命令。在运行该工具的机器上可能执行任意代码。不建议在生产环境中使用此工具,若需使用必须进行严格的沙箱隔离或虚拟机保护。

Source code in 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: str)

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

您应该导入您希望使用的任何库。您可以访问用户已安装的所有库。

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

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

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

Source code in 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}"
优云智算