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
fix(repository): ensure expandedTopK is at least params.TopK in VectorRetrieve
wizardchenaf58e9cff444
diff --git a/internal/application/repository/retriever/postgres/repository.go b/internal/application/repository/retriever/postgres/repository.goindex 3ff7796a3..5d26e591c 100644--- a/internal/application/repository/retriever/postgres/repository.go+++ b/internal/application/repository/retriever/postgres/repository.go@@ -348,6 +348,9 @@ func (g *pgRepository) VectorRetrieve(ctx context.Context,if expandedTopK > 1000 {expandedTopK = 1000 // Maximum 1000 candidates}+ if expandedTopK < params.TopK {+ expandedTopK = params.TopK // Ensure subquery limit is at least final limit+ }// Optimized query: Use subquery to calculate distance once// Strategy: Use ORDER BY with vector distance to leverage HNSW index,@@ -364,16 +367,16 @@ func (g *pgRepository) VectorRetrieve(ctx context.Context,FROM (SELECTid, content, source_id, source_type, chunk_id, knowledge_id, knowledge_base_id, tag_id,- embedding::halfvec(%d) <=> $1::halfvec as distance+ embedding <=> $1::halfvec as distanceFROM embeddings%s- ORDER BY embedding::halfvec(%d) <=> $1::halfvec+ ORDER BY embedding <=> $1::halfvecLIMIT $%d) AS candidatesWHERE distance <= $%dORDER BY distance ASCLIMIT $%d- `, dimension, whereClause, dimension, subqueryLimitParam, thresholdParam, finalLimitParam)+ `, whereClause, subqueryLimitParam, thresholdParam, finalLimitParam)allVars = append(allVars, expandedTopK) // LIMIT in subqueryallVars = append(allVars, 1-params.Threshold) // Distance threshold