feat: implement parent-child chunking strategy for enhanced context management

- Added support for parent-child chunking in the knowledge base, allowing for a two-level chunking strategy that improves context retrieval.
- Introduced new configuration options for enabling parent-child chunking, including parent and child chunk sizes.
- Updated relevant components and services to handle parent chunk data, ensuring proper linking and retrieval of child chunks.
- Enhanced user interface elements to reflect the new chunking options and provide better user guidance.

These changes significantly improve the knowledge extraction process by providing richer context through hierarchical chunking.
This commit is contained in:
wizardchen
2026-03-04 20:35:10 +08:00
committed by lyingbug
parent 8399527022
commit b333499078
22 changed files with 1100 additions and 403 deletions
+46 -8
View File
@@ -4,6 +4,9 @@ import (
"crypto/md5"
"encoding/hex"
"strings"
"unicode"
"github.com/Tencent/WeKnora/internal/types"
)
// BuildContentSignature creates a normalized MD5 signature for content to detect duplicates.
@@ -20,20 +23,55 @@ func BuildContentSignature(content string) string {
return hex.EncodeToString(hash[:])
}
// TokenizeSimple tokenizes text into a set of words (simple whitespace-based).
// Returns a map where keys are lowercase tokens with length > 1.
// containsChinese checks whether text contains any CJK unified ideographs.
func containsChinese(text string) bool {
for _, r := range text {
if unicode.Is(unicode.Han, r) {
return true
}
}
return false
}
// TokenizeSimple tokenizes text into a set of unique tokens.
// For text containing Chinese characters, it uses jieba segmentation for accurate word boundaries.
// For pure non-Chinese text, it falls back to whitespace-based splitting.
// Returns a map where keys are lowercase tokens with rune length > 1.
func TokenizeSimple(text string) map[string]struct{} {
text = strings.ToLower(text)
fields := strings.Fields(text)
set := make(map[string]struct{}, len(fields))
for _, f := range fields {
if len(f) > 1 {
set[f] = struct{}{}
text = strings.ToLower(strings.TrimSpace(text))
if text == "" {
return nil
}
var words []string
if containsChinese(text) {
// Use jieba for Chinese text segmentation (search mode for finer granularity)
words = types.Jieba.CutForSearch(text, true)
} else {
words = strings.Fields(text)
}
set := make(map[string]struct{}, len(words))
for _, w := range words {
w = strings.TrimSpace(w)
// Filter out single-rune tokens and pure punctuation/whitespace
if len([]rune(w)) > 1 && !isAllPunct(w) {
set[w] = struct{}{}
}
}
return set
}
// isAllPunct checks if a string consists entirely of punctuation or whitespace.
func isAllPunct(s string) bool {
for _, r := range s {
if !unicode.IsPunct(r) && !unicode.IsSpace(r) && !unicode.IsSymbol(r) {
return false
}
}
return true
}
// Jaccard calculates Jaccard similarity between two token sets.
// Returns a value between 0 and 1, where 1 means identical sets.
func Jaccard(a, b map[string]struct{}) float64 {