@mastra/fastembed 1.3.5-alpha.2 → 1.3.5
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/fastembed.d.ts.map +1 -1
- package/dist/index.cjs +12 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +12 -4
- package/dist/index.js.map +1 -1
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -18352,6 +18352,7 @@ let SparseEmbeddingModel = /* @__PURE__ */ function(SparseEmbeddingModel) {
|
|
|
18352
18352
|
return SparseEmbeddingModel;
|
|
18353
18353
|
}({});
|
|
18354
18354
|
function normalize(v) {
|
|
18355
|
+
if (v.some((val) => !Number.isFinite(val))) throw new Error("Model produced a non-finite dense embedding value");
|
|
18355
18356
|
const norm = Math.sqrt(v.reduce((acc, val) => acc + val * val, 0));
|
|
18356
18357
|
const epsilon = 1e-12;
|
|
18357
18358
|
return v.map((val) => val / Math.max(norm, epsilon));
|
|
@@ -18366,7 +18367,7 @@ function getEmbeddings(data, dimensions, masks) {
|
|
|
18366
18367
|
for (let tokenIndex = 0; tokenIndex < sequenceLength; tokenIndex++) {
|
|
18367
18368
|
if (masks[batchIndex][tokenIndex] === 0n) continue;
|
|
18368
18369
|
tokenCount++;
|
|
18369
|
-
for (let dimension = 0; dimension < hiddenSize; dimension++) embedding[dimension]
|
|
18370
|
+
for (let dimension = 0; dimension < hiddenSize; dimension++) embedding[dimension] += data[start + tokenIndex * hiddenSize + dimension];
|
|
18370
18371
|
}
|
|
18371
18372
|
return embedding.map((value) => value / Math.max(tokenCount, 1));
|
|
18372
18373
|
});
|
|
@@ -18414,6 +18415,11 @@ function assertValidBatchSize(batchSize) {
|
|
|
18414
18415
|
function assertValidMaxLength(maxLength) {
|
|
18415
18416
|
if (!Number.isSafeInteger(maxLength) || maxLength <= 0) throw new RangeError(`maxLength must be a positive safe integer, received ${String(maxLength)}`);
|
|
18416
18417
|
}
|
|
18418
|
+
function getRequiredOutput(outputs, outputName) {
|
|
18419
|
+
const tensor = outputs[outputName];
|
|
18420
|
+
if (!tensor) throw new Error(`Expected model output "${outputName}" was not found. Available outputs: ${Object.keys(outputs).join(", ") || "(none)"}`);
|
|
18421
|
+
return tensor;
|
|
18422
|
+
}
|
|
18417
18423
|
function getOutputDimensions(outputName, tensor) {
|
|
18418
18424
|
if (tensor.dims.length !== 3) throw new Error(`Expected model output "${outputName}" to have 3 dimensions, received [${tensor.dims.join(", ")}]`);
|
|
18419
18425
|
return tensor.dims;
|
|
@@ -18527,7 +18533,7 @@ var FlagEmbedding = class FlagEmbedding extends Embedding {
|
|
|
18527
18533
|
token_type_ids: new ort.Tensor("int64", typeIdsArray.flat(), [batchTexts.length, maxLength])
|
|
18528
18534
|
};
|
|
18529
18535
|
if (this.model === "fast-multilingual-e5-large") delete inputs.token_type_ids;
|
|
18530
|
-
const lastHiddenState = (await this.session.run(inputs))
|
|
18536
|
+
const lastHiddenState = getRequiredOutput(await this.session.run(inputs), "last_hidden_state");
|
|
18531
18537
|
yield getEmbeddings(lastHiddenState.data, getOutputDimensions("last_hidden_state", lastHiddenState), this.model === "fast-all-MiniLM-L6-v2" || this.model === "fast-multilingual-e5-large" ? maskArray : void 0).map(normalize);
|
|
18532
18538
|
}
|
|
18533
18539
|
}
|
|
@@ -18669,14 +18675,16 @@ var SparseTextEmbedding = class SparseTextEmbedding extends SparseEmbedding {
|
|
|
18669
18675
|
input_mask: new ort.Tensor("int64", maskArray.flat().map(BigInt), [batchTexts.length, maxLength]),
|
|
18670
18676
|
segment_ids: new ort.Tensor("int64", typeIdsArray.flat(), [batchTexts.length, maxLength])
|
|
18671
18677
|
};
|
|
18672
|
-
const outputTensor = (await this.session.run(inputs))
|
|
18678
|
+
const outputTensor = getRequiredOutput(await this.session.run(inputs), "output");
|
|
18673
18679
|
const logits = outputTensor.cpuData;
|
|
18674
18680
|
const [currentBatchSize, seqLen, currentVocabSize] = getOutputDimensions("output", outputTensor);
|
|
18675
18681
|
const sparseVectors = [];
|
|
18676
18682
|
for (let batchIdx = 0; batchIdx < currentBatchSize; batchIdx++) {
|
|
18677
18683
|
const valuesArr = new Float32Array(currentVocabSize).fill(0);
|
|
18678
18684
|
for (let seqIdx = 0; seqIdx < seqLen; seqIdx++) if (maskArray[batchIdx][seqIdx] > 0) for (let vocabIdx = 0; vocabIdx < currentVocabSize; vocabIdx++) {
|
|
18679
|
-
const
|
|
18685
|
+
const logitIdx = batchIdx * seqLen * currentVocabSize + seqIdx * currentVocabSize + vocabIdx;
|
|
18686
|
+
const logitValue = logits[logitIdx];
|
|
18687
|
+
if (!Number.isFinite(logitValue)) throw new Error(`Model produced a non-finite sparse embedding value at index ${logitIdx}`);
|
|
18680
18688
|
const logValue = Math.log(1 + Math.max(0, logitValue));
|
|
18681
18689
|
valuesArr[vocabIdx] = Math.max(valuesArr[vocabIdx], logValue);
|
|
18682
18690
|
}
|