tools整合彻底完成

This commit is contained in:
UnCLAS-Prommer
2025-07-28 23:57:55 +08:00
parent 8bf7166aa4
commit af27d0dbf0
13 changed files with 189 additions and 601 deletions

View File

@@ -1,24 +1,27 @@
from typing import List, Any, Optional, Type
from src.common.logger import get_logger
from abc import ABC, abstractmethod
from typing import Any, Dict
from rich.traceback import install
from src.common.logger import get_logger
from src.plugin_system.base.component_types import ComponentType, ToolInfo
install(extra_lines=3)
logger = get_logger("base_tool")
class BaseTool:
class BaseTool(ABC):
"""所有工具的基类"""
# 工具名称,子类必须重写
name = None
# 工具描述,子类必须重写
description = None
# 工具参数定义,子类必须重写
parameters = None
# 是否可供LLM使用默认为False
available_for_llm = False
name: str = ""
"""工具的名称"""
description: str = ""
"""工具的描述"""
parameters: Dict[str, Any] = {}
"""工具的参数定义"""
available_for_llm: bool = False
"""是否可供LLM使用"""
@classmethod
def get_tool_definition(cls) -> dict[str, Any]:
@@ -38,18 +41,18 @@ class BaseTool:
@classmethod
def get_tool_info(cls) -> ToolInfo:
"""获取工具信息"""
if not cls.name or not cls.description:
raise NotImplementedError(f"工具类 {cls.__name__} 必须定义 name description 属性")
if not cls.name or not cls.description or not cls.parameters:
raise NotImplementedError(f"工具类 {cls.__name__} 必须定义 name, description 和 parameters 属性")
return ToolInfo(
name=cls.name,
tool_description=cls.description,
available_for_llm=cls.available_for_llm,
enabled=cls.available_for_llm,
tool_parameters=cls.parameters,
component_type=ComponentType.TOOL,
)
# 工具参数定义,子类必须重写
@abstractmethod
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
"""执行工具函数