Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(chat): keep distinct facts in partial-overlap dedup (#4719)

jack0bd6cde12103
diff --git a/internal/application/service/chat_pipeline/search.go b/internal/application/service/chat_pipeline/search.go
index b0f89c260..760471bea 100644
--- a/internal/application/service/chat_pipeline/search.go
+++ b/internal/application/service/chat_pipeline/search.go
@@ -220,11 +220,14 @@ func buildContentSignature(content string) string {
// Two thresholds are used:
// - Substring containment: if the normalized short text is a literal substring
// of the normalized long text, the shorter chunk is removed.
-// - Token overlap coefficient >= 0.85: if 85%+ of the smaller chunk's tokens
-// appear in the larger chunk, the smaller one is redundant. Only applied
-// when the two token sets are within maxOverlapSizeRatio of each other:
-// near-duplicates are of similar size, while a long web page or parent
-// chunk shares most words with any short chunk on its topic.
+// - Token overlap coefficient >= 0.85, and only when the shorter text's
+// tokens are almost a subset of the longer text's (shorter-only fraction
+// at most maxShorterOnlyFraction). Overlap alone is not enough: shared
+// boilerplate can clear 0.85 while each side still has its own
+// requirement, number, or conclusion. Only applied when the two token
+// sets are within maxOverlapSizeRatio of each other: near-duplicates are
+// of similar size, while a long web page or parent chunk shares most
+// words with any short chunk on its topic.
//
// Each text is tokenized once; tokenizing both sides of every pair ran jieba
// O(n²) times over parent- and page-sized texts before the first token of
@@ -237,6 +240,11 @@ func removePartialOverlaps(ctx context.Context, results []*types.SearchResult) [
const (
overlapThreshold = 0.85
maxOverlapSizeRatio = 3
+ // maxShorterOnlyFraction is the largest share of the shorter token set
+ // that may be missing from the longer set and still count as one fact.
+ // It sits far below the 0.15 residue the overlap coefficient allows, so
+ // boilerplate cannot hide a requirement, a number, or a conclusion.
+ maxShorterOnlyFraction = 0.02
)
if len(results) <= 1 {
@@ -292,6 +300,11 @@ func removePartialOverlaps(ctx context.Context, results []*types.SearchResult) [
searchutil.TokenOverlapRatio(shortTokens, longTokens) < overlapThreshold {
continue
}
+ // Drop only when the longer chunk actually carries the shorter
+ // one's tokens. A pair where each side has its own tokens is kept.
+ if shorterOnlyFraction(shortTokens, longTokens) > maxShorterOnlyFraction {
+ continue
+ }
}
victim := shortIdx
@@ -322,6 +335,21 @@ func removePartialOverlaps(ctx context.Context, results []*types.SearchResult) [
return out
}
+// shorterOnlyFraction is the share of short's tokens that do not appear in long.
+// An empty short set is not evidence that long carries it.
+func shorterOnlyFraction(short, long map[string]struct{}) float64 {
+ if len(short) == 0 {
+ return 1
+ }
+ only := 0
+ for tok := range short {
+ if _, ok := long[tok]; !ok {
+ only++
+ }
+ }
+ return float64(only) / float64(len(short))
+}
+
func logSearchScoreSample(ctx context.Context, action string, results []*types.SearchResult) {
const maxLogRows = 8
limit := min(maxLogRows, len(results))
diff --git a/internal/application/service/chat_pipeline/search_test.go b/internal/application/service/chat_pipeline/search_test.go
new file mode 100644
index 000000000..a408a9b55
--- /dev/null
+++ b/internal/application/service/chat_pipeline/search_test.go
@@ -0,0 +1,128 @@
+package chatpipeline
+
+import (
+ "context"
+ "sort"
+ "strings"
+ "testing"
+
+ "github.com/Tencent/WeKnora/internal/types"
+)
+
+// Anonymized FAQ entries from the partial-overlap report. Product and file
+// names are placeholders; the requirements and support conclusions are not.
+const (
+ faqSigningRequirement = `标准问题:产品 X 是否满足以下要求:256位SM2签名速度≥200000TPS
+答案:
+产品:产品 X
+产品型号:(原文未填写)
+要求:256位SM2签名速度≥200000TPS
+支持结论:条件支持
+备注:需定制实现
+来源:product-X.csv,CSV 第 18 行。`
+
+ faqKeygenRequirement = `标准问题:产品 X 是否满足以下要求:256位SM2密钥对生成速度≥300000对/秒
+答案:
+产品:产品 X
+产品型号:(原文未填写)
+要求:256位SM2密钥对生成速度≥300000对/秒
+支持结论:不支持
+备注:(原文未填写)
+来源:product-X.csv,CSV 第 15 行。`
+
+ // Shared template line. The sanitized entries above measure a token-overlap
+ // coefficient of about 0.846, just under the 0.85 gate, so dedup already
+ // keeps them. The reported failure was contained=false at or above that
+ // gate. This line is on both sides and only restores that condition.
+ faqSharedTemplateLine = "适用场景:生产环境部署"
+)
+
+func TestRemovePartialOverlapsKeepsDistinctFAQFacts(t *testing.T) {
+ signing := faqSigningRequirement + "\n" + faqSharedTemplateLine
+ keygen := faqKeygenRequirement + "\n" + faqSharedTemplateLine
+
+ t.Run("sanitized entries", func(t *testing.T) {
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "faq-A-correct", Content: faqSigningRequirement, Score: 1, KnowledgeID: "product-x"},
+ {ID: "faq-B-other-requirement", Content: faqKeygenRequirement, Score: 1, KnowledgeID: "product-x"},
+ })
+ assertResultIDs(t, got, "faq-A-correct", "faq-B-other-requirement")
+ })
+
+ t.Run("same knowledge", func(t *testing.T) {
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "faq-A-correct", Content: signing, Score: 1, KnowledgeID: "product-x"},
+ {ID: "faq-B-other-requirement", Content: keygen, Score: 1, KnowledgeID: "product-x"},
+ })
+ assertResultIDs(t, got, "faq-A-correct", "faq-B-other-requirement")
+ })
+
+ t.Run("different knowledge", func(t *testing.T) {
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "faq-A-correct", Content: signing, Score: 1, KnowledgeID: "kb-signing"},
+ {ID: "faq-B-other-requirement", Content: keygen, Score: 1, KnowledgeID: "kb-keygen"},
+ })
+ assertResultIDs(t, got, "faq-A-correct", "faq-B-other-requirement")
+ })
+}
+
+func TestRemovePartialOverlapsCollapsesCoveredNearDuplicate(t *testing.T) {
+ t.Run("renewal extends the borrowing period", func(t *testing.T) {
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "two-months", Content: "borrowing lasts two months", Score: 0.8},
+ {ID: "with-renewal", Content: "borrowing lasts two months and permits one one-month renewal", Score: 0.8},
+ })
+ assertResultIDs(t, got, "with-renewal")
+ })
+
+ // Same tokens on the shorter side, but not a literal substring, so only
+ // the overlap path can collapse the pair.
+ t.Run("token subset", func(t *testing.T) {
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "short", Content: "renewal window two months borrowing", Score: 0.4},
+ {ID: "long", Content: "the borrowing window is two months and permits one renewal", Score: 0.9},
+ })
+ assertResultIDs(t, got, "long")
+ })
+
+ // One leftover token in a long shared stem is still the same fact.
+ t.Run("single leftover token", func(t *testing.T) {
+ var stem strings.Builder
+ for i := 0; i < 55; i++ {
+ if i > 0 {
+ stem.WriteByte(' ')
+ }
+ stem.WriteString("word")
+ stem.WriteString(strings.Repeat("x", i%3))
+ stem.WriteByte(byte('a' + i%26))
+ }
+ shared := stem.String()
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "short", Content: shared + " stray", Score: 0.4},
+ {ID: "long", Content: shared + " other alpha beta", Score: 0.9},
+ })
+ assertResultIDs(t, got, "long")
+ })
+}
+
+func TestRemovePartialOverlapsKeepsDistinctNumbers(t *testing.T) {
+ base := "产品 X 标准问题 是否满足以下要求 答案 产品型号 原文未填写 备注 来源 product-X csv 行 支持结论 条件 签名速度"
+ got := removePartialOverlaps(context.Background(), []*types.SearchResult{
+ {ID: "limit-200000", Content: base + " 200000", Score: 1, KnowledgeID: "kb-a"},
+ {ID: "limit-300000", Content: base + " 300000", Score: 1, KnowledgeID: "kb-b"},
+ })
+ assertResultIDs(t, got, "limit-200000", "limit-300000")
+}
+
+func assertResultIDs(t *testing.T, results []*types.SearchResult, want ...string) {
+ t.Helper()
+ got := make([]string, len(results))
+ for i, r := range results {
+ got[i] = r.ID
+ }
+ sort.Strings(got)
+ sort.Strings(want)
+ if strings.Join(got, ",") != strings.Join(want, ",") {
+ t.Fatalf("ids = %v, want %v", got, want)
+ }
+}