mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-04-29 10:56:22 +02:00
simplify tool convert
This commit is contained in:
parent
0116de01b9
commit
bf4b13ec4a
4 changed files with 51 additions and 113 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Literal
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
|
@ -90,14 +91,16 @@ class FillMissingValue(DataPreprocessTool):
|
|||
Completing missing values with simple strategies.
|
||||
"""
|
||||
|
||||
def __init__(self, features: list, strategy: str = "mean", fill_value=None):
|
||||
def __init__(
|
||||
self, features: list, strategy: Literal["mean", "median", "most_frequent", "constant"] = "mean", fill_value=None
|
||||
):
|
||||
"""
|
||||
Initialize self.
|
||||
|
||||
Args:
|
||||
features (list): Columns to be processed.
|
||||
strategy (str, optional): The imputation strategy, notice 'mean' and 'median' can only
|
||||
be used for numeric features. Enum: ['mean', 'median', 'most_frequent', 'constant']. Defaults to 'mean'.
|
||||
strategy (Literal["mean", "median", "most_frequent", "constant"], optional): The imputation strategy, notice 'mean' and 'median' can only
|
||||
be used for numeric features. Defaults to 'mean'.
|
||||
fill_value (int, optional): Fill_value is used to replace all occurrences of missing_values.
|
||||
Defaults to None.
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@ import inspect
|
|||
|
||||
from metagpt.utils.parse_docstring import GoogleDocstringParser, remove_spaces
|
||||
|
||||
PARSER = GoogleDocstringParser
|
||||
|
||||
|
||||
def convert_code_to_tool_schema(obj, include: list[str] = None):
|
||||
docstring = inspect.getdoc(obj)
|
||||
|
|
@ -23,54 +25,31 @@ def convert_code_to_tool_schema(obj, include: list[str] = None):
|
|||
return schema
|
||||
|
||||
|
||||
def function_docstring_to_schema(fn_obj, docstring):
|
||||
def function_docstring_to_schema(fn_obj, docstring) -> dict:
|
||||
"""
|
||||
Converts a function's docstring into a schema dictionary.
|
||||
|
||||
Args:
|
||||
fn_obj: The function object.
|
||||
docstring: The docstring of the function.
|
||||
|
||||
Returns:
|
||||
A dictionary representing the schema of the function's docstring.
|
||||
The dictionary contains the following keys:
|
||||
- 'type': The type of the function ('function' or 'async_function').
|
||||
- 'description': The first section of the docstring describing the function overall. Provided to LLMs for both recommending and using the function.
|
||||
- 'signature': The signature of the function, which helps LLMs understand how to call the function.
|
||||
- 'parameters': Docstring section describing parameters including args and returns, served as extra details for LLM perception.
|
||||
"""
|
||||
signature = inspect.signature(fn_obj)
|
||||
|
||||
docstring = remove_spaces(docstring)
|
||||
|
||||
overall_desc, param_desc = PARSER.parse(docstring)
|
||||
|
||||
function_type = "function" if not inspect.iscoroutinefunction(fn_obj) else "async_function"
|
||||
return {"type": function_type, **docstring_to_schema(docstring)}
|
||||
|
||||
|
||||
def docstring_to_schema(docstring: str):
|
||||
if docstring is None:
|
||||
return {}
|
||||
|
||||
parser = GoogleDocstringParser(docstring=docstring)
|
||||
|
||||
# 匹配简介部分
|
||||
description = parser.parse_desc()
|
||||
|
||||
# 匹配Args部分
|
||||
params = parser.parse_params()
|
||||
parameter_schema = {"properties": {}, "required": []}
|
||||
for param in params:
|
||||
param_name, param_type, param_desc = param
|
||||
# check required or optional
|
||||
is_optional, param_type = parser.check_and_parse_optional(param_type)
|
||||
if not is_optional:
|
||||
parameter_schema["required"].append(param_name)
|
||||
# type and desc
|
||||
param_dict = {"type": param_type, "description": remove_spaces(param_desc)}
|
||||
# match Default for optional args
|
||||
has_default_val, default_val = parser.check_and_parse_default_value(param_desc)
|
||||
if has_default_val:
|
||||
param_dict["default"] = default_val
|
||||
# match Enum
|
||||
has_enum, enum_vals = parser.check_and_parse_enum(param_desc)
|
||||
if has_enum:
|
||||
param_dict["enum"] = enum_vals
|
||||
# add to parameter schema
|
||||
parameter_schema["properties"].update({param_name: param_dict})
|
||||
|
||||
# 匹配Returns部分
|
||||
returns = parser.parse_returns()
|
||||
|
||||
# 构建YAML字典
|
||||
schema = {
|
||||
"description": description,
|
||||
"parameters": parameter_schema,
|
||||
}
|
||||
if returns:
|
||||
schema["returns"] = [{"type": ret[0], "description": remove_spaces(ret[1])} for ret in returns]
|
||||
|
||||
return schema
|
||||
return {"type": function_type, "description": overall_desc, "signature": str(signature), "parameters": param_desc}
|
||||
|
||||
|
||||
def get_class_method_docstring(cls, method_name):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue