@langchain/core
Version:
Core LangChain.js abstractions and schemas
90 lines (88 loc) • 2.97 kB
JavaScript
const require_embeddings = require('../../embeddings.cjs');
//#region src/utils/testing/embeddings.ts
/**
* A class that provides synthetic embeddings by overriding the
* embedDocuments and embedQuery methods to generate embeddings based on
* the input documents. The embeddings are generated by converting each
* document into chunks, calculating a numerical value for each chunk, and
* returning an array of these values as the embedding.
*/
var SyntheticEmbeddings = class extends require_embeddings.Embeddings {
vectorSize;
constructor(params) {
super(params ?? {});
this.vectorSize = params?.vectorSize ?? 4;
}
/**
* Generates synthetic embeddings for a list of documents.
* @param documents List of documents to generate embeddings for.
* @returns A promise that resolves with a list of synthetic embeddings for each document.
*/
async embedDocuments(documents) {
return Promise.all(documents.map((doc) => this.embedQuery(doc)));
}
/**
* Generates a synthetic embedding for a document. The document is
* converted into chunks, a numerical value is calculated for each chunk,
* and an array of these values is returned as the embedding.
* @param document The document to generate an embedding for.
* @returns A promise that resolves with a synthetic embedding for the document.
*/
async embedQuery(document) {
let doc = document;
doc = doc.toLowerCase().replaceAll(/[^a-z ]/g, "");
const padMod = doc.length % this.vectorSize;
const padGapSize = padMod === 0 ? 0 : this.vectorSize - padMod;
const padSize = doc.length + padGapSize;
doc = doc.padEnd(padSize, " ");
const chunkSize = doc.length / this.vectorSize;
const docChunk = [];
for (let co = 0; co < doc.length; co += chunkSize) docChunk.push(doc.slice(co, co + chunkSize));
const ret = docChunk.map((s) => {
let sum = 0;
for (let co = 0; co < s.length; co += 1) sum += s === " " ? 0 : s.charCodeAt(co);
const ret$1 = sum % 26 / 26;
return ret$1;
});
return ret;
}
};
/**
* A class that provides fake embeddings by overriding the embedDocuments
* and embedQuery methods to return fixed values.
*/
var FakeEmbeddings = class extends require_embeddings.Embeddings {
constructor(params) {
super(params ?? {});
}
/**
* Generates fixed embeddings for a list of documents.
* @param documents List of documents to generate embeddings for.
* @returns A promise that resolves with a list of fixed embeddings for each document.
*/
embedDocuments(documents) {
return Promise.resolve(documents.map(() => [
.1,
.2,
.3,
.4
]));
}
/**
* Generates a fixed embedding for a query.
* @param _ The query to generate an embedding for.
* @returns A promise that resolves with a fixed embedding for the query.
*/
embedQuery(_) {
return Promise.resolve([
.1,
.2,
.3,
.4
]);
}
};
//#endregion
exports.FakeEmbeddings = FakeEmbeddings;
exports.SyntheticEmbeddings = SyntheticEmbeddings;
//# sourceMappingURL=embeddings.cjs.map