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:
@@ -36,7 +36,7 @@ type SplitterConfig struct {
|
||||
func DefaultConfig() SplitterConfig {
|
||||
return SplitterConfig{
|
||||
ChunkSize: 512,
|
||||
ChunkOverlap: 50,
|
||||
ChunkOverlap: 128,
|
||||
Separators: []string{"\n\n", "\n", "。"},
|
||||
}
|
||||
}
|
||||
@@ -316,6 +316,52 @@ func isSeparatorOnly(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// ParentChildResult holds the two-level chunking output.
|
||||
// Parent chunks provide context (large window), child chunks are used for
|
||||
// embedding/retrieval (small window). Each child carries its ParentIndex so
|
||||
// the caller can wire up ParentChunkID after DB insertion.
|
||||
type ParentChildResult struct {
|
||||
Parents []Chunk
|
||||
Children []ChildChunk
|
||||
}
|
||||
|
||||
// ChildChunk extends Chunk with a reference to its parent.
|
||||
type ChildChunk struct {
|
||||
Chunk
|
||||
ParentIndex int // index into ParentChildResult.Parents
|
||||
}
|
||||
|
||||
// SplitTextParentChild performs two-level chunking:
|
||||
// 1. Split text into large parent chunks (parentCfg).
|
||||
// 2. Split each parent into smaller child chunks (childCfg) for embedding.
|
||||
//
|
||||
// The child Seq is globally unique across the entire document.
|
||||
func SplitTextParentChild(text string, parentCfg, childCfg SplitterConfig) ParentChildResult {
|
||||
parents := SplitText(text, parentCfg)
|
||||
if len(parents) == 0 {
|
||||
return ParentChildResult{}
|
||||
}
|
||||
|
||||
var children []ChildChunk
|
||||
childSeq := 0
|
||||
for pi, parent := range parents {
|
||||
subs := SplitText(parent.Content, childCfg)
|
||||
for _, sub := range subs {
|
||||
// Adjust offsets: sub positions are relative to parent content,
|
||||
// shift to document-level offsets.
|
||||
sub.Seq = childSeq
|
||||
sub.Start += parent.Start
|
||||
sub.End = sub.Start + runeLen(sub.Content)
|
||||
children = append(children, ChildChunk{
|
||||
Chunk: sub,
|
||||
ParentIndex: pi,
|
||||
})
|
||||
childSeq++
|
||||
}
|
||||
}
|
||||
return ParentChildResult{Parents: parents, Children: children}
|
||||
}
|
||||
|
||||
// ExtractImageRefs extracts markdown image references from text.
|
||||
var imageRefPattern = regexp.MustCompile(`!\[([^\]]*)\]\(([^)]+)\)`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user