forked from CortexReach/memory-lancedb-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedder.ts
More file actions
789 lines (666 loc) · 26.8 KB
/
embedder.ts
File metadata and controls
789 lines (666 loc) · 26.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/**
* Embedding Abstraction Layer
* OpenAI-compatible API for various embedding providers.
* Supports automatic chunking for documents exceeding embedding context limits.
*
* Note: Some providers (e.g. Jina) support extra parameters like `task` and
* `normalized` on the embeddings endpoint. The OpenAI SDK types do not include
* these fields, so we pass them via a narrow `any` cast.
*/
import OpenAI from "openai";
import { createHash } from "node:crypto";
import { smartChunk } from "./chunker.js";
// ============================================================================
// Embedding Cache (LRU with TTL)
// ============================================================================
interface CacheEntry {
vector: number[];
createdAt: number;
}
class EmbeddingCache {
private cache = new Map<string, CacheEntry>();
private readonly maxSize: number;
private readonly ttlMs: number;
public hits = 0;
public misses = 0;
constructor(maxSize = 256, ttlMinutes = 30) {
this.maxSize = maxSize;
this.ttlMs = ttlMinutes * 60_000;
}
private key(text: string, task?: string): string {
const hash = createHash("sha256").update(`${task || ""}:${text}`).digest("hex").slice(0, 24);
return hash;
}
get(text: string, task?: string): number[] | undefined {
const k = this.key(text, task);
const entry = this.cache.get(k);
if (!entry) {
this.misses++;
return undefined;
}
if (Date.now() - entry.createdAt > this.ttlMs) {
this.cache.delete(k);
this.misses++;
return undefined;
}
// Move to end (most recently used)
this.cache.delete(k);
this.cache.set(k, entry);
this.hits++;
return entry.vector;
}
set(text: string, task: string | undefined, vector: number[]): void {
const k = this.key(text, task);
// Evict oldest if full
if (this.cache.size >= this.maxSize) {
const firstKey = this.cache.keys().next().value;
if (firstKey !== undefined) this.cache.delete(firstKey);
}
this.cache.set(k, { vector, createdAt: Date.now() });
}
get size(): number { return this.cache.size; }
get stats(): { size: number; hits: number; misses: number; hitRate: string } {
const total = this.hits + this.misses;
return {
size: this.cache.size,
hits: this.hits,
misses: this.misses,
hitRate: total > 0 ? `${((this.hits / total) * 100).toFixed(1)}%` : "N/A",
};
}
}
// ============================================================================
// Types & Configuration
// ============================================================================
export interface EmbeddingConfig {
provider: "openai-compatible";
/** Single API key or array of keys for round-robin rotation with failover. */
apiKey: string | string[];
model: string;
baseURL?: string;
dimensions?: number;
/** Optional task type for query embeddings (e.g. "retrieval.query") */
taskQuery?: string;
/** Optional task type for passage/document embeddings (e.g. "retrieval.passage") */
taskPassage?: string;
/** Optional flag to request normalized embeddings (provider-dependent, e.g. Jina v5) */
normalized?: boolean;
/** Enable automatic chunking for documents exceeding context limits (default: true) */
chunking?: boolean;
}
// Known embedding model dimensions
const EMBEDDING_DIMENSIONS: Record<string, number> = {
"text-embedding-3-small": 1536,
"text-embedding-3-large": 3072,
"text-embedding-004": 768,
"gemini-embedding-001": 3072,
"nomic-embed-text": 768,
"mxbai-embed-large": 1024,
"BAAI/bge-m3": 1024,
"all-MiniLM-L6-v2": 384,
"all-mpnet-base-v2": 512,
// Jina v5
"jina-embeddings-v5-text-small": 1024,
"jina-embeddings-v5-text-nano": 768,
};
// ============================================================================
// Utility Functions
// ============================================================================
function resolveEnvVars(value: string): string {
return value.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
const envValue = process.env[envVar];
if (!envValue) {
throw new Error(`Environment variable ${envVar} is not set`);
}
return envValue;
});
}
function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
function getErrorStatus(error: unknown): number | undefined {
if (!error || typeof error !== "object") return undefined;
const err = error as Record<string, any>;
if (typeof err.status === "number") return err.status;
if (typeof err.statusCode === "number") return err.statusCode;
if (err.error && typeof err.error === "object") {
if (typeof err.error.status === "number") return err.error.status;
if (typeof err.error.statusCode === "number") return err.error.statusCode;
}
return undefined;
}
function getErrorCode(error: unknown): string | undefined {
if (!error || typeof error !== "object") return undefined;
const err = error as Record<string, any>;
if (typeof err.code === "string") return err.code;
if (err.error && typeof err.error === "object" && typeof err.error.code === "string") {
return err.error.code;
}
return undefined;
}
function getProviderLabel(baseURL: string | undefined, model: string): string {
const base = baseURL || "";
if (base) {
if (/api\.jina\.ai/i.test(base)) return "Jina";
if (/localhost:11434|127\.0\.0\.1:11434|\/ollama\b/i.test(base)) return "Ollama";
if (/api\.openai\.com/i.test(base)) return "OpenAI";
try {
return new URL(base).host;
} catch {
return base;
}
}
if (/^jina-/i.test(model)) return "Jina";
return "embedding provider";
}
function isAuthError(error: unknown): boolean {
const status = getErrorStatus(error);
if (status === 401 || status === 403) return true;
const code = getErrorCode(error);
if (code && /invalid.*key|auth|forbidden|unauthorized/i.test(code)) return true;
const msg = getErrorMessage(error);
return /\b401\b|\b403\b|invalid api key|api key expired|expired api key|forbidden|unauthorized|authentication failed|access denied/i.test(msg);
}
function isNetworkError(error: unknown): boolean {
const code = getErrorCode(error);
if (code && /ECONNREFUSED|ECONNRESET|ENOTFOUND|EHOSTUNREACH|ETIMEDOUT/i.test(code)) {
return true;
}
const msg = getErrorMessage(error);
return /ECONNREFUSED|ECONNRESET|ENOTFOUND|EHOSTUNREACH|ETIMEDOUT|fetch failed|network error|socket hang up|connection refused|getaddrinfo/i.test(msg);
}
export function formatEmbeddingProviderError(
error: unknown,
opts: { baseURL?: string; model: string; mode?: "single" | "batch" },
): string {
const raw = getErrorMessage(error).trim();
if (
raw.startsWith("Embedding provider authentication failed") ||
raw.startsWith("Embedding provider unreachable") ||
raw.startsWith("Failed to generate embedding from ") ||
raw.startsWith("Failed to generate batch embeddings from ")
) {
return raw;
}
const status = getErrorStatus(error);
const code = getErrorCode(error);
const provider = getProviderLabel(opts.baseURL, opts.model);
const detail = raw.length > 0 ? raw : "unknown error";
const suffix = [status, code].filter(Boolean).join(" ");
const detailText = suffix ? `${suffix}: ${detail}` : detail;
const genericPrefix =
opts.mode === "batch"
? `Failed to generate batch embeddings from ${provider}: `
: `Failed to generate embedding from ${provider}: `;
if (isAuthError(error)) {
let hint = `Check embedding.apiKey and endpoint for ${provider}.`;
if (provider === "Jina") {
hint +=
" If your Jina key expired or lost access, replace the key or switch to a local OpenAI-compatible endpoint such as Ollama (for example baseURL http://127.0.0.1:11434/v1, with a matching model and embedding.dimensions).";
} else if (provider === "Ollama") {
hint +=
" Ollama usually works with a dummy apiKey; verify the local server is running, the model is pulled, and embedding.dimensions matches the model output.";
}
return `Embedding provider authentication failed (${detailText}). ${hint}`;
}
if (isNetworkError(error)) {
let hint = `Verify the endpoint is reachable`;
if (opts.baseURL) {
hint += ` at ${opts.baseURL}`;
}
hint += ` and that model \"${opts.model}\" is available.`;
return `Embedding provider unreachable (${detailText}). ${hint}`;
}
return `${genericPrefix}${detailText}`;
}
// ============================================================================
// Safety Constants
// ============================================================================
/** Maximum recursion depth for embedSingle chunking retries. */
const MAX_EMBED_DEPTH = 3;
/** Global timeout for a single embedding operation (ms). */
const EMBED_TIMEOUT_MS = 10_000;
/**
* Safe character limits per model for forced truncation.
* CJK characters typically consume ~3 tokens each, so the char limit is
* conservative compared to the token limit.
*/
const SAFE_CHAR_LIMITS: Record<string, number> = {
"nomic-embed-text": 2300,
"mxbai-embed-large": 2300,
"all-MiniLM-L6-v2": 1000,
"all-mpnet-base-v2": 1500,
};
const DEFAULT_SAFE_CHAR_LIMIT = 2000;
/** Return a safe character count for forced truncation given a model name. */
function getSafeCharLimit(model: string): number {
return SAFE_CHAR_LIMITS[model] ?? DEFAULT_SAFE_CHAR_LIMIT;
}
export function getVectorDimensions(model: string, overrideDims?: number): number {
if (overrideDims && overrideDims > 0) {
return overrideDims;
}
const dims = EMBEDDING_DIMENSIONS[model];
if (!dims) {
throw new Error(
`Unsupported embedding model: ${model}. Either add it to EMBEDDING_DIMENSIONS or set embedding.dimensions in config.`
);
}
return dims;
}
// ============================================================================
// Embedder Class
// ============================================================================
export class Embedder {
/** Pool of OpenAI clients — one per API key for round-robin rotation. */
private clients: OpenAI[];
/** Round-robin index for client rotation. */
private _clientIndex: number = 0;
public readonly dimensions: number;
private readonly _cache: EmbeddingCache;
private readonly _model: string;
private readonly _baseURL?: string;
private readonly _taskQuery?: string;
private readonly _taskPassage?: string;
private readonly _normalized?: boolean;
/** Optional requested dimensions to pass through to the embedding provider (OpenAI-compatible). */
private readonly _requestDimensions?: number;
/** Enable automatic chunking for long documents (default: true) */
private readonly _autoChunk: boolean;
constructor(config: EmbeddingConfig & { chunking?: boolean }) {
// Normalize apiKey to array and resolve environment variables
const apiKeys = Array.isArray(config.apiKey) ? config.apiKey : [config.apiKey];
const resolvedKeys = apiKeys.map(k => resolveEnvVars(k));
this._model = config.model;
this._baseURL = config.baseURL;
this._taskQuery = config.taskQuery;
this._taskPassage = config.taskPassage;
this._normalized = config.normalized;
this._requestDimensions = config.dimensions;
// Enable auto-chunking by default for better handling of long documents
this._autoChunk = config.chunking !== false;
// Create a client pool — one OpenAI client per key
this.clients = resolvedKeys.map(key => new OpenAI({
apiKey: key,
...(config.baseURL ? { baseURL: config.baseURL } : {}),
}));
if (this.clients.length > 1) {
console.log(`[memory-lancedb-pro] Initialized ${this.clients.length} API keys for round-robin rotation`);
}
this.dimensions = getVectorDimensions(config.model, config.dimensions);
this._cache = new EmbeddingCache(256, 30); // 256 entries, 30 min TTL
}
// --------------------------------------------------------------------------
// Multi-key rotation helpers
// --------------------------------------------------------------------------
/** Return the next client in round-robin order. */
private nextClient(): OpenAI {
const client = this.clients[this._clientIndex % this.clients.length];
this._clientIndex = (this._clientIndex + 1) % this.clients.length;
return client;
}
/** Check whether an error is a rate-limit / quota-exceeded / overload error. */
private isRateLimitError(error: unknown): boolean {
if (!error || typeof error !== "object") return false;
const err = error as Record<string, any>;
// HTTP status: 429 (rate limit) or 503 (service overload)
if (err.status === 429 || err.status === 503) return true;
// OpenAI SDK structured error code
if (err.code === "rate_limit_exceeded" || err.code === "insufficient_quota") return true;
// Nested error object (some providers)
const nested = err.error;
if (nested && typeof nested === "object") {
if (nested.type === "rate_limit_exceeded" || nested.type === "insufficient_quota") return true;
if (nested.code === "rate_limit_exceeded" || nested.code === "insufficient_quota") return true;
}
// Fallback: message text matching
const msg = error instanceof Error ? error.message : String(error);
return /rate.limit|quota|too many requests|insufficient.*credit|429|503.*overload/i.test(msg);
}
/**
* Call embeddings.create with automatic key rotation on rate-limit errors.
* Tries each key in the pool at most once before giving up.
*/
private async embedWithRetry(payload: any): Promise<any> {
const maxAttempts = this.clients.length;
let lastError: Error | undefined;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const client = this.nextClient();
try {
return await client.embeddings.create(payload);
} catch (error) {
lastError = error instanceof Error ? error : new Error(String(error));
if (this.isRateLimitError(error) && attempt < maxAttempts - 1) {
console.log(
`[memory-lancedb-pro] Attempt ${attempt + 1}/${maxAttempts} hit rate limit, rotating to next key...`
);
continue;
}
// Non-rate-limit error → don't retry, let caller handle (e.g. chunking)
if (!this.isRateLimitError(error)) {
throw error;
}
}
}
// All keys exhausted with rate-limit errors
throw new Error(
`All ${maxAttempts} API keys exhausted (rate limited). Last error: ${lastError?.message || "unknown"}`,
{ cause: lastError }
);
}
/** Number of API keys in the rotation pool. */
get keyCount(): number {
return this.clients.length;
}
/** FR-05: Wrap a promise with a global timeout to prevent indefinite hangs. */
private withTimeout<T>(promise: Promise<T>, label: string): Promise<T> {
return Promise.race([
promise,
new Promise<never>((_, reject) => {
setTimeout(
() => reject(new Error(
`[memory-lancedb-pro] ${label} timed out after ${EMBED_TIMEOUT_MS}ms`
)),
EMBED_TIMEOUT_MS,
);
}),
]);
}
// --------------------------------------------------------------------------
// Backward-compatible API
// --------------------------------------------------------------------------
/**
* Backward-compatible embedding API.
*
* Historically the plugin used a single `embed()` method for both query and
* passage embeddings. With task-aware providers we treat this as passage.
*/
async embed(text: string): Promise<number[]> {
return this.embedPassage(text);
}
/** Backward-compatible batch embedding API (treated as passage). */
async embedBatch(texts: string[]): Promise<number[][]> {
return this.embedBatchPassage(texts);
}
// --------------------------------------------------------------------------
// Task-aware API
// --------------------------------------------------------------------------
async embedQuery(text: string): Promise<number[]> {
return this.withTimeout(this.embedSingle(text, this._taskQuery), "embedQuery");
}
async embedPassage(text: string): Promise<number[]> {
return this.withTimeout(this.embedSingle(text, this._taskPassage), "embedPassage");
}
async embedBatchQuery(texts: string[]): Promise<number[][]> {
return this.embedMany(texts, this._taskQuery);
}
async embedBatchPassage(texts: string[]): Promise<number[][]> {
return this.embedMany(texts, this._taskPassage);
}
// --------------------------------------------------------------------------
// Internals
// --------------------------------------------------------------------------
private validateEmbedding(embedding: number[]): void {
if (!Array.isArray(embedding)) {
throw new Error(`Embedding is not an array (got ${typeof embedding})`);
}
if (embedding.length !== this.dimensions) {
throw new Error(
`Embedding dimension mismatch: expected ${this.dimensions}, got ${embedding.length}`
);
}
}
private buildPayload(input: string | string[], task?: string): any {
const payload: any = {
model: this.model,
input,
// Force float output to avoid SDK default base64 decoding path.
encoding_format: "float",
};
if (task) payload.task = task;
if (this._normalized !== undefined) payload.normalized = this._normalized;
// Some OpenAI-compatible providers support requesting a specific vector size.
// We only pass it through when explicitly configured to avoid breaking providers
// that reject unknown fields.
if (this._requestDimensions && this._requestDimensions > 0) {
payload.dimensions = this._requestDimensions;
}
return payload;
}
private async embedSingle(text: string, task?: string, depth: number = 0): Promise<number[]> {
if (!text || text.trim().length === 0) {
throw new Error("Cannot embed empty text");
}
// FR-01: Recursion depth limit — force truncate when too deep
if (depth >= MAX_EMBED_DEPTH) {
const safeLimit = getSafeCharLimit(this._model);
console.warn(
`[memory-lancedb-pro] Recursion depth ${depth} reached MAX_EMBED_DEPTH (${MAX_EMBED_DEPTH}), ` +
`force-truncating ${text.length} chars → ${safeLimit} chars`
);
text = text.slice(0, safeLimit);
}
// Check cache first
const cached = this._cache.get(text, task);
if (cached) return cached;
try {
const response = await this.embedWithRetry(this.buildPayload(text, task));
const embedding = response.data[0]?.embedding as number[] | undefined;
if (!embedding) {
throw new Error("No embedding returned from provider");
}
this.validateEmbedding(embedding);
this._cache.set(text, task, embedding);
return embedding;
} catch (error) {
// Check if this is a context length exceeded error and try chunking
const errorMsg = error instanceof Error ? error.message : String(error);
const isContextError = /context|too long|exceed|length/i.test(errorMsg);
if (isContextError && this._autoChunk) {
try {
console.log(`Document exceeded context limit (${errorMsg}), attempting chunking...`);
const chunkResult = smartChunk(text, this._model);
if (chunkResult.chunks.length === 0) {
throw new Error(`Failed to chunk document: ${errorMsg}`);
}
// FR-03: Single chunk output detection — if smartChunk produced only
// one chunk that is nearly the same size as the original text, chunking
// did not actually reduce the problem. Force-truncate instead of
// recursing (which would loop forever).
if (
chunkResult.chunks.length === 1 &&
chunkResult.chunks[0].length > text.length * 0.9
) {
const safeLimit = getSafeCharLimit(this._model);
console.warn(
`[memory-lancedb-pro] smartChunk produced 1 chunk (${chunkResult.chunks[0].length} chars) ≈ original (${text.length} chars). ` +
`Force-truncating to ${safeLimit} chars to avoid infinite recursion.`
);
const truncated = text.slice(0, safeLimit);
return this.embedSingle(truncated, task, depth + 1);
}
// Embed all chunks in parallel
console.log(`Split document into ${chunkResult.chunkCount} chunks for embedding`);
const chunkEmbeddings = await Promise.all(
chunkResult.chunks.map(async (chunk, idx) => {
try {
const embedding = await this.embedSingle(chunk, task, depth + 1);
return { embedding };
} catch (chunkError) {
console.warn(`Failed to embed chunk ${idx}:`, chunkError);
throw chunkError;
}
})
);
// Compute average embedding across chunks
const avgEmbedding = chunkEmbeddings.reduce(
(sum, { embedding }) => {
for (let i = 0; i < embedding.length; i++) {
sum[i] += embedding[i];
}
return sum;
},
new Array(this.dimensions).fill(0)
);
const finalEmbedding = avgEmbedding.map(v => v / chunkEmbeddings.length);
// Cache the result for the original text (using its hash)
this._cache.set(text, task, finalEmbedding);
console.log(`Successfully embedded long document as ${chunkEmbeddings.length} averaged chunks`);
return finalEmbedding;
} catch (chunkError) {
// If chunking fails, throw the original error
console.warn(`Chunking failed, using original error:`, chunkError);
const friendly = formatEmbeddingProviderError(error, {
baseURL: this._baseURL,
model: this._model,
mode: "single",
});
throw new Error(friendly, { cause: error });
}
}
const friendly = formatEmbeddingProviderError(error, {
baseURL: this._baseURL,
model: this._model,
mode: "single",
});
throw new Error(friendly, { cause: error instanceof Error ? error : undefined });
}
}
private async embedMany(texts: string[], task?: string): Promise<number[][]> {
if (!texts || texts.length === 0) {
return [];
}
// Filter out empty texts and track indices
const validTexts: string[] = [];
const validIndices: number[] = [];
texts.forEach((text, index) => {
if (text && text.trim().length > 0) {
validTexts.push(text);
validIndices.push(index);
}
});
if (validTexts.length === 0) {
return texts.map(() => []);
}
try {
const response = await this.embedWithRetry(
this.buildPayload(validTexts, task)
);
// Create result array with proper length
const results: number[][] = new Array(texts.length);
// Fill in embeddings for valid texts
response.data.forEach((item, idx) => {
const originalIndex = validIndices[idx];
const embedding = item.embedding as number[];
this.validateEmbedding(embedding);
results[originalIndex] = embedding;
});
// Fill empty arrays for invalid texts
for (let i = 0; i < texts.length; i++) {
if (!results[i]) {
results[i] = [];
}
}
return results;
} catch (error) {
// Check if this is a context length exceeded error and try chunking each text
const errorMsg = error instanceof Error ? error.message : String(error);
const isContextError = /context|too long|exceed|length/i.test(errorMsg);
if (isContextError && this._autoChunk) {
try {
console.log(`Batch embedding failed with context error, attempting chunking...`);
const chunkResults = await Promise.all(
validTexts.map(async (text, idx) => {
const chunkResult = smartChunk(text, this._model);
if (chunkResult.chunks.length === 0) {
throw new Error("Chunker produced no chunks");
}
// Embed all chunks in parallel, then average.
const embeddings = await Promise.all(
chunkResult.chunks.map((chunk) => this.embedSingle(chunk, task))
);
const avgEmbedding = embeddings.reduce(
(sum, emb) => {
for (let i = 0; i < emb.length; i++) {
sum[i] += emb[i];
}
return sum;
},
new Array(this.dimensions).fill(0)
);
const finalEmbedding = avgEmbedding.map((v) => v / embeddings.length);
// Cache the averaged embedding for the original (long) text.
this._cache.set(text, task, finalEmbedding);
return { embedding: finalEmbedding, index: validIndices[idx] };
})
);
console.log(`Successfully chunked and embedded ${chunkResults.length} long documents`);
// Build results array
const results: number[][] = new Array(texts.length);
chunkResults.forEach(({ embedding, index }) => {
if (embedding.length > 0) {
this.validateEmbedding(embedding);
results[index] = embedding;
} else {
results[index] = [];
}
});
// Fill empty arrays for invalid texts
for (let i = 0; i < texts.length; i++) {
if (!results[i]) {
results[i] = [];
}
}
return results;
} catch (chunkError) {
const friendly = formatEmbeddingProviderError(error, {
baseURL: this._baseURL,
model: this._model,
mode: "batch",
});
throw new Error(`Failed to embed documents after chunking attempt: ${friendly}`, {
cause: error instanceof Error ? error : undefined,
});
}
}
const friendly = formatEmbeddingProviderError(error, {
baseURL: this._baseURL,
model: this._model,
mode: "batch",
});
throw new Error(friendly, {
cause: error instanceof Error ? error : undefined,
});
}
}
get model(): string {
return this._model;
}
// Test connection and validate configuration
async test(): Promise<{ success: boolean; error?: string; dimensions?: number }> {
try {
const testEmbedding = await this.embedPassage("test");
return {
success: true,
dimensions: testEmbedding.length,
};
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
}
get cacheStats() {
return {
...this._cache.stats,
keyCount: this.clients.length,
};
}
}
// ============================================================================
// Factory Function
// ============================================================================
export function createEmbedder(config: EmbeddingConfig): Embedder {
return new Embedder(config);
}