35 lines
912 B
Python
35 lines
912 B
Python
"""Tool 基类: 子类只需声明 name/description/parameters 和 execute。"""
|
|
from __future__ import annotations
|
|
|
|
from abc import ABC, abstractmethod
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
|
|
class Tool(ABC):
|
|
name: str = ""
|
|
description: str = ""
|
|
parameters: dict = {}
|
|
|
|
def __init__(self, base_dir: Optional[Path] = None) -> None:
|
|
self.base_dir: Path = Path(base_dir) if base_dir else Path.cwd()
|
|
|
|
@abstractmethod
|
|
def execute(self, **kwargs) -> str:
|
|
...
|
|
|
|
@property
|
|
def schema(self) -> dict:
|
|
return {
|
|
"type": "function",
|
|
"function": {
|
|
"name": self.name,
|
|
"description": self.description,
|
|
"parameters": self.parameters,
|
|
},
|
|
}
|
|
|
|
def _resolve(self, path: str) -> Path:
|
|
p = Path(path)
|
|
return p if p.is_absolute() else (self.base_dir / p)
|