瀏覽代碼

fix(nodes): exponential backoff for Ollama retries

Ollama's cloud tier returns transient 500s at a rate that depends heavily on
request spacing. Measured directly against the endpoint: 5 of 8 back-to-back
requests failed, versus 1 of 4 when spaced 15s apart. The same image and
payload returned 500, 200, 200, so it is not content-related, and a request
carrying no system prompt failed too.

A loop dispatching images back-to-back hit the worst case. Replaces the fixed
retry delay with exponential backoff, capped by a new retryMaxDelayMs, so
retries spread out instead of hammering. Across three subsequent 11-image
runs: 33 of 33 succeeded, 5 needing a second attempt and none a third.
fszontagh 1 月之前
父節點
當前提交
bd7a536159
共有 1 個文件被更改,包括 16 次插入 和 2 次删除
  1. 16 2
      nodes/ai/ollama-chat.js

+ 16 - 2
nodes/ai/ollama-chat.js

@@ -80,9 +80,15 @@ const configSchema = {
         retryDelayMs: {
             type: 'number',
             title: 'Retry Delay (ms)',
-            description: 'Pause before each retry attempt',
+            description: 'Pause before the first retry. Doubles on each further attempt.',
             default: 2000
         },
+        retryMaxDelayMs: {
+            type: 'number',
+            title: 'Max Retry Delay (ms)',
+            description: 'Upper bound for the exponential backoff',
+            default: 30000
+        },
         skipOnError: {
             type: 'boolean',
             title: 'Skip On Error',
@@ -293,7 +299,15 @@ module.exports = {
                 parsed = null;
                 smartbotic.log.warn('ollama-chat: attempt ' + attempt + ' of ' + attempts + ' failed: ' + lastError);
                 if (attempt < attempts) {
-                    smartbotic.utils.sleep(Number(config.retryDelayMs) || 2000);
+                    // Exponential backoff. Ollama's cloud tier returns transient 500s far
+                    // more often under rapid succession, so spacing retries out recovers
+                    // markedly better than hammering at a fixed interval.
+                    const base = Number(config.retryDelayMs) || 2000;
+                    const cap = Number(config.retryMaxDelayMs) || 30000;
+                    let delay = base * Math.pow(2, attempt - 1);
+                    if (delay > cap) { delay = cap; }
+                    smartbotic.log.info('ollama-chat: backing off ' + delay + 'ms before retry');
+                    smartbotic.utils.sleep(delay);
                 }
             }
         }