Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat: 增强知识库搜索功能,支持多知识库和指定文件ID搜索;优化SearchKnowledgeRequest结构,向后兼容单知识库ID参数
wizardchene45b82e9be3a
diff --git a/client/session.go b/client/session.goindex 7b9de4ee6..ff6ad437d 100644--- a/client/session.go+++ b/client/session.go@@ -423,8 +423,10 @@ func (c *Client) StopSession(ctx context.Context, sessionID string, messageID st// SearchKnowledgeRequest knowledge search requesttype SearchKnowledgeRequest struct {- Query string `json:"query"` // Query content- KnowledgeBaseID string `json:"knowledge_base_id"` // Knowledge base ID+ Query string `json:"query"` // Query content+ KnowledgeBaseID string `json:"knowledge_base_id,omitempty"` // Single knowledge base ID (for backward compatibility)+ KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"` // Knowledge base IDs (multi-KB support)+ KnowledgeIDs []string `json:"knowledge_ids,omitempty"` // Specific knowledge (file) IDs}// SearchKnowledgeResponse search results response@@ -435,8 +437,8 @@ type SearchKnowledgeResponse struct {// SearchKnowledge performs knowledge base search without LLM summarizationfunc (c *Client) SearchKnowledge(ctx context.Context, request *SearchKnowledgeRequest) ([]*SearchResult, error) {- fmt.Printf("Starting SearchKnowledge request, knowledge base ID: %s, query: %s\n",- request.KnowledgeBaseID, request.Query)+ fmt.Printf("Starting SearchKnowledge request, knowledge base IDs: %v, knowledge IDs: %v, query: %s\n",+ request.KnowledgeBaseIDs, request.KnowledgeIDs, request.Query)resp, err := c.doRequest(ctx, http.MethodPost, "/api/v1/knowledge-search", request, nil)if err != nil {diff --git a/internal/application/service/evaluation.go b/internal/application/service/evaluation.goindex b71ea63d2..cf3c01fe3 100644--- a/internal/application/service/evaluation.go+++ b/internal/application/service/evaluation.go@@ -395,6 +395,15 @@ func (e *EvaluationService) EvalDataset(ctx context.Context, detail *types.Evalu// Prepare chat management parameters for this QA pairchatManage := detail.Params.Clone()chatManage.Query = qaPair.Question+ chatManage.RewriteQuery = qaPair.Question+ // Set knowledge base ID and search targets for this evaluation+ chatManage.KnowledgeBaseIDs = []string{knowledgeBaseID}+ chatManage.SearchTargets = types.SearchTargets{+ &types.SearchTarget{+ Type: types.SearchTargetTypeKnowledgeBase,+ KnowledgeBaseID: knowledgeBaseID,+ },+ }// Execute knowledge QA pipelinelogger.Infof(ctx, "Running knowledge QA for question: %s", qaPair.Question)diff --git a/internal/application/service/session.go b/internal/application/service/session.goindex 9aac121f9..9f384f9a1 100644--- a/internal/application/service/session.go+++ b/internal/application/service/session.go@@ -871,24 +871,46 @@ func getTenantConversationConfig(ctx context.Context) (*types.ConversationConfig}// SearchKnowledge performs knowledge base search without LLM summarization+// knowledgeBaseIDs: list of knowledge base IDs to search (supports multi-KB)+// knowledgeIDs: list of specific knowledge (file) IDs to searchfunc (s *sessionService) SearchKnowledge(ctx context.Context,- knowledgeBaseID, query string,+ knowledgeBaseIDs []string, knowledgeIDs []string, query string,) ([]*types.SearchResult, error) {logger.Info(ctx, "Start knowledge base search without LLM summary")- logger.Infof(ctx, "Knowledge base search parameters, knowledge base ID: %s, query: %s", knowledgeBaseID, query)+ logger.Infof(ctx, "Knowledge base search parameters, knowledge base IDs: %v, knowledge IDs: %v, query: %s",+ knowledgeBaseIDs, knowledgeIDs, query)++ // Get tenant ID from context+ tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)+ if !ok {+ logger.Error(ctx, "Failed to get tenant ID from context")+ return nil, fmt.Errorf("tenant ID not found in context")+ }++ // Build unified search targets (computed once, used throughout pipeline)+ searchTargets, err := s.buildSearchTargets(ctx, tenantID, knowledgeBaseIDs, knowledgeIDs)+ if err != nil {+ logger.Warnf(ctx, "Failed to build search targets: %v", err)+ }++ if len(searchTargets) == 0 {+ logger.Warn(ctx, "No search targets available, returning empty results")+ return []*types.SearchResult{}, nil+ }// Create default retrieval parameterschatManage := &types.ChatManage{- Query: query,- RewriteQuery: query,- VectorThreshold: s.cfg.Conversation.VectorThreshold, // Use default configuration- KeywordThreshold: s.cfg.Conversation.KeywordThreshold, // Use default configuration- EmbeddingTopK: s.cfg.Conversation.EmbeddingTopK, // Use default configuration- RerankTopK: s.cfg.Conversation.RerankTopK, // Use default configuration- RerankThreshold: s.cfg.Conversation.RerankThreshold, // Use default configuration- MaxRounds: s.cfg.Conversation.MaxRounds,- RewritePromptSystem: s.cfg.Conversation.RewritePromptSystem,- RewritePromptUser: s.cfg.Conversation.RewritePromptUser,+ Query: query,+ RewriteQuery: query,+ KnowledgeBaseIDs: knowledgeBaseIDs,+ KnowledgeIDs: knowledgeIDs,+ SearchTargets: searchTargets,+ VectorThreshold: s.cfg.Conversation.VectorThreshold, // Use default configuration+ KeywordThreshold: s.cfg.Conversation.KeywordThreshold, // Use default configuration+ EmbeddingTopK: s.cfg.Conversation.EmbeddingTopK, // Use default configuration+ RerankTopK: s.cfg.Conversation.RerankTopK, // Use default configuration+ RerankThreshold: s.cfg.Conversation.RerankThreshold, // Use default configuration+ MaxRounds: s.cfg.Conversation.MaxRounds,}// Get default models@@ -930,7 +952,8 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,logger.Infof(ctx, "Trigger search event list: %v", methods)span.SetAttributes(attribute.String("query", query),- attribute.String("knowledge_base_id", knowledgeBaseID),+ attribute.StringSlice("knowledge_base_ids", knowledgeBaseIDs),+ attribute.StringSlice("knowledge_ids", knowledgeIDs),attribute.String("method", strings.Join(methods, ",")),)diff --git a/internal/handler/session/qa.go b/internal/handler/session/qa.goindex 7c7ad8a6c..dc2b88541 100644--- a/internal/handler/session/qa.go+++ b/internal/handler/session/qa.go@@ -48,21 +48,38 @@ func (h *Handler) SearchKnowledge(c *gin.Context) {return}- if request.KnowledgeBaseID == "" {- logger.Error(ctx, "Knowledge base ID is empty")- c.Error(errors.NewBadRequestError("Knowledge base ID cannot be empty"))+ // Merge single knowledge_base_id into knowledge_base_ids for backward compatibility+ knowledgeBaseIDs := request.KnowledgeBaseIDs+ if request.KnowledgeBaseID != "" {+ // Check if it's already in the list to avoid duplicates+ found := false+ for _, id := range knowledgeBaseIDs {+ if id == request.KnowledgeBaseID {+ found = true+ break+ }+ }+ if !found {+ knowledgeBaseIDs = append(knowledgeBaseIDs, request.KnowledgeBaseID)+ }+ }++ if len(knowledgeBaseIDs) == 0 && len(request.KnowledgeIDs) == 0 {+ logger.Error(ctx, "No knowledge base IDs or knowledge IDs provided")+ c.Error(errors.NewBadRequestError("At least one knowledge_base_id, knowledge_base_ids or knowledge_ids must be provided"))return}logger.Infof(ctx,- "Knowledge search request, knowledge base ID: %s, query: %s",- secutils.SanitizeForLog(request.KnowledgeBaseID),+ "Knowledge search request, knowledge base IDs: %v, knowledge IDs: %v, query: %s",+ secutils.SanitizeForLogArray(knowledgeBaseIDs),+ secutils.SanitizeForLogArray(request.KnowledgeIDs),secutils.SanitizeForLog(request.Query),)// Directly call knowledge retrieval service without LLM summarization- searchResults, err := h.sessionService.SearchKnowledge(ctx, request.KnowledgeBaseID, request.Query)+ searchResults, err := h.sessionService.SearchKnowledge(ctx, knowledgeBaseIDs, request.KnowledgeIDs, request.Query)if err != nil {logger.ErrorWithFields(ctx, err, nil)c.Error(errors.NewInternalServerError(err.Error()))diff --git a/internal/handler/session/types.go b/internal/handler/session/types.goindex fce15fc8b..6bcc76235 100644--- a/internal/handler/session/types.go+++ b/internal/handler/session/types.go@@ -72,8 +72,10 @@ type CreateKnowledgeQARequest struct {// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarizationtype SearchKnowledgeRequest struct {- Query string `json:"query" binding:"required"` // Query text to search for- KnowledgeBaseID string `json:"knowledge_base_id" binding:"required"` // ID of the knowledge base to search+ Query string `json:"query" binding:"required"` // Query text to search for+ KnowledgeBaseID string `json:"knowledge_base_id"` // Single knowledge base ID (for backward compatibility)+ KnowledgeBaseIDs []string `json:"knowledge_base_ids"` // IDs of knowledge bases to search (multi-KB support)+ KnowledgeIDs []string `json:"knowledge_ids"` // IDs of specific knowledge (files) to search}// StopSessionRequest represents the stop session requestdiff --git a/internal/types/chat_manage.go b/internal/types/chat_manage.goindex 576507ba4..22bf529a4 100644--- a/internal/types/chat_manage.go+++ b/internal/types/chat_manage.go@@ -65,12 +65,27 @@ func (c *ChatManage) Clone() *ChatManage {knowledgeIDs := make([]string, len(c.KnowledgeIDs))copy(knowledgeIDs, c.KnowledgeIDs)+ // Deep copy search targets slice+ searchTargets := make(SearchTargets, len(c.SearchTargets))+ for i, t := range c.SearchTargets {+ if t != nil {+ kidsCopy := make([]string, len(t.KnowledgeIDs))+ copy(kidsCopy, t.KnowledgeIDs)+ searchTargets[i] = &SearchTarget{+ Type: t.Type,+ KnowledgeBaseID: t.KnowledgeBaseID,+ KnowledgeIDs: kidsCopy,+ }+ }+ }+return &ChatManage{Query: c.Query,RewriteQuery: c.RewriteQuery,SessionID: c.SessionID,KnowledgeBaseIDs: knowledgeBaseIDs,KnowledgeIDs: knowledgeIDs,+ SearchTargets: searchTargets,VectorThreshold: c.VectorThreshold,KeywordThreshold: c.KeywordThreshold,EmbeddingTopK: c.EmbeddingTopK,@@ -101,6 +116,7 @@ func (c *ChatManage) Clone() *ChatManage {RewritePromptUser: c.RewritePromptUser,EnableRewrite: c.EnableRewrite,EnableQueryExpansion: c.EnableQueryExpansion,+ TenantID: c.TenantID,}}diff --git a/internal/types/interfaces/session.go b/internal/types/interfaces/session.goindex b82623ff8..6c129147d 100644--- a/internal/types/interfaces/session.go+++ b/internal/types/interfaces/session.go@@ -39,7 +39,9 @@ type SessionService interface {// KnowledgeQAByEvent performs knowledge-based question answering by eventKnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error// SearchKnowledge performs knowledge-based search, without summarization- SearchKnowledge(ctx context.Context, knowledgeBaseID, query string) ([]*types.SearchResult, error)+ // knowledgeBaseIDs: list of knowledge base IDs to search (supports multi-KB)+ // knowledgeIDs: list of specific knowledge (file) IDs to search+ SearchKnowledge(ctx context.Context, knowledgeBaseIDs []string, knowledgeIDs []string, query string) ([]*types.SearchResult, error)// AgentQA performs agent-based question answering with conversation history and streaming support// eventBus is optional - if nil, uses service's default EventBusAgentQA(