-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembed-worker.js
More file actions
53 lines (46 loc) · 1.77 KB
/
embed-worker.js
File metadata and controls
53 lines (46 loc) · 1.77 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
import { parentPort } from "worker_threads";
import { pipeline } from "@huggingface/transformers";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { statSync, rmSync } from "fs";
const __dirname = dirname(fileURLToPath(import.meta.url));
const MODEL_DIR = join(__dirname, "node_modules", "@huggingface", "transformers", ".cache",
"Xenova", "gte-small");
const MODEL_PATH = join(MODEL_DIR, "onnx", "model_quantized.onnx");
const MIN_MODEL_SIZE = 5_000_000; // 5MB — real model is ~34MB
// Delete corrupt/truncated model so the library re-downloads it
try {
const size = statSync(MODEL_PATH).size;
if (size < MIN_MODEL_SIZE) {
rmSync(MODEL_DIR, { recursive: true, force: true });
}
} catch {}
let extractor = null;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function getExtractor() {
if (!extractor) {
// Retry with backoff — Windows Defender can lock newly downloaded files
for (let attempt = 1; attempt <= 3; attempt++) {
try {
extractor = await pipeline("feature-extraction", "Xenova/gte-small", { dtype: "q8" });
parentPort.postMessage({ type: "ready" });
return extractor;
} catch (err) {
if (attempt === 3) throw err;
await sleep(attempt * 3000);
}
}
}
return extractor;
}
// Pre-warm the model on startup
getExtractor().catch((err) => {
parentPort.postMessage({ type: "error", message: err.message });
});
parentPort.on("message", async ({ id, text }) => {
const ext = await getExtractor();
const truncated = text.length > 2000 ? text.slice(0, 2000) : text;
const output = await ext(truncated, { pooling: "mean", normalize: true });
const buffer = Buffer.from(output.data.buffer);
parentPort.postMessage({ id, embedding: buffer });
});