def convert_tool_to_dict(tool: Tool):
    """Convert tool into function parameter for openai"""
    inspection = inspect.getfullargspec(tool.func)
    arg_description = tool.arg_description or {}

    def _type_to_string(t: type) -> str:
        prog = re.compile(r"<class '(\w+)'>")
        cls = prog.findall(str(t))

        primary_type_map = {"str": "string"}

        if len(cls) > 0:
            cls_name = cls[0].split(".")[-1]
            return primary_type_map.get(cls_name, cls_name)

        if issubclass(t, enum.Enum):
            return "enum"

        return str(t)

    def _format_property(t: type, arg_desp: str):
        p = {"type": _type_to_string(t)}
        if arg_desp:
            p["description"] = arg_desp

        return p

    arg_annotations = inspection.annotations
    if arg_annotations:
        properties = {
            arg: _format_property(t, arg_description.get(arg))
            for arg, t in arg_annotations.items()
        }
    else:
        properties = {
            arg: _format_property(str, arg_description.get(arg))
            for arg in inspection.args
        }

    default_args = inspection.defaults or []
    required_args = inspection.args[: len(inspection.args) - len(default_args)]

    output = {
        "name": tool.name,
        "description": tool.description,
        "parameters": {
            "type": "object",
            "properties": properties,
            "required": required_args,
        },
    }

    return output
Convert tool into function parameter for openai
autochain/models/chat_openai.py