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


##########
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:
   Fixed in the latest commit. OPENAI_BASE_URL is now checked first; 
LLM_BASE_URL is only consulted when OPENAI_API_KEY is not set (meaning the key 
came from LLM_API_KEY), so a real OpenAI API key can never be redirected to an 
unintended server.



##########
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:
   Fixed. Added an `isLargeModel()` helper that checks whether the size tag is 
>= 14B. `allModelsSmall()` now uses `noneMatch(isLargeModel)` instead of 
`allMatch(isSmallModel)`, so models with a non-numeric tag like 
`llama3.2:latest` are treated as "not known to be large" — which means users 
pulling only such models will correctly see the small-model warning. Updated 
the Javadoc to document this behaviour.



##########
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:
   Added 8 unit tests in `OllamaDoctorSupportTest` covering all the cases you 
listed: small numeric tags (`qwen2.5:7b`, `gemma3:1b-it`), large tags 
(`qwen2.5:32b`, `llama3.1:70b`), non-numeric tags (`llama3.2:latest`, 
`phi4-mini:latest`), model without colon, `allModelsSmall` with mixed sizes, 
unknown-size tag, and empty/null list.



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