mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-27 09:46:24 +02:00
add UDFS for make tools.
This commit is contained in:
parent
2b8dbec5d0
commit
3de10e7656
2 changed files with 59 additions and 0 deletions
|
|
@ -0,0 +1,50 @@
|
|||
import ast
|
||||
import os
|
||||
import inspect
|
||||
import importlib
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def extract_function_signatures(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
source_code = file.read()
|
||||
|
||||
tree = ast.parse(source_code)
|
||||
function_signatures = []
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.FunctionDef):
|
||||
# 只提取用户自定义函数,排除内置函数
|
||||
if not (node.name.startswith('__') and node.name.endswith('__')):
|
||||
# 获取函数名
|
||||
function_name = node.name
|
||||
# 获取参数列表
|
||||
args = [arg.arg for arg in node.args.args]
|
||||
# 获取函数签名
|
||||
function_signature = f"{function_name}({', '.join(args)})"
|
||||
# 导入函数
|
||||
module = Path(file_path).parts[-1][:-len(Path(file_path).suffix)]
|
||||
module = importlib.import_module(f"metagpt.tools.functions.libs.udf.{module}")
|
||||
# 获取函数注释
|
||||
function_schema = {'name': function_signature, 'doc': inspect.getdoc(getattr(module, function_name))}
|
||||
function_signatures.append(function_schema)
|
||||
|
||||
return function_signatures
|
||||
|
||||
|
||||
def get_function_signatures_in_folder(folder_path):
|
||||
python_files = [f for f in os.listdir(folder_path) if f.endswith('.py')]
|
||||
all_function_signatures = []
|
||||
|
||||
for file_name in python_files:
|
||||
file_path = os.path.join(folder_path, file_name)
|
||||
function_signatures = extract_function_signatures(file_path)
|
||||
all_function_signatures.extend(function_signatures)
|
||||
|
||||
return all_function_signatures
|
||||
|
||||
|
||||
folder_path = str(Path(__file__).parent.absolute())
|
||||
function_signatures = get_function_signatures_in_folder(folder_path)
|
||||
|
||||
UDFS = [func for func in function_signatures
|
||||
if not func['name'].startswith(('extract_function_signatures', 'get_function_signatures_in_folder'))]
|
||||
Loading…
Add table
Add a link
Reference in a new issue