hansva commented on code in PR #9371:
URL: https://github.com/apache/hop/pull/9371#discussion_r4537641243


##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedTextMeta.java:
##########
@@ -303,7 +303,12 @@ public void check(
     }
     // Batch size accepts variables, which a design-time check cannot resolve. 
Only a value that is
     // genuinely fixed can be judged here.
-    if (isResolvedNumberBad(variables, batchSize, size -> size < 1)) {
+    String resolvedBatchSize = variables.resolve(batchSize);
+    if (!Utils.isEmpty(resolvedBatchSize)
+        && !StringUtil.containsVariableToken(resolvedBatchSize)
+        && Const.toInt(resolvedBatchSize, Integer.MIN_VALUE) == 
Integer.MIN_VALUE) {
+      error(remarks, transformMeta, 
"EmbedText.Validation.BatchSizeNotANumber", resolvedBatchSize);
+    } else if (isResolvedNumberBad(variables, batchSize, size -> size < 1)) {

Review Comment:
   With an empty batch size `init()` now uses the default of 16, but this 
branch still reports it: `isResolvedNumberBad` parses `""` as -1, so Verify 
shows 'Batch size must be at least 1' for a configuration that runs fine. 
Treating empty as valid here too would line them up.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -161,12 +180,115 @@ private void flushBatch() throws HopException {
   }
 
   /**
-   * True for text that has no content to embed. langchain4j refuses a text 
segment that trims to
-   * nothing, which takes control characters as well as spaces, and would stop 
the transform before
-   * the provider is called. Unicode whitespace has nothing to embed either.
+   * Calls the provider, retrying a failure that may pass (no connection, a 
timeout, a rate limit, a
+   * server error). A refusal that will not change, such as a bad API key or 
an unknown model, fails
+   * at once.
+   */
+  private List<Embedding> embedWithRetries(List<TextSegment> segments) {
+    long delay = data.retryDelayMillis;
+    for (int attempt = 1; ; attempt++) {
+      try {
+        return data.model.embedAll(segments).content();
+      } catch (RuntimeException e) {
+        if (attempt > MAX_RETRIES || e instanceof NonRetriableException || 
isStopped()) {
+          throw e;
+        }
+        logBasic(
+            BaseMessages.getString(
+                PKG,
+                "EmbedText.Log.Retrying",
+                describeFailure(e),
+                String.valueOf(attempt),
+                String.valueOf(MAX_RETRIES)));
+        if (isDebug()) {
+          logDebug(Const.getStackTracker(e));
+        }
+        if (!pause(delay)) {
+          throw e;
+        }
+        delay = delay * 3 / 2;
+      }
+    }
+  }
+
+  /** Waits, but gives up as soon as the pipeline is stopped. False when the 
wait was cut short. */
+  private boolean pause(long millis) {
+    long until = System.currentTimeMillis() + millis;
+    try {
+      while (!isStopped()) {
+        long left = until - System.currentTimeMillis();
+        if (left <= 0) {
+          return true;
+        }
+        Thread.sleep(Math.min(left, 100L));
+      }
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
+    return false;
+  }
+
+  /**
+   * One line saying which model, provider and endpoint failed, and why. The 
exception of a failed
+   * connection carries no message of its own, so on its own it says nothing 
about where it went.
+   */
+  String describeFailure(Throwable e) {
+    String endpoint = endpointForMessages(data.baseUrl);
+    return BaseMessages.getString(
+        PKG,
+        "EmbedText.Error.Embedding",
+        data.modelName,
+        data.providerName,
+        Utils.isEmpty(endpoint) ? "?" : endpoint,
+        rootCause(e));
+  }
+
+  /** The deepest cause, as its class name and its message when it has one. */
+  static String rootCause(Throwable e) {
+    Throwable root = e;
+    while (root.getCause() != null && root.getCause() != root) {
+      root = root.getCause();
+    }
+    String message = root.getMessage();
+    String name = root.getClass().getSimpleName();
+    return Utils.isEmpty(message) ? name : name + ": " + message;

Review Comment:
   The deepest exception's full message goes into the error rows here, although 
the URL is cleaned for the stated reason that error rows can end up in a table. 
Provider/HTTP client messages can contain secrets too: e.g. OpenAI's 401 body 
`Incorrect API key provided: sk-...`, or a client exception that includes the 
request URL with `?api_key=`. A long HTML 5xx body also becomes a multi-KB 
description that can overflow an error-description column. Maybe pass the 
message through the same masking and cap its length?



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedTextMeta.java:
##########
@@ -303,7 +303,12 @@ public void check(
     }
     // Batch size accepts variables, which a design-time check cannot resolve. 
Only a value that is
     // genuinely fixed can be judged here.
-    if (isResolvedNumberBad(variables, batchSize, size -> size < 1)) {
+    String resolvedBatchSize = variables.resolve(batchSize);
+    if (!Utils.isEmpty(resolvedBatchSize)
+        && !StringUtil.containsVariableToken(resolvedBatchSize)
+        && Const.toInt(resolvedBatchSize, Integer.MIN_VALUE) == 
Integer.MIN_VALUE) {

Review Comment:
   `Const.toInt` doesn't trim, while `init()` parses `batchSize.trim()`. So `' 
8 '` (or a variable resolving with a trailing space) runs with 8, as the new 
test shows, but Verify reports it isn't a whole number. Sharing one trimmed 
parse helper between `check()` and `init()` would keep them in sync (it would 
also avoid the `Integer.MIN_VALUE` sentinel, which makes the literal 
`-2147483648` 'not a number').



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -161,12 +180,115 @@ private void flushBatch() throws HopException {
   }
 
   /**
-   * True for text that has no content to embed. langchain4j refuses a text 
segment that trims to
-   * nothing, which takes control characters as well as spaces, and would stop 
the transform before
-   * the provider is called. Unicode whitespace has nothing to embed either.
+   * Calls the provider, retrying a failure that may pass (no connection, a 
timeout, a rate limit, a
+   * server error). A refusal that will not change, such as a bad API key or 
an unknown model, fails
+   * at once.
+   */
+  private List<Embedding> embedWithRetries(List<TextSegment> segments) {
+    long delay = data.retryDelayMillis;
+    for (int attempt = 1; ; attempt++) {
+      try {
+        return data.model.embedAll(segments).content();
+      } catch (RuntimeException e) {
+        if (attempt > MAX_RETRIES || e instanceof NonRetriableException || 
isStopped()) {
+          throw e;
+        }
+        logBasic(
+            BaseMessages.getString(
+                PKG,
+                "EmbedText.Log.Retrying",
+                describeFailure(e),
+                String.valueOf(attempt),
+                String.valueOf(MAX_RETRIES)));
+        if (isDebug()) {
+          logDebug(Const.getStackTracker(e));
+        }
+        if (!pause(delay)) {
+          throw e;

Review Comment:
   When the pipeline is stopped during a retry pause (or at the `isStopped()` 
check at line 193), the provider exception is rethrown and `flushBatch` handles 
it as a provider failure. With an error hop, pressing Stop during an outage 
sends every row of the batch to the error rows as 'Error calling embedding 
model ... ConnectException', although the user stopped the run. A stop should 
probably just abandon the batch.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -161,12 +180,115 @@ private void flushBatch() throws HopException {
   }
 
   /**
-   * True for text that has no content to embed. langchain4j refuses a text 
segment that trims to
-   * nothing, which takes control characters as well as spaces, and would stop 
the transform before
-   * the provider is called. Unicode whitespace has nothing to embed either.
+   * Calls the provider, retrying a failure that may pass (no connection, a 
timeout, a rate limit, a
+   * server error). A refusal that will not change, such as a bad API key or 
an unknown model, fails
+   * at once.
+   */
+  private List<Embedding> embedWithRetries(List<TextSegment> segments) {
+    long delay = data.retryDelayMillis;
+    for (int attempt = 1; ; attempt++) {
+      try {
+        return data.model.embedAll(segments).content();
+      } catch (RuntimeException e) {
+        if (attempt > MAX_RETRIES || e instanceof NonRetriableException || 
isStopped()) {
+          throw e;
+        }
+        logBasic(
+            BaseMessages.getString(
+                PKG,
+                "EmbedText.Log.Retrying",
+                describeFailure(e),
+                String.valueOf(attempt),
+                String.valueOf(MAX_RETRIES)));
+        if (isDebug()) {
+          logDebug(Const.getStackTracker(e));
+        }
+        if (!pause(delay)) {
+          throw e;
+        }
+        delay = delay * 3 / 2;
+      }
+    }
+  }
+
+  /** Waits, but gives up as soon as the pipeline is stopped. False when the 
wait was cut short. */
+  private boolean pause(long millis) {
+    long until = System.currentTimeMillis() + millis;
+    try {
+      while (!isStopped()) {
+        long left = until - System.currentTimeMillis();
+        if (left <= 0) {
+          return true;
+        }
+        Thread.sleep(Math.min(left, 100L));
+      }
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
+    return false;
+  }
+
+  /**
+   * One line saying which model, provider and endpoint failed, and why. The 
exception of a failed
+   * connection carries no message of its own, so on its own it says nothing 
about where it went.
+   */
+  String describeFailure(Throwable e) {
+    String endpoint = endpointForMessages(data.baseUrl);
+    return BaseMessages.getString(
+        PKG,
+        "EmbedText.Error.Embedding",
+        data.modelName,
+        data.providerName,
+        Utils.isEmpty(endpoint) ? "?" : endpoint,

Review Comment:
   With the OpenAI provider and no base URL configured (the usual setup) this 
shows `at ?`, e.g. `Error calling embedding model text-embedding-3-small of AI 
provider 'openai' at ?: ConnectException`. Since the point is to show which 
endpoint didn't answer, the factory could report the effective default URL 
(`https://api.openai.com/v1`) in the resolved model.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -52,7 +64,19 @@ public boolean init() {
       logError(BaseMessages.getString(PKG, 
"EmbedText.Validation.ProviderRequired"));
       return false;
     }
-    data.batchSize = Const.toInt(resolve(meta.getBatchSize()), 16);
+    String batchSize = resolve(meta.getBatchSize());
+    if (Utils.isEmpty(batchSize)) {
+      data.batchSize = DEFAULT_BATCH_SIZE;
+    } else {
+      try {
+        data.batchSize = Integer.parseInt(batchSize.trim());

Review Comment:
   Small one: a whole number that's out of range (e.g. `99999999999`) also 
lands here and gets 'is not a whole number. Check that every variable in it is 
set', although it's a whole number and has no variables. A separate 'too large' 
message would be clearer.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -161,12 +180,115 @@ private void flushBatch() throws HopException {
   }
 
   /**
-   * True for text that has no content to embed. langchain4j refuses a text 
segment that trims to
-   * nothing, which takes control characters as well as spaces, and would stop 
the transform before
-   * the provider is called. Unicode whitespace has nothing to embed either.
+   * Calls the provider, retrying a failure that may pass (no connection, a 
timeout, a rate limit, a
+   * server error). A refusal that will not change, such as a bad API key or 
an unknown model, fails
+   * at once.
+   */
+  private List<Embedding> embedWithRetries(List<TextSegment> segments) {
+    long delay = data.retryDelayMillis;
+    for (int attempt = 1; ; attempt++) {
+      try {
+        return data.model.embedAll(segments).content();
+      } catch (RuntimeException e) {
+        if (attempt > MAX_RETRIES || e instanceof NonRetriableException || 
isStopped()) {
+          throw e;
+        }
+        logBasic(
+            BaseMessages.getString(
+                PKG,
+                "EmbedText.Log.Retrying",
+                describeFailure(e),
+                String.valueOf(attempt),
+                String.valueOf(MAX_RETRIES)));
+        if (isDebug()) {
+          logDebug(Const.getStackTracker(e));
+        }
+        if (!pause(delay)) {
+          throw e;
+        }
+        delay = delay * 3 / 2;
+      }
+    }
+  }
+
+  /** Waits, but gives up as soon as the pipeline is stopped. False when the 
wait was cut short. */
+  private boolean pause(long millis) {
+    long until = System.currentTimeMillis() + millis;
+    try {
+      while (!isStopped()) {
+        long left = until - System.currentTimeMillis();
+        if (left <= 0) {
+          return true;
+        }
+        Thread.sleep(Math.min(left, 100L));
+      }
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
+    return false;
+  }
+
+  /**
+   * One line saying which model, provider and endpoint failed, and why. The 
exception of a failed
+   * connection carries no message of its own, so on its own it says nothing 
about where it went.
+   */
+  String describeFailure(Throwable e) {
+    String endpoint = endpointForMessages(data.baseUrl);
+    return BaseMessages.getString(
+        PKG,
+        "EmbedText.Error.Embedding",
+        data.modelName,
+        data.providerName,
+        Utils.isEmpty(endpoint) ? "?" : endpoint,
+        rootCause(e));
+  }
+
+  /** The deepest cause, as its class name and its message when it has one. */
+  static String rootCause(Throwable e) {
+    Throwable root = e;
+    while (root.getCause() != null && root.getCause() != root) {
+      root = root.getCause();
+    }
+    String message = root.getMessage();
+    String name = root.getClass().getSimpleName();
+    return Utils.isEmpty(message) ? name : name + ": " + message;
+  }
+
+  /**
+   * The base URL without user info, query or fragment, which is where a key 
would sit if someone
+   * put one in the URL. Error rows can end up in a table, so they must not 
carry it.
+   */
+  static String endpointForMessages(String baseUrl) {
+    if (Utils.isEmpty(baseUrl)) {
+      return "";
+    }
+    try {
+      URI uri = new URI(baseUrl.trim());
+      if (uri.getHost() == null) {
+        return uri.getRawUserInfo() == null && uri.getRawQuery() == null ? 
baseUrl.trim() : "";

Review Comment:
   When `java.net.URI` can't parse a server host, `getRawUserInfo()` and 
`getRawQuery()` are null as well, so this returns the full URL with the 
credentials in it. Two examples: `http://user:secret@ollama_host:11434` (the 
underscore makes the authority registry-based) and 
`myhost:11434/v1?api_key=abc` (no scheme, so it parses as an opaque URI). The 
password or key then goes into the exception, the retry log line and every 
error row, which is what this method is meant to prevent. Returning `""` (or 
only what's safe, like the scheme) whenever `getHost()` is null would be safer.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/transforms/embedtext/EmbedText.java:
##########
@@ -161,12 +180,115 @@ private void flushBatch() throws HopException {
   }
 
   /**
-   * True for text that has no content to embed. langchain4j refuses a text 
segment that trims to
-   * nothing, which takes control characters as well as spaces, and would stop 
the transform before
-   * the provider is called. Unicode whitespace has nothing to embed either.
+   * Calls the provider, retrying a failure that may pass (no connection, a 
timeout, a rate limit, a
+   * server error). A refusal that will not change, such as a bad API key or 
an unknown model, fails
+   * at once.
+   */
+  private List<Embedding> embedWithRetries(List<TextSegment> segments) {
+    long delay = data.retryDelayMillis;
+    for (int attempt = 1; ; attempt++) {
+      try {
+        return data.model.embedAll(segments).content();
+      } catch (RuntimeException e) {
+        if (attempt > MAX_RETRIES || e instanceof NonRetriableException || 
isStopped()) {

Review Comment:
   Every RuntimeException that isn't a `NonRetriableException` is retried, 
including failures that will happen again every time, e.g. a response the 
parser can't map (IllegalStateException, a Jackson exception). That's 3 
identical calls, ~1.25 s of back-off and two 'Retry n of 2' log lines per batch 
for an error that can't pass. Retrying only langchain4j's `RetriableException` 
and IO/timeout causes would avoid that.



##########
plugins/tech/ai/src/main/java/org/apache/hop/ai/engine/AiEmbeddingFactory.java:
##########
@@ -103,7 +124,7 @@ public static EmbeddingModel createEmbeddingModel(
 
   private static EmbeddingModel ollamaModel(AiProviderSettings settings, 
String model) {
     OllamaEmbeddingModel.OllamaEmbeddingModelBuilder builder =
-        
OllamaEmbeddingModel.builder().baseUrl(settings.baseUrl()).modelName(model);
+        
OllamaEmbeddingModel.builder().baseUrl(settings.baseUrl()).modelName(model).maxRetries(0);

Review Comment:
   This public factory method now builds every model with `maxRetries(0)`, but 
only `EmbedText` adds its own retry loop. Other callers (vector store / RAG 
transforms, a test-connection button, third-party plugins) silently lose 
langchain4j's default retries, so a single transient 429 or 5xx now fails. 
Could the retry count be a parameter (with the old default for other callers), 
or the retry live in the factory as a wrapping model?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to