fix(uploads): Resolve the bug of large document attachment upload failure
This commit is contained in:
parent
64d2a467b0
commit
18b157d4e6
1 changed files with 17 additions and 4 deletions
|
|
@ -115,13 +115,26 @@ router.post(
|
|||
const pathToSave = file.path.replace(/\.\w+$/, '-extracted.json');
|
||||
fs.writeFileSync(pathToSave, json);
|
||||
|
||||
const embeddings = await embeddingsModel.embedDocuments(
|
||||
splitted.map((doc) => doc.pageContent),
|
||||
const batchSize = 32; // Maximum allowable batch size
|
||||
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({
|
||||
title: file.originalname,
|
||||
embeddings: embeddings,
|
||||
embeddings: allEmbeddings,
|
||||
});
|
||||
|
||||
const pathToSaveEmbeddings = file.path.replace(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue