tools.py 2.83 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
import inspect
Michael Poluektov's avatar
Michael Poluektov committed
2
from typing import get_type_hints
Timothy J. Baek's avatar
Timothy J. Baek committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


def doc_to_dict(docstring):
    lines = docstring.split("\n")
    description = lines[1].strip()
    param_dict = {}

    for line in lines:
        if ":param" in line:
            line = line.replace(":param", "").strip()
            param, desc = line.split(":", 1)
            param_dict[param.strip()] = desc.strip()
    ret_dict = {"description": description, "params": param_dict}
    return ret_dict


Michael Poluektov's avatar
Michael Poluektov committed
19
def get_tools_specs(tools) -> list[dict]:
Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
    function_list = [
        {"name": func, "function": getattr(tools, func)}
        for func in dir(tools)
Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
        if callable(getattr(tools, func))
        and not func.startswith("__")
        and not inspect.isclass(getattr(tools, func))
Timothy J. Baek's avatar
Timothy J. Baek committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
    ]

    specs = []
    for function_item in function_list:
        function_name = function_item["name"]
        function = function_item["function"]

        function_doc = doc_to_dict(function.__doc__ or function_name)
        specs.append(
            {
                "name": function_name,
                # TODO: multi-line desc?
                "description": function_doc.get("description", function_name),
                "parameters": {
                    "type": "object",
                    "properties": {
                        param_name: {
                            "type": param_annotation.__name__.lower(),
                            **(
                                {
                                    "enum": (
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
47
                                        str(param_annotation.__args__)
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
                                        if hasattr(param_annotation, "__args__")
                                        else None
                                    )
                                }
                                if hasattr(param_annotation, "__args__")
                                else {}
                            ),
                            "description": function_doc.get("params", {}).get(
                                param_name, param_name
                            ),
                        }
                        for param_name, param_annotation in get_type_hints(
                            function
                        ).items()
62
63
64
65
                        if param_name != "return"
                        and not (
                            param_name.startswith("__") and param_name.endswith("__")
                        )
Timothy J. Baek's avatar
Timothy J. Baek committed
66
67
68
69
70
71
72
                    },
                    "required": [
                        name
                        for name, param in inspect.signature(
                            function
                        ).parameters.items()
                        if param.default is param.empty
Timothy J. Baek's avatar
Timothy J. Baek committed
73
                        and not (name.startswith("__") and name.endswith("__"))
Timothy J. Baek's avatar
Timothy J. Baek committed
74
75
76
77
78
79
                    ],
                },
            }
        )

    return specs