Merge 18b157d4e6 into 64d2a467b0
This commit is contained in:
commit
b9a4f71228
1 changed files with 17 additions and 4 deletions
|
|
@ -115,13 +115,26 @@ router.post(
|
||||||
const pathToSave = file.path.replace(/\.\w+$/, '-extracted.json');
|
const pathToSave = file.path.replace(/\.\w+$/, '-extracted.json');
|
||||||
fs.writeFileSync(pathToSave, json);
|
fs.writeFileSync(pathToSave, json);
|
||||||
|
|
||||||
const embeddings = await embeddingsModel.embedDocuments(
|
const batchSize = 32; // Maximum allowable batch size
|
||||||
splitted.map((doc) => doc.pageContent),
|
const batches: Document[][] = [];
|
||||||
);
|
|
||||||
|
// Split the document into multiple batches
|
||||||
|
for (let i = 0; i < splitted.length; i += batchSize) {
|
||||||
|
batches.push(splitted.slice(i, i + batchSize));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch generate embeddings and merge results
|
||||||
|
const allEmbeddings: number[][] = [];
|
||||||
|
for (const batch of batches) {
|
||||||
|
const batchEmbeddings = await embeddingsModel.embedDocuments(
|
||||||
|
batch.map((doc) => doc.pageContent),
|
||||||
|
);
|
||||||
|
allEmbeddings.push(...batchEmbeddings);
|
||||||
|
}
|
||||||
|
|
||||||
const embeddingsJSON = JSON.stringify({
|
const embeddingsJSON = JSON.stringify({
|
||||||
title: file.originalname,
|
title: file.originalname,
|
||||||
embeddings: embeddings,
|
embeddings: allEmbeddings,
|
||||||
});
|
});
|
||||||
|
|
||||||
const pathToSaveEmbeddings = file.path.replace(
|
const pathToSaveEmbeddings = file.path.replace(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue