gnodet commented on code in PR #26162:
URL: https://github.com/apache/camel/pull/26162#discussion_r3948037172


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/LlmClient.java:
##########
@@ -1795,7 +1802,13 @@ private boolean tryOpenAi() {
             apiKey = key;
             openAiAuthMode = OpenAiAuthMode.bearer;
             if (url == null || url.isBlank()) {
-                url = "https://api.openai.com";;
+                // LLM_BASE_URL / OPENAI_BASE_URL let users point at any 
OpenAI-compatible
+                // server (LM Studio, vLLM, LocalAI, Jan, …) without a CLI flag
+                String baseUrl = System.getenv("LLM_BASE_URL");
+                if (baseUrl == null || baseUrl.isBlank()) {
+                    baseUrl = System.getenv("OPENAI_BASE_URL");
+                }
+                url = (baseUrl != null && !baseUrl.isBlank()) ? 
stripTrailingSlash(baseUrl) : "https://api.openai.com";;
             }
             return true;

Review Comment:
   ⚠️ **Security concern:** When `OPENAI_API_KEY` is the matched key, 
`LLM_BASE_URL` should NOT be consulted — it would redirect the real OpenAI API 
key to an unintended server. The docs correctly separate these as two distinct 
detection steps (5. `OPENAI_API_KEY` → `api.openai.com`, 6. `LLM_API_KEY` + 
`LLM_BASE_URL` → custom), but the code merges them.
   
   `OPENAI_BASE_URL` is fine with `OPENAI_API_KEY` (it's a standard OpenAI SDK 
convention for proxies), but `LLM_BASE_URL` should only apply to `LLM_API_KEY`.
   
   ```suggestion
                   // LLM_BASE_URL / OPENAI_BASE_URL let users point at any 
OpenAI-compatible
                   // server (LM Studio, vLLM, LocalAI, Jan, …) without a CLI 
flag
                   String baseUrl = System.getenv("OPENAI_BASE_URL");
                   if (baseUrl == null || baseUrl.isBlank()) {
                       // Only check LLM_BASE_URL when the key came from 
LLM_API_KEY
                       // to avoid redirecting a real OPENAI_API_KEY to an 
unintended server
                       if (System.getenv("OPENAI_API_KEY") == null || 
System.getenv("OPENAI_API_KEY").isBlank()) {
                           baseUrl = System.getenv("LLM_BASE_URL");
                       }
                   }
   ```



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/OllamaDoctorSupport.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.common;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.camel.dsl.jbang.core.commands.LlmClient;
+
+/**
+ * Detects a local Ollama instance for {@code camel doctor} and the TUI doctor 
popup.
+ */
+public final class OllamaDoctorSupport {
+
+    private static final String DEFAULT_OLLAMA_DISPLAY = "localhost:11434";
+
+    private OllamaDoctorSupport() {
+    }
+
+    public record Status(boolean running, String baseUrl, List<String> models) 
{
+
+        public static Status notRunning() {
+            return new Status(false, null, List.of());
+        }
+    }
+
+    /**
+     * Probes {@code camel infra run ollama} PID files and the default {@code 
http://localhost:11434} endpoint.
+     */
+    public static Status detect() {
+        return detect(LlmClient.create());
+    }
+
+    static Status detect(LlmClient client) {
+        client.withApiType(LlmClient.ApiType.ollama);
+        if (!client.detectEndpoint()) {
+            return Status.notRunning();
+        }
+        return new Status(true, client.endpointUrl(), client.listModels());
+    }
+
+    public static String formatDisplayHost(String baseUrl) {
+        if (baseUrl == null || baseUrl.isBlank()) {
+            return DEFAULT_OLLAMA_DISPLAY;
+        }
+        String host = baseUrl;
+        if (host.startsWith("http://";)) {
+            host = host.substring("http://".length());
+        } else if (host.startsWith("https://";)) {
+            host = host.substring("https://".length());
+        }
+        while (host.endsWith("/")) {
+            host = host.substring(0, host.length() - 1);
+        }
+        return host.isBlank() ? DEFAULT_OLLAMA_DISPLAY : host;
+    }
+
+    public static String formatModels(List<String> models) {
+        if (models == null || models.isEmpty()) {
+            return "no models pulled";
+        }
+        return models.stream().collect(Collectors.joining(", "));
+    }
+
+    public static String cliRunningLine(Status status) {
+        return "Running at " + formatDisplayHost(status.baseUrl()) + " — 
models: " + formatModels(status.models());
+    }
+
+    public static String cliNotDetectedLine() {
+        return "Not detected (optional — start for local AI with F8 in TUI)";
+    }
+
+    public static String tuiRunningSummary(Status status, int maxLength) {
+        String summary = formatDisplayHost(status.baseUrl()) + " (" + 
modelCountLabel(status.models()) + ")";
+        if (maxLength > 0 && summary.length() > maxLength) {
+            return summary.substring(0, Math.max(0, maxLength - 3)) + "...";
+        }
+        return summary;
+    }
+
+    public static String modelCountLabel(List<String> models) {
+        if (models == null || models.isEmpty()) {
+            return "no models";
+        }
+        int count = models.size();
+        return count + (count == 1 ? " model" : " models");
+    }
+
+    /**
+     * Returns true when the model tag suggests fewer than 14B parameters. 
Tool-calling in the TUI F8 panel requires at
+     * least 14B.
+     */
+    public static boolean isSmallModel(String name) {
+        int colon = name.lastIndexOf(':');
+        String tag = colon >= 0 ? name.substring(colon + 1).toLowerCase() : "";
+        if (tag.matches("\\d+b.*")) {
+            int b = tag.indexOf('b');
+            try {
+                int params = Integer.parseInt(tag.substring(0, b));
+                return params < 14;
+            } catch (NumberFormatException e) {
+                return false;
+            }
+        }
+        return false;
+    }
+

Review Comment:
   🔴 **False negatives on models with non-numeric tags.** Many Ollama models 
use `latest` as their default tag (e.g., `llama3.2:latest` is 3B, 
`phi4-mini:latest` is 3.8B). The regex `\\d+b.*` won't match these, so 
`isSmallModel` returns `false` — the user never sees the small-model warning 
despite having a model far too small for tool calling.
   
   This means `allModelsSmall(["llama3.2:latest"])` returns `false` (because 
`allMatch` sees a non-small model), and the user gets a green checkmark for a 
3B model.
   
   Consider extracting the parameter count from the model name itself (e.g., 
`llama3.2` → likely small) or treating models without a recognized size tag as 
"unknown" rather than "not small." At minimum, if no tag matches `\d+b`, the 
method should return `false` conservatively but `allModelsSmall` should treat 
unknown-sized models differently — or document that the check only works with 
explicit size tags.



##########
dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/common/OllamaDoctorSupportTest.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dsl.jbang.core.common;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import com.sun.net.httpserver.HttpServer;
+import org.apache.camel.dsl.jbang.core.commands.LlmClient;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+class OllamaDoctorSupportTest {
+
+    private HttpServer server;
+
+    @AfterEach
+    void stopServer() {
+        if (server != null) {
+            server.stop(0);
+        }
+    }
+
+    @Test
+    void detectReturnsNotRunningWhenEndpointUnreachable() {
+        LlmClient client = 
LlmClient.create().withApiType(LlmClient.ApiType.ollama).withUrl("http://127.0.0.1:1";);
+
+        OllamaDoctorSupport.Status status = OllamaDoctorSupport.detect(client);
+
+        assertThat(status.running()).isFalse();
+        assertThat(status.baseUrl()).isNull();
+        assertThat(status.models()).isEmpty();
+    }
+
+    @Test
+    void detectUsesLlmClientProbeLogic() throws IOException {
+        String baseUrl = 
startOllamaServer("{\"models\":[{\"name\":\"qwen2.5:32b\"}]}");
+        LlmClient client = 
LlmClient.create().withApiType(LlmClient.ApiType.ollama).withUrl(baseUrl);
+
+        assertThat(client.detectEndpoint()).isTrue();
+        assertThat(client.listModels()).containsExactly("qwen2.5:32b");
+    }
+
+    @Test
+    void detectListsModelsFromRunningOllama() throws IOException {
+        String baseUrl = 
startOllamaServer("{\"models\":[{\"name\":\"qwen2.5:32b\"},{\"name\":\"llama3.2:latest\"}]}");
+
+        OllamaDoctorSupport.Status status = detectAt(baseUrl);
+
+        assertThat(status.running()).isTrue();
+        assertThat(status.baseUrl()).isEqualTo(baseUrl);
+        assertThat(status.models()).containsExactly("qwen2.5:32b", 
"llama3.2:latest");
+    }
+
+    @Test
+    void detectReportsRunningWhenNoModelsPulled() throws IOException {
+        String baseUrl = startOllamaServer("{\"models\":[]}");
+
+        OllamaDoctorSupport.Status status = detectAt(baseUrl);
+
+        assertThat(status.running()).isTrue();
+        assertThat(status.models()).isEmpty();
+        
assertThat(OllamaDoctorSupport.formatModels(status.models())).isEqualTo("no 
models pulled");
+    }
+
+    @Test
+    void cliRunningLineMatchesDoctorFormat() throws IOException {
+        String baseUrl = 
startOllamaServer("{\"models\":[{\"name\":\"qwen2.5:32b\"},{\"name\":\"llama3.2:latest\"}]}");
+        OllamaDoctorSupport.Status status = detectAt(baseUrl);
+
+        assertThat(OllamaDoctorSupport.cliRunningLine(status))
+                .isEqualTo("Running at 127.0.0.1:" + displayPort(baseUrl)
+                           + " — models: qwen2.5:32b, llama3.2:latest");
+    }
+
+    @Test
+    void formatDisplayHostStripsSchemeAndTrailingSlash() {
+        
assertThat(OllamaDoctorSupport.formatDisplayHost("http://localhost:11434/";))
+                .isEqualTo("localhost:11434");
+        
assertThat(OllamaDoctorSupport.formatDisplayHost("https://127.0.0.1:11434";))
+                .isEqualTo("127.0.0.1:11434");
+    }
+
+    @Test
+    void tuiRunningSummaryIncludesModelCount() {
+        OllamaDoctorSupport.Status status
+                = new OllamaDoctorSupport.Status(true, 
"http://localhost:11434";, List.of("a", "b"));
+
+        assertThat(OllamaDoctorSupport.tuiRunningSummary(status, 30))
+                .isEqualTo("localhost:11434 (2 models)");
+        
assertThat(OllamaDoctorSupport.modelCountLabel(List.of("one"))).isEqualTo("1 
model");
+    }
+
+    private OllamaDoctorSupport.Status detectAt(String baseUrl) {
+        LlmClient client = 
LlmClient.create().withApiType(LlmClient.ApiType.ollama).withUrl(baseUrl);
+        assertThat(client.detectEndpoint()).isTrue();
+        return new OllamaDoctorSupport.Status(true, client.endpointUrl(), 
client.listModels());
+    }
+
+    private String startOllamaServer(String apiTagsBody) throws IOException {
+        server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
+        server.createContext("/", exchange -> {
+            String path = exchange.getRequestURI().getPath();
+            String body;
+            int status;
+            if ("/api/tags".equals(path)) {
+                body = apiTagsBody;
+                status = 200;
+            } else if ("/".equals(path)) {
+                body = "Ollama is running";
+                status = 200;
+            } else {
+                body = "";
+                status = 404;
+            }
+            byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
+            exchange.sendResponseHeaders(status, bytes.length);
+            try (OutputStream os = exchange.getResponseBody()) {
+                os.write(bytes);
+            }
+        });
+        server.start();
+        return "http://127.0.0.1:"; + server.getAddress().getPort();
+    }
+
+    private static int displayPort(String baseUrl) {
+        return Integer.parseInt(baseUrl.substring(baseUrl.lastIndexOf(':') + 
1));
+    }
+}

Review Comment:
   💡 **Missing test coverage:** `isSmallModel()` and `allModelsSmall()` have no 
dedicated unit tests. Given the regex-based parsing, these need tests for:
   - `qwen2.5:7b` → true (small)
   - `qwen2.5:32b` → false (large)
   - `llama3.2:latest` → false (no size tag — current behavior, though arguably 
wrong)
   - `gemma3:1b-it` → true (tag with suffix)
   - `model-without-colon` → false (no tag)
   - `allModelsSmall` with mixed sizes
   - `allModelsSmall` with empty list → false



-- 
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