Skip to content

Commit f799fd4

Browse files
committed
📦 NEW: Batch upload documents to memory
1 parent 2b3fa03 commit f799fd4

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

‎packages/baseai/src/deploy/index.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -643,24 +643,42 @@ export async function uploadDocumentsToMemory({
643643
name: string;
644644
account: Account;
645645
}) {
646-
for (const doc of documents) {
647-
try {
648-
p.log.message(`Uploading document: ${doc.name} ....`);
649-
await new Promise(resolve => setTimeout(resolve, 800)); // To avoid rate limiting
650-
const signedUrl = await getSignedUploadUrl({
651-
documentName: doc.name,
652-
memoryName: name,
653-
account,
654-
meta: doc.meta
655-
});
646+
const BATCH_SIZE = 5; // Number of concurrent uploads
647+
const RATE_LIMIT_DELAY = 1000; // 1 second delay between requests
648+
649+
// Process documents in batches to avoid rate limiting
650+
for (let i = 0; i < documents.length; i += BATCH_SIZE) {
651+
const batch = documents.slice(i, i + BATCH_SIZE);
652+
653+
const batchUploadPromises = batch.map(async (doc, index) => {
654+
try {
655+
// Stagger requests within batch
656+
await new Promise(resolve =>
657+
setTimeout(resolve, index * RATE_LIMIT_DELAY)
658+
);
659+
660+
// p.log.message(`Uploading document: ${doc.name} ....`);
661+
const signedUrl = await getSignedUploadUrl({
662+
documentName: doc.name,
663+
memoryName: name,
664+
account
665+
});
656666

657-
const uploadResponse = await uploadDocument(signedUrl, doc.blob);
658-
dlog(`Upload response status: ${uploadResponse.status}`);
667+
const uploadResponse = await uploadDocument(
668+
signedUrl,
669+
doc.blob
670+
);
671+
dlog(`Upload response status: ${uploadResponse.status}`);
659672

660-
p.log.message(`Uploaded document: ${doc.name}`);
661-
} catch (error) {
662-
throw error;
663-
}
673+
p.log.message(`Uploaded document: ${doc.name}`);
674+
} catch (error: any) {
675+
throw new Error(
676+
`Failed to upload ${doc.name}: ${error.message ?? error}`
677+
);
678+
}
679+
});
680+
681+
await Promise.all(batchUploadPromises);
664682
}
665683
}
666684

0 commit comments

Comments
 (0)