File size: 1,180 Bytes
fbf3c28 |
1 2 3 4 5 6 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 |
import importlib.util
import inspect
import json
import sys
from pathlib import Path
tool_path = Path(sys.argv[1])
def make_specs_from_tools_class(module) -> list:
specs = []
if not hasattr(module, 'Tools'):
return specs
cls = getattr(module, 'Tools')
for name, func in inspect.getmembers(cls, predicate=inspect.isfunction):
if name.startswith('_'):
continue
sig = inspect.signature(func)
props = {}
req = []
for pname, p in sig.parameters.items():
if pname == 'self':
continue
props[pname] = {"type":"string"}
if p.default is inspect._empty:
req.append(pname)
specs.append({
"name": name,
"description": func.__doc__.strip() if func.__doc__ else name,
"parameters": {"type":"object","properties": props, **({"required": req} if req else {})}
})
return specs
spec = importlib.util.spec_from_file_location(tool_path.stem, str(tool_path))
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
specs = make_specs_from_tools_class(mod)
print(json.dumps(specs))
|