ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
16 lines (15 loc) • 764 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.upsertTextChunks = void 0;
const nanoid_1 = require("nanoid");
const embedText_js_1 = require("../model-function/embed-text/embedText.cjs");
async function upsertTextChunks({ vectorIndex, embeddingModel, generateId = nanoid_1.nanoid, chunks, ids, }, options) {
// many embedding models support bulk embedding, so we first embed all texts:
const { embeddings } = await (0, embedText_js_1.embedTexts)(embeddingModel, chunks.map((chunk) => chunk.content), options);
await vectorIndex.upsertMany(chunks.map((chunk, i) => ({
id: ids?.[i] ?? generateId(),
vector: embeddings[i],
data: chunk,
})));
}
exports.upsertTextChunks = upsertTextChunks;