feat: 增强知识库和搜索功能的逻辑
This commit is contained in:
@@ -195,6 +195,11 @@ func (s *agentService) registerTools(
|
|||||||
"database_query": true,
|
"database_query": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no knowledge and no web search, also disable todo_write (not useful for simple chat)
|
||||||
|
if !config.WebSearchEnabled {
|
||||||
|
kbTools["todo_write"] = true
|
||||||
|
}
|
||||||
|
|
||||||
for _, toolName := range allowedTools {
|
for _, toolName := range allowedTools {
|
||||||
if !kbTools[toolName] {
|
if !kbTools[toolName] {
|
||||||
filteredTools = append(filteredTools, toolName)
|
filteredTools = append(filteredTools, toolName)
|
||||||
|
|||||||
@@ -57,8 +57,9 @@ func (p *PluginSearch) ActivationEvents() []types.EventType {
|
|||||||
func (p *PluginSearch) OnEvent(ctx context.Context,
|
func (p *PluginSearch) OnEvent(ctx context.Context,
|
||||||
eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError,
|
eventType types.EventType, chatManage *types.ChatManage, next func() *PluginError,
|
||||||
) *PluginError {
|
) *PluginError {
|
||||||
// Check if we have search targets
|
// Check if we have search targets or web search enabled
|
||||||
if len(chatManage.SearchTargets) == 0 && len(chatManage.KnowledgeBaseIDs) == 0 && len(chatManage.KnowledgeIDs) == 0 {
|
hasKBTargets := len(chatManage.SearchTargets) > 0 || len(chatManage.KnowledgeBaseIDs) > 0 || len(chatManage.KnowledgeIDs) > 0
|
||||||
|
if !hasKBTargets && !chatManage.WebSearchEnabled {
|
||||||
pipelineError(ctx, "Search", "kb_not_found", map[string]interface{}{
|
pipelineError(ctx, "Search", "kb_not_found", map[string]interface{}{
|
||||||
"session_id": chatManage.SessionID,
|
"session_id": chatManage.SessionID,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -558,16 +558,21 @@ func (s *sessionService) KnowledgeQA(
|
|||||||
EnableQueryExpansion: enableQueryExpansion,
|
EnableQueryExpansion: enableQueryExpansion,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine pipeline based on knowledge bases availability
|
// Determine pipeline based on knowledge bases availability and web search setting
|
||||||
// If no knowledge bases are selected, use pure chat pipeline
|
// If no knowledge bases are selected AND web search is disabled, use pure chat pipeline
|
||||||
|
// Otherwise use rag_stream pipeline (which handles both KB search and web search)
|
||||||
var pipeline []types.EventType
|
var pipeline []types.EventType
|
||||||
if len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 {
|
if len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 && !webSearchEnabled {
|
||||||
logger.Info(ctx, "No knowledge bases selected, using chat_stream pipeline")
|
logger.Info(ctx, "No knowledge bases selected and web search disabled, using chat_stream pipeline")
|
||||||
pipeline = types.Pipline["chat_stream"]
|
pipeline = types.Pipline["chat_stream"]
|
||||||
// For pure chat, UserContent is the Query (since INTO_CHAT_MESSAGE is skipped)
|
// For pure chat, UserContent is the Query (since INTO_CHAT_MESSAGE is skipped)
|
||||||
chatManage.UserContent = query
|
chatManage.UserContent = query
|
||||||
} else {
|
} else {
|
||||||
logger.Info(ctx, "Knowledge bases selected, using rag_stream pipeline")
|
if webSearchEnabled && len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) == 0 {
|
||||||
|
logger.Info(ctx, "Web search enabled without knowledge bases, using rag_stream pipeline for web search only")
|
||||||
|
} else {
|
||||||
|
logger.Info(ctx, "Knowledge bases selected, using rag_stream pipeline")
|
||||||
|
}
|
||||||
pipeline = types.Pipline["rag_stream"]
|
pipeline = types.Pipline["rag_stream"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -234,10 +234,11 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
|||||||
|
|
||||||
// Detect if knowledge bases or agent mode has changed
|
// Detect if knowledge bases or agent mode has changed
|
||||||
knowledgeBasesChanged := false
|
knowledgeBasesChanged := false
|
||||||
|
knowledgeIdsChanged := false
|
||||||
configChanged := false
|
configChanged := false
|
||||||
|
|
||||||
// Check if knowledge bases array has changed
|
// Check if knowledge bases array has changed
|
||||||
if len(request.KnowledgeBaseIDs) > 0 {
|
if len(request.KnowledgeBaseIDs) > 0 || len(session.AgentConfig.KnowledgeBases) > 0 {
|
||||||
// Compare arrays to detect changes
|
// Compare arrays to detect changes
|
||||||
currentKBs := session.AgentConfig.KnowledgeBases
|
currentKBs := session.AgentConfig.KnowledgeBases
|
||||||
if len(currentKBs) != len(request.KnowledgeBaseIDs) {
|
if len(currentKBs) != len(request.KnowledgeBaseIDs) {
|
||||||
@@ -265,6 +266,35 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if knowledge IDs array has changed
|
||||||
|
if len(request.KnowledgeIds) > 0 || len(session.AgentConfig.KnowledgeIDs) > 0 {
|
||||||
|
// Compare arrays to detect changes
|
||||||
|
currentKIDs := session.AgentConfig.KnowledgeIDs
|
||||||
|
if len(currentKIDs) != len(request.KnowledgeIds) {
|
||||||
|
knowledgeIdsChanged = true
|
||||||
|
configChanged = true
|
||||||
|
} else {
|
||||||
|
// Check if contents are different
|
||||||
|
kidMap := make(map[string]bool)
|
||||||
|
for _, kid := range currentKIDs {
|
||||||
|
kidMap[kid] = true
|
||||||
|
}
|
||||||
|
for _, kid := range request.KnowledgeIds {
|
||||||
|
if !kidMap[kid] {
|
||||||
|
knowledgeIdsChanged = true
|
||||||
|
configChanged = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if knowledgeIdsChanged {
|
||||||
|
logger.Infof(ctx, "Knowledge IDs changed from %s to %s",
|
||||||
|
secutils.SanitizeForLog(fmt.Sprintf("%v", session.AgentConfig.KnowledgeIDs)),
|
||||||
|
secutils.SanitizeForLog(fmt.Sprintf("%v", request.KnowledgeIds)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if agent mode has changed
|
// Check if agent mode has changed
|
||||||
currentAgentEnabled := session.AgentConfig.AgentModeEnabled
|
currentAgentEnabled := session.AgentConfig.AgentModeEnabled
|
||||||
if request.AgentEnabled != currentAgentEnabled {
|
if request.AgentEnabled != currentAgentEnabled {
|
||||||
@@ -301,6 +331,7 @@ func (h *Handler) AgentQA(c *gin.Context) {
|
|||||||
// Continue anyway - this is not a fatal error
|
// Continue anyway - this is not a fatal error
|
||||||
}
|
}
|
||||||
session.AgentConfig.KnowledgeBases = secutils.SanitizeForLogArray(request.KnowledgeBaseIDs)
|
session.AgentConfig.KnowledgeBases = secutils.SanitizeForLogArray(request.KnowledgeBaseIDs)
|
||||||
|
session.AgentConfig.KnowledgeIDs = secutils.SanitizeForLogArray(request.KnowledgeIds)
|
||||||
session.AgentConfig.AgentModeEnabled = request.AgentEnabled
|
session.AgentConfig.AgentModeEnabled = request.AgentEnabled
|
||||||
session.AgentConfig.WebSearchEnabled = request.WebSearchEnabled
|
session.AgentConfig.WebSearchEnabled = request.WebSearchEnabled
|
||||||
session.SummaryModelID = secutils.SanitizeForLog(summaryModelID)
|
session.SummaryModelID = secutils.SanitizeForLog(summaryModelID)
|
||||||
|
|||||||
Reference in New Issue
Block a user