Repository navigation
Expand file tree
/
Copy pathembeddingUtils.js
More file actions
191 lines (170 loc) · 6.63 KB
/
Copy pathembeddingUtils.js
File metadata and controls
191 lines (170 loc) · 6.63 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
// ==========================================================================
// DEPRECATED (v3): this module's module-global provider/tokenizer singletons
// are no longer used by chunkit/cramit/sentenceit — the instance-based
// embedding-context.js replaced them (no globals, no per-call re-init, no
// cross-call content retention). It is kept only so pre-v3 deep importers of
// 'semantic-chunking/embeddingUtils.js' keep working. New code should use
// `createChunker` (chunker.js) or embedding-utils directly.
// ==========================================================================
import { createLocalProvider, createTokenizer } from 'embedding-utils';
import { LRUCache } from 'lru-cache';
let provider;
let tk;
const embeddingCache = new LRUCache({
max: 500,
maxSize: 50_000_000,
sizeCalculation: (value, key) => {
return (value.length * 4) + key.length;
},
ttl: 1000 * 60 * 60,
});
// --------------------------------------------
// -- Initialize tokenizer only (for embedCallback use) --
// --------------------------------------------
export async function initializeTokenizer(
onnxEmbeddingModel,
localModelPath = null,
modelCacheDir = null
) {
// Only initialize the tokenizer, not the embedding provider
tk = createTokenizer(onnxEmbeddingModel, {
modelPath: localModelPath,
cacheDir: modelCacheDir,
});
await tk.load();
return {
modelName: onnxEmbeddingModel,
};
}
// --------------------------------------------
// -- Initialize embedding model and tokenizer --
// --------------------------------------------
export async function initializeEmbeddingUtils(
onnxEmbeddingModel,
dtype = 'fp32',
device = 'cpu',
localModelPath = null,
modelCacheDir = null
) {
// Build the local ONNX embedding provider via embedding-utils.
// Pooling is resolved from eu's model registry ('mean' for the default
// Xenova/all-MiniLM-L6-v2); 'mean' is passed explicitly to guarantee
// parity with the former generateEmbedding({ pooling: 'mean' }) call.
provider = createLocalProvider({
model: onnxEmbeddingModel,
precision: dtype,
device: device,
modelPath: localModelPath,
cacheDir: modelCacheDir,
pooling: 'mean',
});
// Create and load the tokenizer used for token counting.
tk = createTokenizer(onnxEmbeddingModel, {
modelPath: localModelPath,
cacheDir: modelCacheDir,
});
await tk.load();
embeddingCache.clear();
return {
modelName: onnxEmbeddingModel,
dtype: dtype,
device: device,
};
}
// -------------------------------------
// -- Function to generate embeddings --
// -------------------------------------
export async function createEmbedding(text) {
const results = await createEmbeddingBatch([text]);
return results[0];
}
// ---------------------------------------------------
// -- Wrap user callback with cache for efficiency --
// ---------------------------------------------------
export function wrapCallbackWithCache(embedCallback, cache) {
return async function cachedCallback(texts) {
const results = new Array(texts.length);
const uncachedIndices = [];
const uncachedTexts = [];
// Check cache for each text
for (let i = 0; i < texts.length; i++) {
const cached = cache.get(texts[i]);
if (cached) {
results[i] = cached;
} else {
uncachedIndices.push(i);
uncachedTexts.push(texts[i]);
}
}
// Call original callback only for uncached texts
if (uncachedTexts.length > 0) {
const embeddings = await embedCallback(uncachedTexts);
for (let j = 0; j < uncachedTexts.length; j++) {
const embedding = embeddings[j];
cache.set(uncachedTexts[j], embedding);
results[uncachedIndices[j]] = embedding;
}
}
return results;
};
}
// ------------------------------------------------
// -- Validate callback return value shape --
// ------------------------------------------------
export function validateEmbeddingResult(texts, embeddings) {
if (!Array.isArray(embeddings) || embeddings.length !== texts.length) {
throw new Error(`embedCallback must return an array of embeddings matching input length. Expected ${texts.length}, got ${embeddings?.length}`);
}
for (let i = 0; i < embeddings.length; i++) {
const embedding = embeddings[i];
if (!Array.isArray(embedding) && !(embedding instanceof Float32Array) && !(embedding instanceof Float64Array)) {
throw new Error(`embedCallback must return an array of embeddings. Item at index ${i} is not an array.`);
}
// Check if elements are numbers (sample first few for performance)
const checkCount = Math.min(embedding.length, 3);
for (let j = 0; j < checkCount; j++) {
if (typeof embedding[j] !== 'number' || Number.isNaN(embedding[j])) {
throw new Error(`embedCallback embeddings must contain numbers. Found invalid value at index ${i}[${j}].`);
}
}
}
}
// ------------------------------------------------
// -- Function to generate embeddings in batches --
// ------------------------------------------------
export async function createEmbeddingBatch(texts, pipelineInstance = null, tokenizerInstance = null, options = {}) {
const embedProvider = pipelineInstance || provider;
const results = new Array(texts.length);
const uncachedIndices = [];
const uncachedTexts = [];
// Check cache for each text
for (let i = 0; i < texts.length; i++) {
const cached = embeddingCache.get(texts[i]);
if (cached) {
results[i] = cached;
} else {
uncachedIndices.push(i);
uncachedTexts.push(texts[i]);
}
}
// Process uncached texts in a single batch via the eu provider
if (uncachedTexts.length > 0) {
const { embeddings } = await embedProvider.embed(uncachedTexts);
for (let j = 0; j < uncachedTexts.length; j++) {
const embeddingData = embeddings[j];
embeddingCache.set(uncachedTexts[j], embeddingData);
results[uncachedIndices[j]] = embeddingData;
}
}
return results;
}
// ------------------------------------------------
// -- Token counting (delegates to eu tokenizer) --
// ------------------------------------------------
export function countTokens(text) {
return tk.count(text);
}
export function countTokensBatch(texts) {
return tk.countBatch(texts);
}
export { embeddingCache };