Skip to content

Commit 128ff86

Browse files
committed
👌 IMPROVE: Rate limit and batch handling
1 parent 8bcca1c commit 128ff86

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -682,23 +682,42 @@ export async function uploadDocumentsToMemory({
682682
name: string;
683683
account: Account;
684684
}) {
685-
for (const doc of documents) {
686-
try {
687-
p.log.message(`Uploading document: ${doc.name} ....`);
688-
await new Promise(resolve => setTimeout(resolve, 800)); // To avoid rate limiting
689-
const signedUrl = await getSignedUploadUrl({
690-
documentName: doc.name,
691-
memoryName: name,
692-
account
693-
});
685+
const BATCH_SIZE = 5; // Number of concurrent uploads
686+
const RATE_LIMIT_DELAY = 1000; // 1 second delay between requests
694687

695-
const uploadResponse = await uploadDocument(signedUrl, doc.blob);
696-
dlog(`Upload response status: ${uploadResponse.status}`);
688+
// Process documents in batches to avoid rate limiting
689+
for (let i = 0; i < documents.length; i += BATCH_SIZE) {
690+
const batch = documents.slice(i, i + BATCH_SIZE);
697691

698-
p.log.message(`Uploaded document: ${doc.name}`);
699-
} catch (error) {
700-
throw error;
701-
}
692+
const batchUploadPromises = batch.map(async (doc, index) => {
693+
try {
694+
// Stagger requests within batch
695+
await new Promise(resolve =>
696+
setTimeout(resolve, index * RATE_LIMIT_DELAY)
697+
);
698+
699+
// p.log.message(`Uploading document: ${doc.name} ....`);
700+
const signedUrl = await getSignedUploadUrl({
701+
documentName: doc.name,
702+
memoryName: name,
703+
account
704+
});
705+
706+
const uploadResponse = await uploadDocument(
707+
signedUrl,
708+
doc.blob
709+
);
710+
dlog(`Upload response status: ${uploadResponse.status}`);
711+
712+
p.log.message(`Uploaded document: ${doc.name}`);
713+
} catch (error: any) {
714+
throw new Error(
715+
`Failed to upload ${doc.name}: ${error.message ?? error}`
716+
);
717+
}
718+
});
719+
720+
await Promise.all(batchUploadPromises);
702721
}
703722
}
704723

0 commit comments

Comments
 (0)