跳至内容

Python文件

Python文件工具规范 #

基类: BaseToolSpec

Source code in llama-index-integrations/tools/llama-index-tools-python-file/llama_index/tools/python_file/base.py
 7
 8
 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class PythonFileToolSpec(BaseToolSpec):
    spec_functions = ["function_definitions", "get_function", "get_functions"]

    def __init__(self, file_name: str) -> None:
        f = open(file_name).read()
        self.tree = ast.parse(f)

    def function_definitions(self, external: Optional[bool] = True) -> str:
        """
        Use this function to get the name and arguments of all function definitions in the python file.

        Args:
            external (Optional[bool]): Defaults to true. If false, this function will also return functions that start with _

        """
        functions = ""
        for node in ast.walk(self.tree):
            if isinstance(node, ast.FunctionDef):
                if external and node.name.startswith("_"):
                    continue
                functions += f"""
name: {node.name}
arguments: {ast.dump(node.args)}
                    """
        return functions

    def get_function(self, name: str) -> str:
        """
        Use this function to get the name and arguments of a single function definition in the python file.

        Args:
            name (str): The name of the function to retrieve

        """
        for node in ast.walk(self.tree):
            if isinstance(node, ast.FunctionDef):
                if node.name == name:
                    return f"""
name: {node.name}
arguments: {ast.dump(node.args)}
docstring: {ast.get_docstring(node)}
                        """
        return None

    def get_functions(self, names: List[str]) -> str:
        """
        Use this function to get the name and arguments of a list of function definition in the python file.

        Args:
            name (List[str]): The names of the functions to retrieve

        """
        functions = ""
        for name in names:
            functions += self.get_function(name) + "\n"
        return functions

函数定义 #

function_definitions(external: Optional[bool] = True) -> str

使用此函数获取Python文件中所有函数定义的名称和参数。

参数:

名称 类型 描述 默认值
external Optional[bool]

默认为true。如果为false,此函数还将返回以下划线开头的函数

True
Source code in llama-index-integrations/tools/llama-index-tools-python-file/llama_index/tools/python_file/base.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    def function_definitions(self, external: Optional[bool] = True) -> str:
        """
        Use this function to get the name and arguments of all function definitions in the python file.

        Args:
            external (Optional[bool]): Defaults to true. If false, this function will also return functions that start with _

        """
        functions = ""
        for node in ast.walk(self.tree):
            if isinstance(node, ast.FunctionDef):
                if external and node.name.startswith("_"):
                    continue
                functions += f"""
name: {node.name}
arguments: {ast.dump(node.args)}
                    """
        return functions

get_function #

get_function(name: str) -> str

使用此函数获取Python文件中单个函数定义的名称和参数。

参数:

名称 类型 描述 默认值
name str

要检索的函数名称

required
Source code in llama-index-integrations/tools/llama-index-tools-python-file/llama_index/tools/python_file/base.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    def get_function(self, name: str) -> str:
        """
        Use this function to get the name and arguments of a single function definition in the python file.

        Args:
            name (str): The name of the function to retrieve

        """
        for node in ast.walk(self.tree):
            if isinstance(node, ast.FunctionDef):
                if node.name == name:
                    return f"""
name: {node.name}
arguments: {ast.dump(node.args)}
docstring: {ast.get_docstring(node)}
                        """
        return None

get_functions #

get_functions(names: List[str]) -> str

使用此函数获取Python文件中一系列函数定义的名称和参数。

参数:

名称 类型 描述 默认值
name List[str]

要检索的函数名称

required
Source code in llama-index-integrations/tools/llama-index-tools-python-file/llama_index/tools/python_file/base.py
51
52
53
54
55
56
57
58
59
60
61
62
def get_functions(self, names: List[str]) -> str:
    """
    Use this function to get the name and arguments of a list of function definition in the python file.

    Args:
        name (List[str]): The names of the functions to retrieve

    """
    functions = ""
    for name in names:
        functions += self.get_function(name) + "\n"
    return functions
优云智算