tool支持是否启用,更人性化的直接调用

This commit is contained in:
Windpicker-owo
2025-07-28 22:56:52 +08:00
parent 4ac487dd14
commit 8131e65e9e
3 changed files with 33 additions and 4 deletions

3
.gitignore vendored
View File

@@ -321,4 +321,5 @@ run_pet.bat
config.toml
interested_rates.txt
interested_rates.txt
MaiBot.code-workspace

View File

@@ -19,6 +19,8 @@ class BaseTool:
parameters = None
# 是否可供LLM使用默认为False
available_for_llm = False
# 是否启用该工具
enabled = True
@classmethod
def get_tool_definition(cls) -> dict[str, Any]:
@@ -43,6 +45,7 @@ class BaseTool:
return ToolInfo(
name=cls.name,
enabled=cls.enabled,
tool_description=cls.description,
available_for_llm=cls.available_for_llm,
tool_parameters=cls.parameters,
@@ -51,7 +54,9 @@ class BaseTool:
# 工具参数定义,子类必须重写
async def execute(self, function_args: dict[str, Any]) -> dict[str, Any]:
"""执行工具函数
"""执行工具函数(供llm调用)
通过该方法maicore会通过llm的tool call来调用工具
传入的是json格式的参数符合parameters定义的格式
Args:
function_args: 工具调用参数
@@ -60,3 +65,22 @@ class BaseTool:
dict: 工具执行结果
"""
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 not self.parameters.get("required") in function_args.keys():
raise ValueError(f"工具类 {self.__class__.__name__} 的参数 {self.parameters.get('required')} 必须在在调用时中提供")
return await self.execute(function_args)

View File

@@ -193,10 +193,14 @@ class ComponentRegistry:
def _register_tool_component(self, tool_info: ToolInfo, tool_class: BaseTool):
"""注册Tool组件到Tool特定注册表"""
tool_name = tool_info.name
if not tool_info.enabled:
logger.info(f"Tool组件 {tool_name} 未启用,跳过注册")
return False
self._tool_registry[tool_name] = tool_class
# 如果是llm可用的且启用的工具,添加到 llm可用工具列表
if tool_info.available_for_llm and tool_info.enabled:
# 如果是llm可用的工具,添加到 llm可用工具列表
if tool_info.available_for_llm:
self._llm_available_tools[tool_name] = tool_class
return True