9797 cursor = result.repository.discussions.pageInfo.endCursor;
9898
9999 // Limit to prevent excessive API calls (adjust as needed)
100- if (allDiscussions.length >= 500 ) break;
100+ if (allDiscussions.length >= 1000 ) break;
101101 }
102102
103103 console.log(`Fetched ${allDiscussions.length} existing discussions (excluding ignored categories)`);
@@ -119,34 +119,43 @@ jobs:
119119 // Prepare texts for batch embedding with indices
120120 const existingTexts = existingDiscussions.map(d => `${d.title}\n${d.body || ''}`);
121121
122- // Process in batches of 100
123- const BATCH_SIZE = 100;
122+ // Process in batches of 50
123+ const BATCH_SIZE = 50;
124+ const MAX_RETRIES = 3;
124125 const discussionEmbeddings = []; // Array of {discussion, embedding} pairs
125126
126127 for (let i = 0; i < existingTexts.length; i += BATCH_SIZE) {
127128 const batchStart = i;
128129 const batchEnd = Math.min(i + BATCH_SIZE, existingTexts.length);
129130 const batch = existingTexts.slice(batchStart, batchEnd);
130131
131- try {
132- const batchEmbeddings = await getEmbedding(batch, apiKey);
133- if (!Array.isArray(batchEmbeddings) || batchEmbeddings.length !== batch.length) {
134- throw new Error(`Expected ${batch.length} embeddings, got ${batchEmbeddings?.length || 0}`);
135- }
136- // Map embeddings back to their discussions
137- for (let j = 0; j < batchEmbeddings.length; j++) {
138- discussionEmbeddings.push({
139- discussion: existingDiscussions[batchStart + j],
140- embedding: batchEmbeddings[j]
141- });
142- }
143- // Small delay between batches to avoid rate limits
144- if (i + BATCH_SIZE < existingTexts.length) {
145- await new Promise(resolve => setTimeout(resolve, 500));
132+ let success = false;
133+ for (let attempt = 1; attempt <= MAX_RETRIES && !success; attempt++) {
134+ try {
135+ const batchEmbeddings = await getEmbedding(batch, apiKey);
136+ if (!Array.isArray(batchEmbeddings) || batchEmbeddings.length !== batch.length) {
137+ throw new Error(`Expected ${batch.length} embeddings, got ${batchEmbeddings?.length || 0}`);
138+ }
139+ // Map embeddings back to their discussions
140+ for (let j = 0; j < batchEmbeddings.length; j++) {
141+ discussionEmbeddings.push({
142+ discussion: existingDiscussions[batchStart + j],
143+ embedding: batchEmbeddings[j]
144+ });
145+ }
146+ success = true;
147+ } catch (batchError) {
148+ console.error(`Error processing batch ${Math.floor(i / BATCH_SIZE) + 1} (discussions ${batchStart}-${batchEnd - 1}), attempt ${attempt}/${MAX_RETRIES}:`, batchError);
149+ if (attempt < MAX_RETRIES) {
150+ const backoff = Math.pow(2, attempt) * 1000;
151+ console.log(`Retrying in ${backoff / 1000}s...`);
152+ await new Promise(resolve => setTimeout(resolve, backoff));
153+ }
146154 }
147- } catch (batchError) {
148- console.error(`Error processing batch ${Math.floor(i / BATCH_SIZE) + 1} (discussions ${batchStart}-${batchEnd - 1}):`, batchError);
149- // Skip this batch and continue with next one
155+ }
156+ // Delay between batches to avoid rate limits
157+ if (i + BATCH_SIZE < existingTexts.length) {
158+ await new Promise(resolve => setTimeout(resolve, 1000));
150159 }
151160 }
152161
@@ -197,7 +206,7 @@ jobs:
197206 ? text.map(t => t.substring(0, 8000))
198207 : text.substring(0, 8000)
199208 }),
200- signal: AbortSignal.timeout(30000 ) // 30s timeout
209+ signal: AbortSignal.timeout(60000 ) // 60s timeout
201210 });
202211
203212 if (!response.ok) {
0 commit comments