fix(chunker): trim surrounding whitespace before embedding

Heuristic and recursive splitters preserve original byte positions in
Chunk.Content (the End-Start == RuneCountInString invariant is required
by document-reconstruction code paths), which means a chunk sliced at a
boundary often carries leading/trailing newlines from the boundary
itself. Feeding that whitespace into the embedding model dilutes the
vector and wastes tokens for no benefit.

TrimSpace the body inside EmbeddingContent for both chunker.Chunk and
the two types.Chunk / types.ParsedChunk mirrors. Inner whitespace is
preserved; positions/Content are unchanged.

Also clean up appendChunk: the previous code computed a trimmed string
for the empty-check then stored a separate untrimmed copy, which read
as if it might be intentionally divergent. Replace with an explicit
'raw text, skip if pure whitespace' shape and document the invariant.
This commit is contained in:
wizardchen
2026-05-06 21:02:06 +08:00
committed by lyingbug
parent ad69240ac1
commit 7b9a831db3
4 changed files with 32 additions and 11 deletions
+7 -3
View File
@@ -3,6 +3,7 @@
package types
import (
"strings"
"time"
"gorm.io/gorm"
@@ -172,15 +173,18 @@ type Chunk struct {
// EmbeddingContent returns the chunk content with ContextHeader prepended
// when set. Use this where the embedding model needs section context that
// isn't part of the literal Content.
// isn't part of the literal Content. Surrounding whitespace on Content is
// trimmed so leading/trailing newlines from boundary slicing don't dilute
// the embedded vector.
func (c *Chunk) EmbeddingContent() string {
if c == nil {
return ""
}
body := strings.TrimSpace(c.Content)
if c.ContextHeader == "" {
return c.Content
return body
}
return c.ContextHeader + "\n\n" + c.Content
return c.ContextHeader + "\n\n" + body
}
// AssignChunkSeqIDs assigns sequential SeqIDs to a batch of chunks that have SeqID == 0.
+8 -3
View File
@@ -1,5 +1,7 @@
package types
import "strings"
// ReadRequest is the unified transport-agnostic request for document reading.
// Set FileContent for file mode, URL for URL mode.
type ReadRequest struct {
@@ -90,12 +92,15 @@ type ParsedChunk struct {
// EmbeddingContent returns the text that should be sent to the embedding
// model: ContextHeader (if any) prepended to Content. Mirrors
// chunker.Chunk.EmbeddingContent so the choice is consistent across the
// chunker output and the indexing pipeline.
// chunker output and the indexing pipeline. Surrounding whitespace on
// Content is trimmed so leading/trailing newlines from boundary slicing
// don't dilute the embedded vector.
func (c ParsedChunk) EmbeddingContent() string {
body := strings.TrimSpace(c.Content)
if c.ContextHeader == "" {
return c.Content
return body
}
return c.ContextHeader + "\n\n" + c.Content
return c.ContextHeader + "\n\n" + body
}
// ParsedParentChunk represents a parent chunk in the parent-child strategy.