3
.gitignore
vendored
3
.gitignore
vendored
@@ -321,4 +321,5 @@ run_pet.bat
|
|||||||
|
|
||||||
config.toml
|
config.toml
|
||||||
|
|
||||||
interested_rates.txt
|
interested_rates.txt
|
||||||
|
MaiBot.code-workspace
|
||||||
|
|||||||
@@ -54,7 +54,9 @@ class BaseTool(ABC):
|
|||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
|
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
|
||||||
"""执行工具函数
|
"""执行工具函数(供llm调用)
|
||||||
|
通过该方法,maicore会通过llm的tool call来调用工具
|
||||||
|
传入的是json格式的参数,符合parameters定义的格式
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
function_args: 工具调用参数
|
function_args: 工具调用参数
|
||||||
@@ -63,3 +65,22 @@ class BaseTool(ABC):
|
|||||||
dict: 工具执行结果
|
dict: 工具执行结果
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError("子类必须实现execute方法")
|
raise NotImplementedError("子类必须实现execute方法")
|
||||||
|
|
||||||
|
async def direct_execute(self, **function_args: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
"""直接执行工具函数(供插件调用)
|
||||||
|
通过该方法,插件可以直接调用工具,而不需要传入字典格式的参数
|
||||||
|
插件可以直接调用此方法,用更加明了的方式传入参数
|
||||||
|
示例: result = await tool.direct_execute(arg1="参数",arg2="参数2")
|
||||||
|
|
||||||
|
工具开发者可以重写此方法以实现与llm调用差异化的执行逻辑
|
||||||
|
|
||||||
|
Args:
|
||||||
|
**function_args: 工具调用参数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: 工具执行结果
|
||||||
|
"""
|
||||||
|
if self.parameters and (missing := [p for p in self.parameters.get("required", []) if p not in function_args]):
|
||||||
|
raise ValueError(f"工具类 {self.__class__.__name__} 缺少必要参数: {', '.join(missing)}")
|
||||||
|
|
||||||
|
return await self.execute(function_args)
|
||||||
|
|||||||
@@ -195,6 +195,7 @@ class ComponentRegistry:
|
|||||||
def _register_tool_component(self, tool_info: ToolInfo, tool_class: Type[BaseTool]) -> bool:
|
def _register_tool_component(self, tool_info: ToolInfo, tool_class: Type[BaseTool]) -> bool:
|
||||||
"""注册Tool组件到Tool特定注册表"""
|
"""注册Tool组件到Tool特定注册表"""
|
||||||
tool_name = tool_info.name
|
tool_name = tool_info.name
|
||||||
|
|
||||||
self._tool_registry[tool_name] = tool_class
|
self._tool_registry[tool_name] = tool_class
|
||||||
|
|
||||||
# 如果是llm可用的且启用的工具,添加到 llm可用工具列表
|
# 如果是llm可用的且启用的工具,添加到 llm可用工具列表
|
||||||
|
|||||||
Reference in New Issue
Block a user