Skip to content

Commit

Permalink
Limit the number of concurrent requests to Upstage API
Browse files Browse the repository at this point in the history
  • Loading branch information
GwonHyeok committed Feb 7, 2025
1 parent 5631ff0 commit 0b5b841
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/lib/health-data/parser/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,25 @@ Follow these guidelines to extract all actual test results:
}
}

async function processBatchWithConcurrency<T, R>(
items: T[],
processItem: (item: T) => Promise<R>,
concurrencyLimit: number
): Promise<R[]> {
const results: R[] = [];

// Process items in batches
for (let i = 0; i < items.length; i += concurrencyLimit) {
const batch = items.slice(i, i + concurrencyLimit);
const batchResults = await Promise.all(
batch.map(item => processItem(item))
);
results.push(...batchResults);
}

return results;
}

async function healthCheckupOCRWithGPTVisionMergeReport(
{file: filePath}: { file: string }
) {
Expand Down Expand Up @@ -297,7 +316,11 @@ async function healthCheckupOCRWithGPTVisionMergeReport(
const ocrResults = await documentOCR({document: filePath})

// prepare parse results
await Promise.all(imagePaths.map((path) => documentParse({document: path})))
await processBatchWithConcurrency(
imagePaths,
async (path) => documentParse({document: path}),
3
)

const [
{finalHealthCheckup: resultTotal, mergedTestResultPage: resultTotalPages},
Expand Down

0 comments on commit 0b5b841

Please sign in to comment.