@aeye/cletus 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +221 -368
- package/dist/embed-worker.js +100 -0
- package/dist/index.js +27756 -9006
- package/package.json +114 -104
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
|
|
2
|
+
import { createRequire as __createRequire } from 'module';
|
|
3
|
+
import { fileURLToPath as __fileURLToPath } from 'url';
|
|
4
|
+
import { dirname as __dirname_func } from 'path';
|
|
5
|
+
const __filename = __fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = __dirname_func(__filename);
|
|
7
|
+
const require = __createRequire(import.meta.url);
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// src/embed-worker.ts
|
|
11
|
+
import { parentPort, workerData } from "worker_threads";
|
|
12
|
+
import { EmbeddingModel, FlagEmbedding } from "fastembed";
|
|
13
|
+
var embeddingModel = null;
|
|
14
|
+
var initPromise = null;
|
|
15
|
+
async function initModel(cacheDir) {
|
|
16
|
+
if (!initPromise) {
|
|
17
|
+
initPromise = (async () => {
|
|
18
|
+
try {
|
|
19
|
+
return await FlagEmbedding.init({
|
|
20
|
+
model: EmbeddingModel.BGEBaseENV15,
|
|
21
|
+
cacheDir
|
|
22
|
+
});
|
|
23
|
+
} catch (err) {
|
|
24
|
+
console.error("Worker: Failed to load embedding model:", err);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
})();
|
|
28
|
+
}
|
|
29
|
+
return initPromise;
|
|
30
|
+
}
|
|
31
|
+
async function handleMessage(message) {
|
|
32
|
+
const { type, id } = message;
|
|
33
|
+
try {
|
|
34
|
+
switch (type) {
|
|
35
|
+
case "init": {
|
|
36
|
+
embeddingModel = await initModel(message.cacheDir);
|
|
37
|
+
return {
|
|
38
|
+
type: "init",
|
|
39
|
+
id,
|
|
40
|
+
success: embeddingModel !== null,
|
|
41
|
+
result: embeddingModel !== null
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
case "canEmbed": {
|
|
45
|
+
return {
|
|
46
|
+
type: "canEmbed",
|
|
47
|
+
id,
|
|
48
|
+
success: true,
|
|
49
|
+
result: embeddingModel !== null
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
case "embed": {
|
|
53
|
+
if (!embeddingModel) {
|
|
54
|
+
return {
|
|
55
|
+
type: "embed",
|
|
56
|
+
id,
|
|
57
|
+
success: false,
|
|
58
|
+
error: "Embedding model not initialized"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const batches = embeddingModel.embed(message.texts);
|
|
62
|
+
const results = [];
|
|
63
|
+
for await (const batch of batches) {
|
|
64
|
+
results.push(...batch.map((x) => Array.prototype.slice.call(x)));
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
type: "embed",
|
|
68
|
+
id,
|
|
69
|
+
success: true,
|
|
70
|
+
result: results
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
default:
|
|
74
|
+
return {
|
|
75
|
+
type,
|
|
76
|
+
id,
|
|
77
|
+
success: false,
|
|
78
|
+
error: `Unknown message type: ${type}`
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return {
|
|
83
|
+
type,
|
|
84
|
+
id,
|
|
85
|
+
success: false,
|
|
86
|
+
error: error.message
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (parentPort) {
|
|
91
|
+
parentPort.on("message", async (message) => {
|
|
92
|
+
const response = await handleMessage(message);
|
|
93
|
+
parentPort.postMessage(response);
|
|
94
|
+
});
|
|
95
|
+
if (workerData?.cacheDir) {
|
|
96
|
+
initModel(workerData.cacheDir).then((model) => {
|
|
97
|
+
embeddingModel = model;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|