forked from CortexReach/memory-lancedb-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunker.ts
More file actions
284 lines (239 loc) · 8.69 KB
/
chunker.ts
File metadata and controls
284 lines (239 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* Long Context Chunking System
*
* Goal: split documents that exceed embedding model context limits into smaller,
* semantically coherent chunks with overlap.
*
* Notes:
* - We use *character counts* as a conservative proxy for tokens.
* - The embedder triggers this only after a provider throws a context-length error.
*/
// ============================================================================
// Types & Constants
// ============================================================================
export interface ChunkMetadata {
startIndex: number;
endIndex: number;
length: number;
}
export interface ChunkResult {
chunks: string[];
metadatas: ChunkMetadata[];
totalOriginalLength: number;
chunkCount: number;
}
export interface ChunkerConfig {
/** Maximum characters per chunk. */
maxChunkSize: number;
/** Overlap between chunks in characters. */
overlapSize: number;
/** Minimum chunk size (except the final chunk). */
minChunkSize: number;
/** Attempt to split on sentence boundaries for better semantic coherence. */
semanticSplit: boolean;
/** Max lines per chunk before we try to split earlier on a line boundary. */
maxLinesPerChunk: number;
}
// Common embedding context limits (provider/model specific). These are typically
// token limits, but we treat them as inputs to a conservative char-based heuristic.
export const EMBEDDING_CONTEXT_LIMITS: Record<string, number> = {
// Jina v5
"jina-embeddings-v5-text-small": 8192,
"jina-embeddings-v5-text-nano": 8192,
// OpenAI
"text-embedding-3-small": 8192,
"text-embedding-3-large": 8192,
// Google
"text-embedding-004": 8192,
"gemini-embedding-001": 2048,
// Local/common
"nomic-embed-text": 8192,
"all-MiniLM-L6-v2": 512,
"all-mpnet-base-v2": 512,
};
export const DEFAULT_CHUNKER_CONFIG: ChunkerConfig = {
maxChunkSize: 4000,
overlapSize: 200,
minChunkSize: 200,
semanticSplit: true,
maxLinesPerChunk: 50,
};
// Sentence ending patterns (English + CJK-ish punctuation)
const SENTENCE_ENDING = /[.!?。!?]/;
// ============================================================================
// Helpers
// ============================================================================
function clamp(n: number, lo: number, hi: number): number {
return Math.max(lo, Math.min(hi, n));
}
function countLines(s: string): number {
// Count \n (treat CRLF as one line break)
return s.split(/\r\n|\n|\r/).length;
}
function findLastIndexWithin(text: string, re: RegExp, start: number, end: number): number {
// Find last match start index for regex within [start, end).
// NOTE: `re` must NOT be global; we will scan manually.
let last = -1;
for (let i = end - 1; i >= start; i--) {
if (re.test(text[i])) return i;
}
return last;
}
function findSplitEnd(text: string, start: number, maxEnd: number, minEnd: number, config: ChunkerConfig): number {
const safeMinEnd = clamp(minEnd, start + 1, maxEnd);
const safeMaxEnd = clamp(maxEnd, safeMinEnd, text.length);
// Respect line limit: if we exceed maxLinesPerChunk, force earlier split at a line break.
if (config.maxLinesPerChunk > 0) {
const candidate = text.slice(start, safeMaxEnd);
if (countLines(candidate) > config.maxLinesPerChunk) {
// Find the position of the Nth line break.
let breaks = 0;
for (let i = start; i < safeMaxEnd; i++) {
const ch = text[i];
if (ch === "\n") {
breaks++;
if (breaks >= config.maxLinesPerChunk) {
// Split right after this newline.
return Math.max(i + 1, safeMinEnd);
}
}
}
}
}
if (config.semanticSplit) {
// Prefer a sentence boundary near the end.
// Scan backward from safeMaxEnd to safeMinEnd.
for (let i = safeMaxEnd - 1; i >= safeMinEnd; i--) {
if (SENTENCE_ENDING.test(text[i])) {
// Include trailing whitespace after punctuation.
let j = i + 1;
while (j < safeMaxEnd && /\s/.test(text[j])) j++;
return j;
}
}
// Next best: newline boundary.
for (let i = safeMaxEnd - 1; i >= safeMinEnd; i--) {
if (text[i] === "\n") return i + 1;
}
}
// Fallback: last whitespace boundary.
for (let i = safeMaxEnd - 1; i >= safeMinEnd; i--) {
if (/\s/.test(text[i])) return i;
}
return safeMaxEnd;
}
function sliceTrimWithIndices(text: string, start: number, end: number): { chunk: string; meta: ChunkMetadata } {
const raw = text.slice(start, end);
const leading = raw.match(/^\s*/)?.[0]?.length ?? 0;
const trailing = raw.match(/\s*$/)?.[0]?.length ?? 0;
const chunk = raw.trim();
const trimmedStart = start + leading;
const trimmedEnd = end - trailing;
return {
chunk,
meta: {
startIndex: trimmedStart,
endIndex: Math.max(trimmedStart, trimmedEnd),
length: chunk.length,
},
};
}
// ============================================================================
// CJK Detection
// ============================================================================
// CJK Unicode ranges: Unified Ideographs, Extension A, Compatibility,
// Hangul Syllables, Katakana, Hiragana
const CJK_RE =
/[\u3040-\u309F\u30A0-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF]/;
/** Ratio of CJK characters to total non-whitespace characters. */
function getCjkRatio(text: string): number {
let cjk = 0;
let total = 0;
for (const ch of text) {
if (/\s/.test(ch)) continue;
total++;
if (CJK_RE.test(ch)) cjk++;
}
return total === 0 ? 0 : cjk / total;
}
// CJK chars are ~2-3 tokens each. When text is predominantly CJK, we divide
// char limits by this factor to stay within the model's token budget.
const CJK_CHAR_TOKEN_DIVISOR = 2.5;
const CJK_RATIO_THRESHOLD = 0.3;
// ============================================================================
// Chunking Core
// ============================================================================
export function chunkDocument(text: string, config: ChunkerConfig = DEFAULT_CHUNKER_CONFIG): ChunkResult {
if (!text || text.trim().length === 0) {
return { chunks: [], metadatas: [], totalOriginalLength: 0, chunkCount: 0 };
}
const totalOriginalLength = text.length;
const chunks: string[] = [];
const metadatas: ChunkMetadata[] = [];
let pos = 0;
const maxGuard = Math.max(4, Math.ceil(text.length / Math.max(1, config.maxChunkSize - config.overlapSize)) + 5);
let guard = 0;
while (pos < text.length && guard < maxGuard) {
guard++;
const remaining = text.length - pos;
if (remaining <= config.maxChunkSize) {
const { chunk, meta } = sliceTrimWithIndices(text, pos, text.length);
if (chunk.length > 0) {
chunks.push(chunk);
metadatas.push(meta);
}
break;
}
const maxEnd = Math.min(pos + config.maxChunkSize, text.length);
const minEnd = Math.min(pos + config.minChunkSize, maxEnd);
const end = findSplitEnd(text, pos, maxEnd, minEnd, config);
const { chunk, meta } = sliceTrimWithIndices(text, pos, end);
// If trimming made it too small, fall back to a hard split.
if (chunk.length < config.minChunkSize) {
const hardEnd = Math.min(pos + config.maxChunkSize, text.length);
const hard = sliceTrimWithIndices(text, pos, hardEnd);
if (hard.chunk.length > 0) {
chunks.push(hard.chunk);
metadatas.push(hard.meta);
}
if (hardEnd >= text.length) break;
pos = Math.max(hardEnd - config.overlapSize, pos + 1);
continue;
}
chunks.push(chunk);
metadatas.push(meta);
if (end >= text.length) break;
// Move forward with overlap.
const nextPos = Math.max(end - config.overlapSize, pos + 1);
pos = nextPos;
}
return {
chunks,
metadatas,
totalOriginalLength,
chunkCount: chunks.length,
};
}
/**
* Smart chunker that adapts to model context limits.
*
* We intentionally pick conservative char limits (70% of the reported limit)
* since token/char ratios vary.
*/
export function smartChunk(text: string, embedderModel?: string): ChunkResult {
const limit = embedderModel ? EMBEDDING_CONTEXT_LIMITS[embedderModel] : undefined;
const base = limit ?? 8192;
// CJK characters consume ~2-3 tokens each, so a char-based limit that works
// for Latin text will vastly overshoot the token budget for CJK-heavy text.
const cjkHeavy = getCjkRatio(text) > CJK_RATIO_THRESHOLD;
const divisor = cjkHeavy ? CJK_CHAR_TOKEN_DIVISOR : 1;
const config: ChunkerConfig = {
maxChunkSize: Math.max(200, Math.floor(base * 0.7 / divisor)),
overlapSize: Math.max(0, Math.floor(base * 0.05 / divisor)),
minChunkSize: Math.max(100, Math.floor(base * 0.1 / divisor)),
semanticSplit: true,
maxLinesPerChunk: 50,
};
return chunkDocument(text, config);
}
export default chunkDocument;