feat: 增强知识库搜索功能,支持多知识库和指定文件ID搜索;优化SearchKnowledgeRequest结构,向后兼容单知识库ID参数
This commit is contained in:
+6
-4
@@ -423,8 +423,10 @@ func (c *Client) StopSession(ctx context.Context, sessionID string, messageID st
|
|||||||
|
|
||||||
// SearchKnowledgeRequest knowledge search request
|
// SearchKnowledgeRequest knowledge search request
|
||||||
type SearchKnowledgeRequest struct {
|
type SearchKnowledgeRequest struct {
|
||||||
Query string `json:"query"` // Query content
|
Query string `json:"query"` // Query content
|
||||||
KnowledgeBaseID string `json:"knowledge_base_id"` // Knowledge base ID
|
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
|
// SearchKnowledgeResponse search results response
|
||||||
@@ -435,8 +437,8 @@ type SearchKnowledgeResponse struct {
|
|||||||
|
|
||||||
// SearchKnowledge performs knowledge base search without LLM summarization
|
// SearchKnowledge performs knowledge base search without LLM summarization
|
||||||
func (c *Client) SearchKnowledge(ctx context.Context, request *SearchKnowledgeRequest) ([]*SearchResult, error) {
|
func (c *Client) SearchKnowledge(ctx context.Context, request *SearchKnowledgeRequest) ([]*SearchResult, error) {
|
||||||
fmt.Printf("Starting SearchKnowledge request, knowledge base ID: %s, query: %s\n",
|
fmt.Printf("Starting SearchKnowledge request, knowledge base IDs: %v, knowledge IDs: %v, query: %s\n",
|
||||||
request.KnowledgeBaseID, request.Query)
|
request.KnowledgeBaseIDs, request.KnowledgeIDs, request.Query)
|
||||||
|
|
||||||
resp, err := c.doRequest(ctx, http.MethodPost, "/api/v1/knowledge-search", request, nil)
|
resp, err := c.doRequest(ctx, http.MethodPost, "/api/v1/knowledge-search", request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -395,6 +395,15 @@ func (e *EvaluationService) EvalDataset(ctx context.Context, detail *types.Evalu
|
|||||||
// Prepare chat management parameters for this QA pair
|
// Prepare chat management parameters for this QA pair
|
||||||
chatManage := detail.Params.Clone()
|
chatManage := detail.Params.Clone()
|
||||||
chatManage.Query = qaPair.Question
|
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 pipeline
|
// Execute knowledge QA pipeline
|
||||||
logger.Infof(ctx, "Running knowledge QA for question: %s", qaPair.Question)
|
logger.Infof(ctx, "Running knowledge QA for question: %s", qaPair.Question)
|
||||||
|
|||||||
@@ -871,24 +871,46 @@ func getTenantConversationConfig(ctx context.Context) (*types.ConversationConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SearchKnowledge performs knowledge base search without LLM summarization
|
// 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 search
|
||||||
func (s *sessionService) SearchKnowledge(ctx context.Context,
|
func (s *sessionService) SearchKnowledge(ctx context.Context,
|
||||||
knowledgeBaseID, query string,
|
knowledgeBaseIDs []string, knowledgeIDs []string, query string,
|
||||||
) ([]*types.SearchResult, error) {
|
) ([]*types.SearchResult, error) {
|
||||||
logger.Info(ctx, "Start knowledge base search without LLM summary")
|
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 parameters
|
// Create default retrieval parameters
|
||||||
chatManage := &types.ChatManage{
|
chatManage := &types.ChatManage{
|
||||||
Query: query,
|
Query: query,
|
||||||
RewriteQuery: query,
|
RewriteQuery: query,
|
||||||
VectorThreshold: s.cfg.Conversation.VectorThreshold, // Use default configuration
|
KnowledgeBaseIDs: knowledgeBaseIDs,
|
||||||
KeywordThreshold: s.cfg.Conversation.KeywordThreshold, // Use default configuration
|
KnowledgeIDs: knowledgeIDs,
|
||||||
EmbeddingTopK: s.cfg.Conversation.EmbeddingTopK, // Use default configuration
|
SearchTargets: searchTargets,
|
||||||
RerankTopK: s.cfg.Conversation.RerankTopK, // Use default configuration
|
VectorThreshold: s.cfg.Conversation.VectorThreshold, // Use default configuration
|
||||||
RerankThreshold: s.cfg.Conversation.RerankThreshold, // Use default configuration
|
KeywordThreshold: s.cfg.Conversation.KeywordThreshold, // Use default configuration
|
||||||
MaxRounds: s.cfg.Conversation.MaxRounds,
|
EmbeddingTopK: s.cfg.Conversation.EmbeddingTopK, // Use default configuration
|
||||||
RewritePromptSystem: s.cfg.Conversation.RewritePromptSystem,
|
RerankTopK: s.cfg.Conversation.RerankTopK, // Use default configuration
|
||||||
RewritePromptUser: s.cfg.Conversation.RewritePromptUser,
|
RerankThreshold: s.cfg.Conversation.RerankThreshold, // Use default configuration
|
||||||
|
MaxRounds: s.cfg.Conversation.MaxRounds,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get default models
|
// Get default models
|
||||||
@@ -930,7 +952,8 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
|
|||||||
logger.Infof(ctx, "Trigger search event list: %v", methods)
|
logger.Infof(ctx, "Trigger search event list: %v", methods)
|
||||||
span.SetAttributes(
|
span.SetAttributes(
|
||||||
attribute.String("query", query),
|
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, ",")),
|
attribute.String("method", strings.Join(methods, ",")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -48,21 +48,38 @@ func (h *Handler) SearchKnowledge(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.KnowledgeBaseID == "" {
|
// Merge single knowledge_base_id into knowledge_base_ids for backward compatibility
|
||||||
logger.Error(ctx, "Knowledge base ID is empty")
|
knowledgeBaseIDs := request.KnowledgeBaseIDs
|
||||||
c.Error(errors.NewBadRequestError("Knowledge base ID cannot be empty"))
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Infof(
|
logger.Infof(
|
||||||
ctx,
|
ctx,
|
||||||
"Knowledge search request, knowledge base ID: %s, query: %s",
|
"Knowledge search request, knowledge base IDs: %v, knowledge IDs: %v, query: %s",
|
||||||
secutils.SanitizeForLog(request.KnowledgeBaseID),
|
secutils.SanitizeForLogArray(knowledgeBaseIDs),
|
||||||
|
secutils.SanitizeForLogArray(request.KnowledgeIDs),
|
||||||
secutils.SanitizeForLog(request.Query),
|
secutils.SanitizeForLog(request.Query),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Directly call knowledge retrieval service without LLM summarization
|
// 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 {
|
if err != nil {
|
||||||
logger.ErrorWithFields(ctx, err, nil)
|
logger.ErrorWithFields(ctx, err, nil)
|
||||||
c.Error(errors.NewInternalServerError(err.Error()))
|
c.Error(errors.NewInternalServerError(err.Error()))
|
||||||
|
|||||||
@@ -72,8 +72,10 @@ type CreateKnowledgeQARequest struct {
|
|||||||
|
|
||||||
// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarization
|
// SearchKnowledgeRequest defines the request structure for searching knowledge without LLM summarization
|
||||||
type SearchKnowledgeRequest struct {
|
type SearchKnowledgeRequest struct {
|
||||||
Query string `json:"query" binding:"required"` // Query text to search for
|
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
|
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 request
|
// StopSessionRequest represents the stop session request
|
||||||
|
|||||||
@@ -65,12 +65,27 @@ func (c *ChatManage) Clone() *ChatManage {
|
|||||||
knowledgeIDs := make([]string, len(c.KnowledgeIDs))
|
knowledgeIDs := make([]string, len(c.KnowledgeIDs))
|
||||||
copy(knowledgeIDs, 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{
|
return &ChatManage{
|
||||||
Query: c.Query,
|
Query: c.Query,
|
||||||
RewriteQuery: c.RewriteQuery,
|
RewriteQuery: c.RewriteQuery,
|
||||||
SessionID: c.SessionID,
|
SessionID: c.SessionID,
|
||||||
KnowledgeBaseIDs: knowledgeBaseIDs,
|
KnowledgeBaseIDs: knowledgeBaseIDs,
|
||||||
KnowledgeIDs: knowledgeIDs,
|
KnowledgeIDs: knowledgeIDs,
|
||||||
|
SearchTargets: searchTargets,
|
||||||
VectorThreshold: c.VectorThreshold,
|
VectorThreshold: c.VectorThreshold,
|
||||||
KeywordThreshold: c.KeywordThreshold,
|
KeywordThreshold: c.KeywordThreshold,
|
||||||
EmbeddingTopK: c.EmbeddingTopK,
|
EmbeddingTopK: c.EmbeddingTopK,
|
||||||
@@ -101,6 +116,7 @@ func (c *ChatManage) Clone() *ChatManage {
|
|||||||
RewritePromptUser: c.RewritePromptUser,
|
RewritePromptUser: c.RewritePromptUser,
|
||||||
EnableRewrite: c.EnableRewrite,
|
EnableRewrite: c.EnableRewrite,
|
||||||
EnableQueryExpansion: c.EnableQueryExpansion,
|
EnableQueryExpansion: c.EnableQueryExpansion,
|
||||||
|
TenantID: c.TenantID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,9 @@ type SessionService interface {
|
|||||||
// KnowledgeQAByEvent performs knowledge-based question answering by event
|
// KnowledgeQAByEvent performs knowledge-based question answering by event
|
||||||
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
|
KnowledgeQAByEvent(ctx context.Context, chatManage *types.ChatManage, eventList []types.EventType) error
|
||||||
// SearchKnowledge performs knowledge-based search, without summarization
|
// 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
|
// AgentQA performs agent-based question answering with conversation history and streaming support
|
||||||
// eventBus is optional - if nil, uses service's default EventBus
|
// eventBus is optional - if nil, uses service's default EventBus
|
||||||
AgentQA(
|
AgentQA(
|
||||||
|
|||||||
Reference in New Issue
Block a user