@n8n/n8n-nodes-langchain
Version:
45 lines • 1.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NvidiaEmbeddings = void 0;
const chunk_array_1 = require("@langchain/core/utils/chunk_array");
const openai_1 = require("@langchain/openai");
const n8n_workflow_1 = require("n8n-workflow");
class NvidiaEmbeddings extends openai_1.OpenAIEmbeddings {
async embedDocuments(texts) {
return await this.embedWithInputType(texts, 'passage');
}
async embedQuery(text) {
const [embedding] = await this.embedWithInputType([text], 'query');
return embedding;
}
async embedWithInputType(texts, inputType) {
const input = this.stripNewLines ? texts.map((text) => text.replace(/\n/g, ' ')) : texts;
const batches = (0, chunk_array_1.chunkArray)(input, this.batchSize);
const batchRequests = batches.map(async (batch) => {
const params = {
model: this.model,
input: batch,
input_type: inputType,
};
if (this.dimensions)
params.dimensions = this.dimensions;
if (this.encodingFormat)
params.encoding_format = this.encodingFormat;
const { data } = await this.embeddingWithRetry(params);
return { expected: batch.length, data };
});
const batchResponses = await Promise.all(batchRequests);
const embeddings = [];
for (const { expected, data } of batchResponses) {
if (data.length !== expected) {
throw new n8n_workflow_1.OperationalError(`NVIDIA embeddings API returned ${data.length} embeddings for a batch of ${expected} inputs`);
}
for (const entry of data) {
embeddings.push(entry.embedding);
}
}
return embeddings;
}
}
exports.NvidiaEmbeddings = NvidiaEmbeddings;
//# sourceMappingURL=helpers.js.map