forbocai 0.3.19 → 0.4.0
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/dist/index.js +37 -5
- package/dist/index.mjs +37 -5
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -202313,6 +202313,13 @@ var createNativeCortex = (config) => {
|
|
|
202313
202313
|
let model;
|
|
202314
202314
|
let context;
|
|
202315
202315
|
let session;
|
|
202316
|
+
let embeddingContext;
|
|
202317
|
+
const createEmbeddingContext = async () => {
|
|
202318
|
+
if (!model.embeddingSupported) {
|
|
202319
|
+
throw new Error("Model does not support embeddings");
|
|
202320
|
+
}
|
|
202321
|
+
embeddingContext = await model.createEmbeddingContext();
|
|
202322
|
+
};
|
|
202316
202323
|
const MODELS_DIR = path.join(process.cwd(), "local_infrastructure", "models");
|
|
202317
202324
|
const init3 = async () => {
|
|
202318
202325
|
if (status.ready) return status;
|
|
@@ -202341,6 +202348,12 @@ var createNativeCortex = (config) => {
|
|
|
202341
202348
|
});
|
|
202342
202349
|
console.log("> Creating context...");
|
|
202343
202350
|
context = await model.createContext();
|
|
202351
|
+
if (model.embeddingSupported) {
|
|
202352
|
+
console.log("> Creating embedding context...");
|
|
202353
|
+
embeddingContext = await model.createEmbeddingContext();
|
|
202354
|
+
} else {
|
|
202355
|
+
console.log("> Model does not support embeddings");
|
|
202356
|
+
}
|
|
202344
202357
|
session = new LlamaChatSession({
|
|
202345
202358
|
contextSequence: context.getSequence()
|
|
202346
202359
|
});
|
|
@@ -202404,9 +202417,14 @@ var createNativeCortex = (config) => {
|
|
|
202404
202417
|
const embed = async (text) => {
|
|
202405
202418
|
if (!status.ready) await init3();
|
|
202406
202419
|
try {
|
|
202407
|
-
|
|
202420
|
+
if (!embeddingContext) {
|
|
202421
|
+
await createEmbeddingContext();
|
|
202422
|
+
}
|
|
202423
|
+
const embeddings = await embeddingContext.getEmbeddingFor(text);
|
|
202424
|
+
return Array.from(embeddings);
|
|
202408
202425
|
} catch (e2) {
|
|
202409
|
-
|
|
202426
|
+
console.warn("Embedding generation failed, falling back to random vector:", e2);
|
|
202427
|
+
return new Array(384).fill(0).map(() => Math.random());
|
|
202410
202428
|
}
|
|
202411
202429
|
};
|
|
202412
202430
|
return {
|
|
@@ -202809,9 +202827,23 @@ var initVectorEngine = async () => {
|
|
|
202809
202827
|
}
|
|
202810
202828
|
};
|
|
202811
202829
|
var generateEmbedding = async (text) => {
|
|
202812
|
-
|
|
202813
|
-
|
|
202814
|
-
|
|
202830
|
+
const cortexConfig = {
|
|
202831
|
+
model: "smollm2-135m",
|
|
202832
|
+
temperature: 0.7,
|
|
202833
|
+
maxTokens: 128,
|
|
202834
|
+
gpu: true
|
|
202835
|
+
};
|
|
202836
|
+
const cortex = createCortex(cortexConfig);
|
|
202837
|
+
try {
|
|
202838
|
+
await cortex.init();
|
|
202839
|
+
const embedding = await cortex.embed(text);
|
|
202840
|
+
return embedding;
|
|
202841
|
+
} catch (e2) {
|
|
202842
|
+
console.warn("Embedding generation failed, falling back to transformers:", e2);
|
|
202843
|
+
if (!pipeline) await initVectorEngine();
|
|
202844
|
+
const output = await pipeline(text, { pooling: "mean", normalize: true });
|
|
202845
|
+
return Array.from(output.data);
|
|
202846
|
+
}
|
|
202815
202847
|
};
|
|
202816
202848
|
var getVectorTable = async (dbPath, tableName = "memories") => {
|
|
202817
202849
|
if (!lancedb) await initVectorEngine();
|
package/dist/index.mjs
CHANGED
|
@@ -51,6 +51,13 @@ var createNativeCortex = (config) => {
|
|
|
51
51
|
let model;
|
|
52
52
|
let context;
|
|
53
53
|
let session;
|
|
54
|
+
let embeddingContext;
|
|
55
|
+
const createEmbeddingContext = async () => {
|
|
56
|
+
if (!model.embeddingSupported) {
|
|
57
|
+
throw new Error("Model does not support embeddings");
|
|
58
|
+
}
|
|
59
|
+
embeddingContext = await model.createEmbeddingContext();
|
|
60
|
+
};
|
|
54
61
|
const MODELS_DIR = path.join(process.cwd(), "local_infrastructure", "models");
|
|
55
62
|
const init2 = async () => {
|
|
56
63
|
if (status.ready) return status;
|
|
@@ -79,6 +86,12 @@ var createNativeCortex = (config) => {
|
|
|
79
86
|
});
|
|
80
87
|
console.log("> Creating context...");
|
|
81
88
|
context = await model.createContext();
|
|
89
|
+
if (model.embeddingSupported) {
|
|
90
|
+
console.log("> Creating embedding context...");
|
|
91
|
+
embeddingContext = await model.createEmbeddingContext();
|
|
92
|
+
} else {
|
|
93
|
+
console.log("> Model does not support embeddings");
|
|
94
|
+
}
|
|
82
95
|
session = new LlamaChatSession({
|
|
83
96
|
contextSequence: context.getSequence()
|
|
84
97
|
});
|
|
@@ -142,9 +155,14 @@ var createNativeCortex = (config) => {
|
|
|
142
155
|
const embed = async (text) => {
|
|
143
156
|
if (!status.ready) await init2();
|
|
144
157
|
try {
|
|
145
|
-
|
|
158
|
+
if (!embeddingContext) {
|
|
159
|
+
await createEmbeddingContext();
|
|
160
|
+
}
|
|
161
|
+
const embeddings = await embeddingContext.getEmbeddingFor(text);
|
|
162
|
+
return Array.from(embeddings);
|
|
146
163
|
} catch (e) {
|
|
147
|
-
|
|
164
|
+
console.warn("Embedding generation failed, falling back to random vector:", e);
|
|
165
|
+
return new Array(384).fill(0).map(() => Math.random());
|
|
148
166
|
}
|
|
149
167
|
};
|
|
150
168
|
return {
|
|
@@ -547,9 +565,23 @@ var initVectorEngine = async () => {
|
|
|
547
565
|
}
|
|
548
566
|
};
|
|
549
567
|
var generateEmbedding = async (text) => {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
568
|
+
const cortexConfig = {
|
|
569
|
+
model: "smollm2-135m",
|
|
570
|
+
temperature: 0.7,
|
|
571
|
+
maxTokens: 128,
|
|
572
|
+
gpu: true
|
|
573
|
+
};
|
|
574
|
+
const cortex = createCortex(cortexConfig);
|
|
575
|
+
try {
|
|
576
|
+
await cortex.init();
|
|
577
|
+
const embedding = await cortex.embed(text);
|
|
578
|
+
return embedding;
|
|
579
|
+
} catch (e) {
|
|
580
|
+
console.warn("Embedding generation failed, falling back to transformers:", e);
|
|
581
|
+
if (!pipeline) await initVectorEngine();
|
|
582
|
+
const output = await pipeline(text, { pooling: "mean", normalize: true });
|
|
583
|
+
return Array.from(output.data);
|
|
584
|
+
}
|
|
553
585
|
};
|
|
554
586
|
var getVectorTable = async (dbPath, tableName = "memories") => {
|
|
555
587
|
if (!lancedb) await initVectorEngine();
|