feat: optimize hybrid search by grouping targets and reusing query embeddings

- Enhanced the concurrent search functionality to group search targets by their embedding model, allowing for a single computation of query embeddings per model.
- Implemented batch fetching of knowledge base records to improve efficiency and reduce redundant API calls.
- Updated the HybridSearch method to support multiple knowledge base IDs, enabling combined retrieval for full-KB targets and improving overall search performance.
- Added a new GetQueryEmbedding method to compute embeddings for queries, facilitating reuse across multiple knowledge bases sharing the same model.

These changes significantly enhance the performance and efficiency of the knowledge search process, reducing API calls and improving response times.
This commit is contained in:
wizardchen
2026-03-13 20:33:47 +08:00
parent 3b006e8bf0
commit fc92b494b0
5 changed files with 392 additions and 133 deletions
@@ -93,6 +93,11 @@ type KnowledgeBaseService interface {
// - Possible errors such as not existing, insufficient permissions, search engine errors, etc.
HybridSearch(ctx context.Context, id string, params types.SearchParams) ([]*types.SearchResult, error)
// GetQueryEmbedding computes the query embedding using the embedding model
// associated with the given knowledge base. This allows callers to pre-compute
// and reuse embeddings across multiple KBs that share the same model.
GetQueryEmbedding(ctx context.Context, kbID string, queryText string) ([]float32, error)
// CopyKnowledgeBase copies a knowledge base
// Parameters:
// - ctx: Context information
+14 -9
View File
@@ -133,15 +133,20 @@ type SearchResult struct {
// SearchParams represents the search parameters
type SearchParams struct {
QueryText string `json:"query_text"`
VectorThreshold float64 `json:"vector_threshold"`
KeywordThreshold float64 `json:"keyword_threshold"`
MatchCount int `json:"match_count"`
DisableKeywordsMatch bool `json:"disable_keywords_match"`
DisableVectorMatch bool `json:"disable_vector_match"`
KnowledgeIDs []string `json:"knowledge_ids"`
TagIDs []string `json:"tag_ids"` // Tag IDs for filtering (used for FAQ priority filtering)
OnlyRecommended bool `json:"only_recommended"`
QueryText string `json:"query_text"`
QueryEmbedding []float32 `json:"query_embedding,omitempty"`
VectorThreshold float64 `json:"vector_threshold"`
KeywordThreshold float64 `json:"keyword_threshold"`
MatchCount int `json:"match_count"`
DisableKeywordsMatch bool `json:"disable_keywords_match"`
DisableVectorMatch bool `json:"disable_vector_match"`
KnowledgeIDs []string `json:"knowledge_ids"`
TagIDs []string `json:"tag_ids"` // Tag IDs for filtering (used for FAQ priority filtering)
OnlyRecommended bool `json:"only_recommended"`
// KnowledgeBaseIDs overrides the single KB ID passed to HybridSearch,
// allowing a single retrieval call to span multiple KBs that share the
// same embedding model. When empty, HybridSearch uses its own id parameter.
KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
// SkipContextEnrichment skips fetching parent, nearby, and relation chunks
// in processSearchResults. Used by the chat pipeline where context assembly
// is handled separately in the merge stage.