From 18b157d4e62cc0174cec1e1c772f245bacb337cd Mon Sep 17 00:00:00 2001 From: QuietlyChan <1013893148@qq.com> Date: Wed, 19 Mar 2025 10:33:19 +0800 Subject: [PATCH] fix(uploads): Resolve the bug of large document attachment upload failure --- src/routes/uploads.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/routes/uploads.ts b/src/routes/uploads.ts index 7b063fc..5e5dc46 100644 --- a/src/routes/uploads.ts +++ b/src/routes/uploads.ts @@ -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(