chunking

package
v0.0.0-...-58a1804 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package chunking provides text chunking functionality for RAG systems.

Index

Constants

View Source
const (
	DefaultMaxChunkSize  = 2000
	DefaultMinChunkSize  = 500
	DefaultOverlapSize   = 200
	DefaultMaxEmptyLines = 2
	DefaultMinWordLen    = 2
	DefaultTokenRatio    = 1.3
)

Variables

View Source
var (
	ErrEmptyContent     = errors.New("content cannot be empty")
	ErrInvalidChunkSize = errors.New("invalid chunk size configuration")
	ErrContextCanceled  = errors.New("operation was canceled")
)
View Source
var (
	ErrInvalidConfig   = errors.New("invalid configuration")
	ErrNoEmbeddingData = errors.New("no embedding data in response")
	ErrChunkingFailed  = errors.New("chunking failed")
)

Common errors.

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	Content       string            `json:"content"`
	Type          string            `json:"type"`
	Level         int               `json:"level"`
	Title         string            `json:"title"`
	StartIndex    int               `json:"start_index"`
	EndIndex      int               `json:"end_index"`
	Metadata      map[string]string `json:"metadata"`
	ID            string            `json:"id,omitempty"`
	TokenCount    int               `json:"token_count,omitempty"`
	Relationships []string          `json:"relationships,omitempty"`
}

type ChunkType

type ChunkType string
const (
	ChunkTypeHeader    ChunkType = "header"
	ChunkTypeParagraph ChunkType = "paragraph"
	ChunkTypeCode      ChunkType = "code"
	ChunkTypeList      ChunkType = "list"
	ChunkTypeSection   ChunkType = "section"
	ChunkTypeText      ChunkType = "text"
)

type Chunker

type Chunker interface {
	ChunkMarkdown(content string) ([]Chunk, error)
	ChunkMarkdownWithContext(ctx context.Context, content string) ([]Chunk, error)
}

type ChunkerConfig

type ChunkerConfig struct {
	MaxChunkSize       int
	MinChunkSize       int
	OverlapSize        int
	PreserveStructure  bool
	EnableSemantic     bool
	MergeSparseParents bool
}

type Config

type Config struct {
	// Required fields.
	MaxChunkSize int
	MinChunkSize int
	Model        string

	// Optional fields with defaults.
	OverlapSize         int
	SimilarityThreshold float64
	MaxMergeChunks      int
	EnableParallel      bool
}

Config defines chunking configuration.

type DocumentNode

type DocumentNode struct {
	Type       ast.NodeKind
	Level      int
	Title      string
	Content    string
	Children   []*DocumentNode
	StartIndex int
	EndIndex   int
	ASTNode    ast.Node
}

type NodeInfo

type NodeInfo struct {
	Title      string
	Content    string
	StartIndex int
	EndIndex   int
}

type OptimizedMarkdownChunker

type OptimizedMarkdownChunker struct {
	// contains filtered or unexported fields
}

func NewMarkdownChunker

func NewMarkdownChunker(maxChunkSize, overlapSize int, preserveStructure bool) (*OptimizedMarkdownChunker, error)

func NewOptimizedMarkdownChunker

func NewOptimizedMarkdownChunker(config ChunkerConfig) (*OptimizedMarkdownChunker, error)

func (*OptimizedMarkdownChunker) ChunkMarkdown

func (omc *OptimizedMarkdownChunker) ChunkMarkdown(content string) ([]Chunk, error)

type Option

type Option func(*Config)

Option configures a SemanticChunker.

func WithMaxMergeChunks

func WithMaxMergeChunks(max int) Option

WithMaxMergeChunks sets the maximum number of chunks to merge.

func WithModel

func WithModel(model string) Option

WithModel sets the embedding model.

func WithParallelProcessing

func WithParallelProcessing(enabled bool) Option

WithParallelProcessing enables parallel embedding generation.

func WithSimilarityThreshold

func WithSimilarityThreshold(threshold float64) Option

WithSimilarityThreshold sets the similarity threshold for merging.

type SemanticChunker

type SemanticChunker struct {
	// contains filtered or unexported fields
}

SemanticChunker implements semantic-aware text chunking.

func NewSemanticChunker

func NewSemanticChunker(
	maxChunkSize, minChunkSize int,
	embedder embedding.Embedder,
	opts ...Option,
) (*SemanticChunker, error)

NewSemanticChunker creates a new semantic chunker.

func (*SemanticChunker) ChunkText

func (sc *SemanticChunker) ChunkText(ctx context.Context, text string) ([]Chunk, error)

ChunkText performs semantic-aware text chunking.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL