tool支持是否启用,更人性化的直接调用
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -322,3 +322,4 @@ run_pet.bat
|
|||||||
config.toml
|
config.toml
|
||||||
|
|
||||||
interested_rates.txt
|
interested_rates.txt
|
||||||
|
MaiBot.code-workspace
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ class BaseTool:
|
|||||||
parameters = None
|
parameters = None
|
||||||
# 是否可供LLM使用,默认为False
|
# 是否可供LLM使用,默认为False
|
||||||
available_for_llm = False
|
available_for_llm = False
|
||||||
|
# 是否启用该工具
|
||||||
|
enabled = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_tool_definition(cls) -> dict[str, Any]:
|
def get_tool_definition(cls) -> dict[str, Any]:
|
||||||
@@ -43,6 +45,7 @@ class BaseTool:
|
|||||||
|
|
||||||
return ToolInfo(
|
return ToolInfo(
|
||||||
name=cls.name,
|
name=cls.name,
|
||||||
|
enabled=cls.enabled,
|
||||||
tool_description=cls.description,
|
tool_description=cls.description,
|
||||||
available_for_llm=cls.available_for_llm,
|
available_for_llm=cls.available_for_llm,
|
||||||
tool_parameters=cls.parameters,
|
tool_parameters=cls.parameters,
|
||||||
@@ -51,7 +54,9 @@ class BaseTool:
|
|||||||
|
|
||||||
# 工具参数定义,子类必须重写
|
# 工具参数定义,子类必须重写
|
||||||
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: 工具调用参数
|
||||||
@@ -60,3 +65,22 @@ class BaseTool:
|
|||||||
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 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)
|
||||||
|
|||||||
@@ -193,10 +193,14 @@ class ComponentRegistry:
|
|||||||
def _register_tool_component(self, tool_info: ToolInfo, tool_class: BaseTool):
|
def _register_tool_component(self, tool_info: ToolInfo, tool_class: BaseTool):
|
||||||
"""注册Tool组件到Tool特定注册表"""
|
"""注册Tool组件到Tool特定注册表"""
|
||||||
tool_name = tool_info.name
|
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
|
self._tool_registry[tool_name] = tool_class
|
||||||
|
|
||||||
# 如果是llm可用的且启用的工具,添加到 llm可用工具列表
|
# 如果是llm可用的工具,添加到 llm可用工具列表
|
||||||
if tool_info.available_for_llm and tool_info.enabled:
|
if tool_info.available_for_llm:
|
||||||
self._llm_available_tools[tool_name] = tool_class
|
self._llm_available_tools[tool_name] = tool_class
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user