@mastra/fastembed 1.3.4-alpha.0 → 1.3.4-alpha.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 +4 -0
- package/dist/fastembed.d.ts +7 -0
- package/dist/fastembed.d.ts.map +1 -1
- package/dist/index.cjs +48 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +49 -31
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI9E,OAAO,EACL,cAAc,EACd,aAAa,EACb,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnF,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,MAAM,kBAE3B;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAI9E,OAAO,EACL,cAAc,EACd,aAAa,EACb,mBAAmB,EACnB,oBAAoB,EACpB,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnF,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC9E,YAAY,EAAE,cAAc,IAAI,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,MAAM,kBAE3B;AAoJD,eAAO,MAAM,SAAS,EAAE,gBAAgB,GAAG;IACzC,KAAK,EAAE,gBAAgB,CAAC;IACxB,IAAI,EAAE,gBAAgB,CAAC;IACvB,wBAAwB,EAAE,gBAAgB,CAAC;IAC3C,0BAA0B,EAAE,gBAAgB,CAAC;IAC7C,OAAO,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAClC,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACjC,WAAW,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACtC,UAAU,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;CAUrC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import os from "os";
|
|
|
3
3
|
import path from "path";
|
|
4
4
|
import { AddedToken, Tokenizer } from "@anush008/tokenizers";
|
|
5
5
|
import fs from "fs";
|
|
6
|
+
import { Readable } from "stream";
|
|
7
|
+
import { pipeline } from "stream/promises";
|
|
6
8
|
import * as ort from "onnxruntime-node";
|
|
7
|
-
import {
|
|
9
|
+
import { downloadFile } from "@huggingface/hub";
|
|
8
10
|
//#region ../../../../../setup-pnpm/node_modules/.bin/store/v11/links/@/zod/4.6.5/6448fd814698825170f93bd428080c609bd45ccbc007f4c2dfd5fac1bb52f28d/node_modules/zod/v4/core/util.js
|
|
9
11
|
function getEnumValues(entries) {
|
|
10
12
|
const numericValues = Object.values(entries).filter((v) => typeof v === "number");
|
|
@@ -21288,6 +21290,25 @@ var FlagEmbedding = class FlagEmbedding extends Embedding {
|
|
|
21288
21290
|
});
|
|
21289
21291
|
return new FlagEmbedding(tokenizer, session, model);
|
|
21290
21292
|
}
|
|
21293
|
+
/**
|
|
21294
|
+
* Counts texts longer than the model's token window. `embed()` silently truncates these to `maxLength`.
|
|
21295
|
+
*/
|
|
21296
|
+
async countTruncated(textStrings, batchSize = 256) {
|
|
21297
|
+
let count = 0;
|
|
21298
|
+
let maxLength = 0;
|
|
21299
|
+
for (let i = 0; i < textStrings.length; i += batchSize) {
|
|
21300
|
+
const batch = textStrings.slice(i, i + batchSize);
|
|
21301
|
+
const encodings = await Promise.all(batch.map((textString) => this.tokenizer.encode(textString)));
|
|
21302
|
+
for (const encoding of encodings) if (encoding.getOverflowing().length > 0) {
|
|
21303
|
+
count++;
|
|
21304
|
+
maxLength = encoding.getLength();
|
|
21305
|
+
}
|
|
21306
|
+
}
|
|
21307
|
+
return {
|
|
21308
|
+
count,
|
|
21309
|
+
maxLength
|
|
21310
|
+
};
|
|
21311
|
+
}
|
|
21291
21312
|
static async retrieveModel(model, cacheDir, showDownloadProgress = true) {
|
|
21292
21313
|
if (model === "custom") throw new Error("Custom models must be loaded from modelAbsoluteDirPath");
|
|
21293
21314
|
if (!fs.existsSync(cacheDir)) fs.mkdirSync(cacheDir, {
|
|
@@ -21298,21 +21319,16 @@ var FlagEmbedding = class FlagEmbedding extends Embedding {
|
|
|
21298
21319
|
if (fs.existsSync(modelDir)) return modelDir;
|
|
21299
21320
|
const { repo, files } = HF_MODEL_SOURCES[model];
|
|
21300
21321
|
const tmpDir = fs.mkdtempSync(`${modelDir}.tmp-`);
|
|
21301
|
-
const hubCacheDir = path.join(tmpDir, ".hf-hub");
|
|
21302
21322
|
try {
|
|
21303
|
-
if (showDownloadProgress) console.
|
|
21323
|
+
if (showDownloadProgress) console.error(`Downloading ${model} from Hugging Face (${repo})...`);
|
|
21304
21324
|
for (const file of [...COMMON_MODEL_FILES, ...files]) {
|
|
21305
|
-
const downloaded = await
|
|
21325
|
+
const downloaded = await downloadFile({
|
|
21306
21326
|
repo,
|
|
21307
|
-
path: file
|
|
21308
|
-
cacheDir: hubCacheDir
|
|
21327
|
+
path: file
|
|
21309
21328
|
});
|
|
21310
|
-
|
|
21329
|
+
if (!downloaded) throw new Error(`Model file not found in ${repo}: ${file}`);
|
|
21330
|
+
await pipeline(Readable.fromWeb(downloaded.stream()), fs.createWriteStream(path.join(tmpDir, file)));
|
|
21311
21331
|
}
|
|
21312
|
-
fs.rmSync(hubCacheDir, {
|
|
21313
|
-
recursive: true,
|
|
21314
|
-
force: true
|
|
21315
|
-
});
|
|
21316
21332
|
try {
|
|
21317
21333
|
fs.renameSync(tmpDir, modelDir);
|
|
21318
21334
|
} catch (error) {
|
|
@@ -21450,11 +21466,12 @@ var SparseTextEmbedding = class SparseTextEmbedding extends SparseEmbedding {
|
|
|
21450
21466
|
recursive: true,
|
|
21451
21467
|
mode: 493
|
|
21452
21468
|
});
|
|
21453
|
-
const downloaded = await
|
|
21469
|
+
const downloaded = await downloadFile({
|
|
21454
21470
|
repo: model,
|
|
21455
21471
|
path: fileName
|
|
21456
21472
|
});
|
|
21457
|
-
if (downloaded
|
|
21473
|
+
if (!downloaded) throw new Error(`Model file not found in ${model}: ${fileName}`);
|
|
21474
|
+
await pipeline(Readable.fromWeb(downloaded.stream()), fs.createWriteStream(outputPath));
|
|
21458
21475
|
}
|
|
21459
21476
|
return modelDir;
|
|
21460
21477
|
}
|
|
@@ -21560,8 +21577,21 @@ async function generateEmbeddings(values, modelType) {
|
|
|
21560
21577
|
if (allResults.length === 0) throw new Error("No embeddings generated");
|
|
21561
21578
|
return { embeddings: allResults };
|
|
21562
21579
|
}
|
|
21563
|
-
|
|
21564
|
-
return
|
|
21580
|
+
function applyPrefix(values, prefix) {
|
|
21581
|
+
return prefix ? values.map((value) => `${prefix}: ${value}`) : values;
|
|
21582
|
+
}
|
|
21583
|
+
async function generateEmbeddingsWithWarnings(values, modelType, prefix) {
|
|
21584
|
+
const inputs = applyPrefix(values, prefix);
|
|
21585
|
+
const result = await generateEmbeddings(inputs, modelType);
|
|
21586
|
+
const { count, maxLength } = await (await getCachedModel(modelType)).countTruncated(inputs);
|
|
21587
|
+
const warnings = count > 0 ? [{
|
|
21588
|
+
type: "other",
|
|
21589
|
+
message: `${count} of ${inputs.length} inputs exceed the model's ${maxLength}-token window and were truncated; text beyond it does not affect the embedding. Use smaller chunks to embed the full text.`
|
|
21590
|
+
}] : [];
|
|
21591
|
+
return {
|
|
21592
|
+
...result,
|
|
21593
|
+
warnings
|
|
21594
|
+
};
|
|
21565
21595
|
}
|
|
21566
21596
|
const fastEmbedLegacyProvider = customProvider$1({ textEmbeddingModels: {
|
|
21567
21597
|
"bge-small-en-v1.5": {
|
|
@@ -21615,10 +21645,7 @@ const fastEmbedProviderV3 = customProvider({ embeddingModels: {
|
|
|
21615
21645
|
maxEmbeddingsPerCall: 256,
|
|
21616
21646
|
supportsParallelCalls: true,
|
|
21617
21647
|
async doEmbed({ values }) {
|
|
21618
|
-
return
|
|
21619
|
-
...await generateEmbeddings(values, "BGESmallENV15"),
|
|
21620
|
-
warnings: []
|
|
21621
|
-
};
|
|
21648
|
+
return generateEmbeddingsWithWarnings(values, "BGESmallENV15");
|
|
21622
21649
|
}
|
|
21623
21650
|
},
|
|
21624
21651
|
"bge-base-en-v1.5": {
|
|
@@ -21628,10 +21655,7 @@ const fastEmbedProviderV3 = customProvider({ embeddingModels: {
|
|
|
21628
21655
|
maxEmbeddingsPerCall: 256,
|
|
21629
21656
|
supportsParallelCalls: true,
|
|
21630
21657
|
async doEmbed({ values }) {
|
|
21631
|
-
return
|
|
21632
|
-
...await generateEmbeddings(values, "BGEBaseENV15"),
|
|
21633
|
-
warnings: []
|
|
21634
|
-
};
|
|
21658
|
+
return generateEmbeddingsWithWarnings(values, "BGEBaseENV15");
|
|
21635
21659
|
}
|
|
21636
21660
|
},
|
|
21637
21661
|
"multilingual-e5-large-query": {
|
|
@@ -21641,10 +21665,7 @@ const fastEmbedProviderV3 = customProvider({ embeddingModels: {
|
|
|
21641
21665
|
maxEmbeddingsPerCall: 256,
|
|
21642
21666
|
supportsParallelCalls: true,
|
|
21643
21667
|
async doEmbed({ values }) {
|
|
21644
|
-
return
|
|
21645
|
-
...await generatePrefixedEmbeddings(values, "MLE5Large", "query"),
|
|
21646
|
-
warnings: []
|
|
21647
|
-
};
|
|
21668
|
+
return generateEmbeddingsWithWarnings(values, "MLE5Large", "query");
|
|
21648
21669
|
}
|
|
21649
21670
|
},
|
|
21650
21671
|
"multilingual-e5-large-passage": {
|
|
@@ -21654,10 +21675,7 @@ const fastEmbedProviderV3 = customProvider({ embeddingModels: {
|
|
|
21654
21675
|
maxEmbeddingsPerCall: 256,
|
|
21655
21676
|
supportsParallelCalls: true,
|
|
21656
21677
|
async doEmbed({ values }) {
|
|
21657
|
-
return
|
|
21658
|
-
...await generatePrefixedEmbeddings(values, "MLE5Large", "passage"),
|
|
21659
|
-
warnings: []
|
|
21660
|
-
};
|
|
21678
|
+
return generateEmbeddingsWithWarnings(values, "MLE5Large", "passage");
|
|
21661
21679
|
}
|
|
21662
21680
|
}
|
|
21663
21681
|
} });
|