feat: enhance knowledge search by resolving embedding model keys
- Introduced a new method to resolve embedding model IDs to their actual identity keys (name + endpoint), enabling optimal grouping of knowledge bases across different tenants. - Updated the concurrent search functionality to utilize the resolved model keys, improving the efficiency of query embedding computations. - Refactored search target grouping to reduce redundant API calls and enhance performance during hybrid searches. These changes significantly improve the knowledge search process by ensuring that embeddings are computed efficiently and reused across multiple knowledge bases sharing the same underlying model.
This commit is contained in:
@@ -431,8 +431,9 @@ func (t *KnowledgeSearchTool) getKnowledgeBaseTypes(ctx context.Context, kbIDs [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// concurrentSearchByTargets executes hybrid search using pre-computed search targets.
|
// concurrentSearchByTargets executes hybrid search using pre-computed search targets.
|
||||||
// Targets sharing the same embedding model are grouped so the query embedding is
|
// Targets sharing the same underlying embedding model (identified by model name + endpoint)
|
||||||
// computed only once per (model, query) pair, reducing redundant API calls.
|
// are grouped so the query embedding is computed once per (model, query) pair, and all
|
||||||
|
// full-KB targets in a group are combined into a single retrieval call.
|
||||||
func (t *KnowledgeSearchTool) concurrentSearchByTargets(
|
func (t *KnowledgeSearchTool) concurrentSearchByTargets(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
queries []string,
|
queries []string,
|
||||||
@@ -443,25 +444,17 @@ func (t *KnowledgeSearchTool) concurrentSearchByTargets(
|
|||||||
) []*searchResultWithMeta {
|
) []*searchResultWithMeta {
|
||||||
// Batch-fetch KB records for embedding model grouping
|
// Batch-fetch KB records for embedding model grouping
|
||||||
kbIDs := searchTargets.GetAllKnowledgeBaseIDs()
|
kbIDs := searchTargets.GetAllKnowledgeBaseIDs()
|
||||||
kbMap := make(map[string]*types.KnowledgeBase)
|
var kbList []*types.KnowledgeBase
|
||||||
if kbs, err := t.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, kbIDs); err == nil {
|
if kbs, err := t.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, kbIDs); err == nil {
|
||||||
for _, kb := range kbs {
|
kbList = kbs
|
||||||
if kb != nil {
|
|
||||||
kbMap[kb.ID] = kb
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type embGroupKey struct {
|
// Resolve actual model identities (name + endpoint) for cross-tenant grouping
|
||||||
ModelID string
|
modelKeyMap := t.knowledgeBaseService.ResolveEmbeddingModelKeys(ctx, kbList)
|
||||||
TenantID uint64
|
|
||||||
}
|
groups := make(map[string][]*types.SearchTarget)
|
||||||
groups := make(map[embGroupKey][]*types.SearchTarget)
|
|
||||||
for _, st := range searchTargets {
|
for _, st := range searchTargets {
|
||||||
var key embGroupKey
|
key := modelKeyMap[st.KnowledgeBaseID]
|
||||||
if kb, ok := kbMap[st.KnowledgeBaseID]; ok {
|
|
||||||
key = embGroupKey{ModelID: kb.EmbeddingModelID, TenantID: kb.TenantID}
|
|
||||||
}
|
|
||||||
groups[key] = append(groups[key], st)
|
groups[key] = append(groups[key], st)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,17 +464,17 @@ func (t *KnowledgeSearchTool) concurrentSearchByTargets(
|
|||||||
|
|
||||||
for _, query := range queries {
|
for _, query := range queries {
|
||||||
q := query
|
q := query
|
||||||
for gk, targets := range groups {
|
for modelKey, targets := range groups {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(q string, gk embGroupKey, targets []*types.SearchTarget) {
|
go func(q string, modelKey string, targets []*types.SearchTarget) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Compute embedding once for this (model, query) pair
|
// Compute embedding once for this (model, query) pair
|
||||||
var queryEmbedding []float32
|
var queryEmbedding []float32
|
||||||
if gk.ModelID != "" {
|
if modelKey != "" {
|
||||||
emb, err := t.knowledgeBaseService.GetQueryEmbedding(ctx, targets[0].KnowledgeBaseID, q)
|
emb, err := t.knowledgeBaseService.GetQueryEmbedding(ctx, targets[0].KnowledgeBaseID, q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to pre-compute embedding for model %s: %v", gk.ModelID, err)
|
logger.Warnf(ctx, "[Tool][KnowledgeSearch] Failed to pre-compute embedding for model %s: %v", modelKey, err)
|
||||||
} else {
|
} else {
|
||||||
queryEmbedding = emb
|
queryEmbedding = emb
|
||||||
}
|
}
|
||||||
@@ -566,7 +559,7 @@ func (t *KnowledgeSearchTool) concurrentSearchByTargets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerWg.Wait()
|
innerWg.Wait()
|
||||||
}(q, gk, targets)
|
}(q, modelKey, targets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
@@ -209,18 +209,11 @@ func logSearchScoreSample(ctx context.Context, action string, results []*types.S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// embeddingGroupKey groups search targets that share the same embedding model
|
|
||||||
// so we only compute the query embedding once per distinct model.
|
|
||||||
type embeddingGroupKey struct {
|
|
||||||
ModelID string
|
|
||||||
TenantID uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// searchByTargets performs KB searches using pre-computed SearchTargets.
|
// searchByTargets performs KB searches using pre-computed SearchTargets.
|
||||||
// Targets sharing the same embedding model are grouped so the query embedding
|
// Targets sharing the same underlying embedding model (identified by model
|
||||||
// is computed only once per model AND all full-KB targets in a group are
|
// name + endpoint, not just model ID) are grouped so the query embedding is
|
||||||
// combined into a single retrieval call, reducing both embedding API calls and
|
// computed once per model AND all full-KB targets in a group are combined into
|
||||||
// database round-trips.
|
// a single retrieval call, reducing both embedding API calls and DB round-trips.
|
||||||
func (p *PluginSearch) searchByTargets(
|
func (p *PluginSearch) searchByTargets(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
chatManage *types.ChatManage,
|
chatManage *types.ChatManage,
|
||||||
@@ -232,14 +225,16 @@ func (p *PluginSearch) searchByTargets(
|
|||||||
queryText := strings.TrimSpace(chatManage.RewriteQuery)
|
queryText := strings.TrimSpace(chatManage.RewriteQuery)
|
||||||
|
|
||||||
// Batch-fetch KB records to determine embedding model grouping.
|
// Batch-fetch KB records to determine embedding model grouping.
|
||||||
// On failure, all targets fall into a zero-key group and HybridSearch
|
// On failure, all targets fall into an empty-key group and HybridSearch
|
||||||
// computes the embedding per-KB (graceful degradation).
|
// computes the embedding per-KB (graceful degradation).
|
||||||
kbIDs := make([]string, 0, len(chatManage.SearchTargets))
|
kbIDs := make([]string, 0, len(chatManage.SearchTargets))
|
||||||
for _, t := range chatManage.SearchTargets {
|
for _, t := range chatManage.SearchTargets {
|
||||||
kbIDs = append(kbIDs, t.KnowledgeBaseID)
|
kbIDs = append(kbIDs, t.KnowledgeBaseID)
|
||||||
}
|
}
|
||||||
|
var kbList []*types.KnowledgeBase
|
||||||
kbMap := make(map[string]*types.KnowledgeBase)
|
kbMap := make(map[string]*types.KnowledgeBase)
|
||||||
if kbs, err := p.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, kbIDs); err == nil {
|
if kbs, err := p.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, kbIDs); err == nil {
|
||||||
|
kbList = kbs
|
||||||
for _, kb := range kbs {
|
for _, kb := range kbs {
|
||||||
if kb != nil {
|
if kb != nil {
|
||||||
kbMap[kb.ID] = kb
|
kbMap[kb.ID] = kb
|
||||||
@@ -251,12 +246,13 @@ func (p *PluginSearch) searchByTargets(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
groups := make(map[embeddingGroupKey][]*types.SearchTarget)
|
// Resolve actual model identities (name + endpoint) so that cross-tenant
|
||||||
|
// KBs backed by the same physical model share one embedding computation.
|
||||||
|
modelKeyMap := p.knowledgeBaseService.ResolveEmbeddingModelKeys(ctx, kbList)
|
||||||
|
|
||||||
|
groups := make(map[string][]*types.SearchTarget)
|
||||||
for _, t := range chatManage.SearchTargets {
|
for _, t := range chatManage.SearchTargets {
|
||||||
var key embeddingGroupKey
|
key := modelKeyMap[t.KnowledgeBaseID] // empty string if unresolved
|
||||||
if kb, ok := kbMap[t.KnowledgeBaseID]; ok {
|
|
||||||
key = embeddingGroupKey{ModelID: kb.EmbeddingModelID, TenantID: kb.TenantID}
|
|
||||||
}
|
|
||||||
groups[key] = append(groups[key], t)
|
groups[key] = append(groups[key], t)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,20 +265,20 @@ func (p *PluginSearch) searchByTargets(
|
|||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
var results []*types.SearchResult
|
var results []*types.SearchResult
|
||||||
|
|
||||||
for gk, targets := range groups {
|
for modelKey, targets := range groups {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(gk embeddingGroupKey, targets []*types.SearchTarget) {
|
go func(modelKey string, targets []*types.SearchTarget) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Compute embedding once for this model group.
|
// Compute embedding once for this model group.
|
||||||
var queryEmbedding []float32
|
var queryEmbedding []float32
|
||||||
if gk.ModelID != "" {
|
if modelKey != "" {
|
||||||
emb, err := p.knowledgeBaseService.GetQueryEmbedding(ctx, targets[0].KnowledgeBaseID, queryText)
|
emb, err := p.knowledgeBaseService.GetQueryEmbedding(ctx, targets[0].KnowledgeBaseID, queryText)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pipelineWarn(ctx, "Search", "group_embed_error", map[string]interface{}{
|
pipelineWarn(ctx, "Search", "group_embed_error", map[string]interface{}{
|
||||||
"model_id": gk.ModelID,
|
"model_key": modelKey,
|
||||||
"kb_id": targets[0].KnowledgeBaseID,
|
"kb_id": targets[0].KnowledgeBaseID,
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
queryEmbedding = emb
|
queryEmbedding = emb
|
||||||
@@ -302,7 +298,7 @@ func (p *PluginSearch) searchByTargets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pipelineInfo(ctx, "Search", "group_plan", map[string]interface{}{
|
pipelineInfo(ctx, "Search", "group_plan", map[string]interface{}{
|
||||||
"model_id": gk.ModelID,
|
"model_key": modelKey,
|
||||||
"combined_kb_count": len(fullKBIDs),
|
"combined_kb_count": len(fullKBIDs),
|
||||||
"individual_targets": len(knowledgeTargets),
|
"individual_targets": len(knowledgeTargets),
|
||||||
"vector_len": len(queryEmbedding),
|
"vector_len": len(queryEmbedding),
|
||||||
@@ -353,7 +349,7 @@ func (p *PluginSearch) searchByTargets(
|
|||||||
}
|
}
|
||||||
|
|
||||||
innerWg.Wait()
|
innerWg.Wait()
|
||||||
}(gk, targets)
|
}(modelKey, targets)
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|||||||
@@ -631,6 +631,44 @@ func (s *knowledgeBaseService) GetQueryEmbedding(ctx context.Context, kbID strin
|
|||||||
return embeddingModel.Embed(ctx, queryText)
|
return embeddingModel.Embed(ctx, queryText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResolveEmbeddingModelKeys resolves embedding model IDs to their actual model
|
||||||
|
// identity key (name + endpoint). KBs using the same underlying model across
|
||||||
|
// different tenants will share the same key, enabling optimal grouping.
|
||||||
|
func (s *knowledgeBaseService) ResolveEmbeddingModelKeys(ctx context.Context, kbs []*types.KnowledgeBase) map[string]string {
|
||||||
|
type modelRef struct {
|
||||||
|
ModelID string
|
||||||
|
TenantID uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deduplicate model references
|
||||||
|
uniqueRefs := make(map[modelRef]struct{})
|
||||||
|
kbRefs := make(map[string]modelRef, len(kbs))
|
||||||
|
for _, kb := range kbs {
|
||||||
|
ref := modelRef{ModelID: kb.EmbeddingModelID, TenantID: kb.TenantID}
|
||||||
|
uniqueRefs[ref] = struct{}{}
|
||||||
|
kbRefs[kb.ID] = ref
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve each unique (modelID, tenantID) to a model identity key
|
||||||
|
resolvedKeys := make(map[modelRef]string, len(uniqueRefs))
|
||||||
|
for ref := range uniqueRefs {
|
||||||
|
tenantCtx := context.WithValue(ctx, types.TenantIDContextKey, ref.TenantID)
|
||||||
|
model, err := s.modelService.GetModelByID(tenantCtx, ref.ModelID)
|
||||||
|
if err != nil || model == nil {
|
||||||
|
logger.Warnf(ctx, "ResolveEmbeddingModelKeys: cannot resolve model %s for tenant %d: %v", ref.ModelID, ref.TenantID, err)
|
||||||
|
resolvedKeys[ref] = ref.ModelID
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
resolvedKeys[ref] = model.Name + "|" + model.Parameters.BaseURL
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(map[string]string, len(kbs))
|
||||||
|
for _, kb := range kbs {
|
||||||
|
result[kb.ID] = resolvedKeys[kbRefs[kb.ID]]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// HybridSearch performs hybrid search, including vector retrieval and keyword retrieval.
|
// HybridSearch performs hybrid search, including vector retrieval and keyword retrieval.
|
||||||
// When params.KnowledgeBaseIDs is set, those IDs are used for retrieval instead of `id`,
|
// When params.KnowledgeBaseIDs is set, those IDs are used for retrieval instead of `id`,
|
||||||
// allowing a single call to span multiple KBs that share the same embedding model.
|
// allowing a single call to span multiple KBs that share the same embedding model.
|
||||||
|
|||||||
@@ -98,6 +98,12 @@ type KnowledgeBaseService interface {
|
|||||||
// and reuse embeddings across multiple KBs that share the same model.
|
// and reuse embeddings across multiple KBs that share the same model.
|
||||||
GetQueryEmbedding(ctx context.Context, kbID string, queryText string) ([]float32, error)
|
GetQueryEmbedding(ctx context.Context, kbID string, queryText string) ([]float32, error)
|
||||||
|
|
||||||
|
// ResolveEmbeddingModelKeys resolves embedding model IDs to their actual
|
||||||
|
// model identity key (name + endpoint). KBs using the same underlying model
|
||||||
|
// across different tenants will share the same key, enabling optimal grouping.
|
||||||
|
// Returns a map from KB ID to model identity key string.
|
||||||
|
ResolveEmbeddingModelKeys(ctx context.Context, kbs []*types.KnowledgeBase) map[string]string
|
||||||
|
|
||||||
// CopyKnowledgeBase copies a knowledge base
|
// CopyKnowledgeBase copies a knowledge base
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - ctx: Context information
|
// - ctx: Context information
|
||||||
|
|||||||
Reference in New Issue
Block a user