mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
update:知识库自动创建function方法
This commit is contained in:
+1
-1
@@ -55,7 +55,7 @@ public class AgentPluginMappingServiceImpl extends ServiceImpl<AgentPluginMappin
|
||||
paramInfo.put("name", knowledgeBaseEntity.getName());
|
||||
paramInfo.put("description", knowledgeBaseEntity.getDescription());
|
||||
mapping.setParamInfo(JSONUtils.toJSONString(paramInfo));
|
||||
String providerCode = "xzmcp_search_from_knowledgeBase_" + modelConfigEntity.getModelCode() + "_"
|
||||
String providerCode = "xzKnowledgeBase_search_from_" + modelConfigEntity.getModelCode() + "_"
|
||||
+ index;
|
||||
index++;
|
||||
mapping.setProviderCode(providerCode);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""服务端插件工具执行器"""
|
||||
|
||||
from typing import Dict, Any
|
||||
from typing import Dict, Any, Optional, Tuple
|
||||
from ..base import ToolType, ToolDefinition, ToolExecutor
|
||||
from plugins_func.register import all_function_registry, Action, ActionResponse
|
||||
|
||||
@@ -11,12 +11,53 @@ class ServerPluginExecutor(ToolExecutor):
|
||||
def __init__(self, conn):
|
||||
self.conn = conn
|
||||
self.config = conn.config
|
||||
# 存储知识库工具名称到真实插件名称的映射
|
||||
self._knowledge_base_mapping: Dict[str, str] = {}
|
||||
|
||||
def _parse_knowledge_base_config(self, config_name: str) -> Optional[str]:
|
||||
"""
|
||||
解析知识库配置名称,提取真实的插件名称
|
||||
|
||||
Args:
|
||||
config_name: 配置名称,格式为 xzKnowledgeBase_<plugin_name>_<index>
|
||||
|
||||
Returns:
|
||||
真实的插件名称,如果不是知识库配置则返回 None
|
||||
|
||||
Example:
|
||||
"xzKnowledgeBase_search_from_ragflow_0" -> "search_from_ragflow"
|
||||
"xzKnowledgeBase_search_from_ragflow_1" -> "search_from_ragflow"
|
||||
"""
|
||||
if not config_name.startswith("xzKnowledgeBase_"):
|
||||
return None
|
||||
|
||||
# 移除前缀
|
||||
name_without_prefix = config_name[len("xzKnowledgeBase_"):]
|
||||
|
||||
# 找到最后一个下划线的位置
|
||||
last_underscore_index = name_without_prefix.rfind("_")
|
||||
|
||||
if last_underscore_index == -1:
|
||||
return None
|
||||
|
||||
# 提取真实插件名称(从开头到最后一个下划线之前)
|
||||
real_plugin_name = name_without_prefix[:last_underscore_index]
|
||||
|
||||
return real_plugin_name
|
||||
|
||||
async def execute(
|
||||
self, conn, tool_name: str, arguments: Dict[str, Any]
|
||||
) -> ActionResponse:
|
||||
"""执行服务端插件工具"""
|
||||
func_item = all_function_registry.get(tool_name)
|
||||
# 检查是否是知识库工具调用
|
||||
real_plugin_name = self._knowledge_base_mapping.get(tool_name)
|
||||
if real_plugin_name:
|
||||
# 使用真实的插件名称获取函数
|
||||
func_item = all_function_registry.get(real_plugin_name)
|
||||
else:
|
||||
# 普通插件调用
|
||||
func_item = all_function_registry.get(tool_name)
|
||||
|
||||
if not func_item:
|
||||
return ActionResponse(
|
||||
action=Action.NOTFOUND, response=f"插件函数 {tool_name} 不存在"
|
||||
@@ -77,8 +118,45 @@ class ServerPluginExecutor(ToolExecutor):
|
||||
tool_type=ToolType.SERVER_PLUGIN,
|
||||
)
|
||||
|
||||
# 处理知识库配置
|
||||
plugins_config = self.config.get("plugins", {})
|
||||
for config_name, config_value in plugins_config.items():
|
||||
# 检查是否是知识库配置
|
||||
real_plugin_name = self._parse_knowledge_base_config(config_name)
|
||||
if real_plugin_name:
|
||||
# 获取真实的插件函数
|
||||
func_item = all_function_registry.get(real_plugin_name)
|
||||
if func_item and isinstance(config_value, dict):
|
||||
# 从配置中获取自定义的 name 和 description
|
||||
custom_name = config_value.get("name", "")
|
||||
custom_description = config_value.get("description", "")
|
||||
|
||||
# 创建动态的工具名称(使用配置名称去掉前缀部分作为工具名)
|
||||
tool_name = config_name[len("xzKnowledgeBase_"):]
|
||||
|
||||
# 复制原始函数描述并修改
|
||||
custom_func_desc = func_item.description.copy()
|
||||
if "function" in custom_func_desc:
|
||||
custom_func_desc["function"] = custom_func_desc["function"].copy()
|
||||
custom_func_desc["function"]["name"] = tool_name
|
||||
custom_func_desc["function"]["description"] = custom_description
|
||||
|
||||
# 注册工具
|
||||
tools[tool_name] = ToolDefinition(
|
||||
name=tool_name,
|
||||
description=custom_func_desc,
|
||||
tool_type=ToolType.SERVER_PLUGIN,
|
||||
)
|
||||
|
||||
# 保存映射关系
|
||||
self._knowledge_base_mapping[tool_name] = real_plugin_name
|
||||
|
||||
return tools
|
||||
|
||||
def has_tool(self, tool_name: str) -> bool:
|
||||
"""检查是否有指定的服务端插件工具"""
|
||||
# 检查是否是知识库工具
|
||||
if tool_name in self._knowledge_base_mapping:
|
||||
return True
|
||||
# 检查是否是普通工具
|
||||
return tool_name in all_function_registry
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
from plugins_func.register import register_function, ToolType, ActionResponse, Action
|
||||
|
||||
# 定义基础的函数描述模板
|
||||
SEARCH_FROM_RAGFLOW_FUNCTION_DESC = {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "search_from_ragflow",
|
||||
"description": "从知识库中查询信息",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {"query": {"type": "string", "description": "查询的关键词"}},
|
||||
"required": ["query"],
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@register_function(
|
||||
"search_from_ragflow", SEARCH_FROM_RAGFLOW_FUNCTION_DESC, ToolType.WAIT
|
||||
)
|
||||
def search_from_ragflow(query=None):
|
||||
"""
|
||||
用于从ragflow知识库中查询信息
|
||||
"""
|
||||
# TODO 从ragflow知识库中查询信息
|
||||
if query and "医生" in query:
|
||||
response_text = "医院有张山、里斯、王五3名全科医生,其中王五医生是主要擅长眼科"
|
||||
elif query and "科室" in query:
|
||||
response_text = "医院眼科、麻醉科"
|
||||
else:
|
||||
response_text = "暂无相关信息"
|
||||
|
||||
return ActionResponse(Action.REQLLM, response_text, None)
|
||||
Reference in New Issue
Block a user