feat: 增强知识库节点配置处理逻辑

- 更新工作流引擎以支持从多个来源获取知识库ID,包括节点定义和节点名称
- 添加从节点名称中提取潜在知识库ID的功能
- 改进错误提示信息,提供更详细的配置指导
This commit is contained in:
eason 2026-01-23 13:59:20 +08:00
parent e5e9eebcf2
commit 02ae5908b9
1 changed files with 32 additions and 2 deletions

View File

@ -950,15 +950,45 @@ class WorkflowEngine:
"""执行知识库节点"""
from ..services.document import DocumentService
config = input_data['node_config']
config = input_data.get('node_config', {})
# 支持多种字段名knowledge_base_id, knowledgeBase, kb_id
# 先从 config 中获取
knowledge_base_id = config.get('knowledge_base_id') or config.get('knowledgeBase') or config.get('kb_id')
# 如果 config 中没有,尝试从节点定义本身获取
if not knowledge_base_id:
knowledge_base_id = node.get('knowledge_base_id') or node.get('knowledgeBase') or node.get('kb_id')
# 如果还是没有,尝试从节点的 config 字段(节点定义中的 config获取
if not knowledge_base_id and 'config' in node:
node_config = node.get('config', {})
knowledge_base_id = node_config.get('knowledge_base_id') or node_config.get('knowledgeBase') or node_config.get('kb_id')
query = config.get('query', '')
top_k = config.get('top_k', config.get('topK', 5))
similarity_threshold = config.get('similarity_threshold', config.get('similarityThreshold', 0.7))
# 如果还是没有,尝试从节点名称中提取(例如 "knowledge-base 2" -> 2
if not knowledge_base_id:
raise ValueError("知识库节点配置缺少 knowledge_base_id (或 knowledgeBase/kb_id)")
node_name = node.get('name', '')
import re
# 尝试从名称中提取数字可能是知识库ID
match = re.search(r'(\d+)', node_name)
if match:
try:
potential_id = int(match.group(1))
logger.info(f"从节点名称 '{node_name}' 中提取到潜在的知识库ID: {potential_id}")
knowledge_base_id = potential_id
except:
pass
if not knowledge_base_id:
raise ValueError(
f"知识库节点配置缺少 knowledge_base_id (或 knowledgeBase/kb_id)。\n"
f"请在节点配置中添加知识库ID例如\n"
f" - config.knowledge_base_id: 2\n"
f" - config.knowledgeBase: 2\n"
f" - config.kb_id: 2\n"
f"当前节点配置: {config}"
)
if not query:
# 尝试从 resolved_inputs 中获取查询文本