|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Ericsson |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.core.llm; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.URI; |
| 15 | +import java.net.http.HttpClient; |
| 16 | +import java.net.http.HttpRequest; |
| 17 | +import java.net.http.HttpResponse; |
| 18 | +import java.time.Duration; |
| 19 | + |
| 20 | +import org.eclipse.swt.widgets.Display; |
| 21 | + |
| 22 | +/** |
| 23 | + * Minimal LLM client. An instance is configured with the endpoint URL and the |
| 24 | + * model name and exposes {@link #infer(String)} to query the model. |
| 25 | + * <p> |
| 26 | + * {@link #infer(String)} performs a blocking HTTP request and must never be |
| 27 | + * invoked from the SWT UI thread; doing so throws {@link IllegalStateException}. |
| 28 | + * </p> |
| 29 | + */ |
| 30 | +public final class LlmModel { |
| 31 | + |
| 32 | + private final String url; |
| 33 | + private final String model; |
| 34 | + |
| 35 | + public LlmModel(String url, String model) { |
| 36 | + this.url = url; |
| 37 | + this.model = model; |
| 38 | + } |
| 39 | + |
| 40 | + public String url() { |
| 41 | + return url; |
| 42 | + } |
| 43 | + |
| 44 | + public String model() { |
| 45 | + return model; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Send {@code prompt} to the configured LLM and return the raw response body. |
| 50 | + * |
| 51 | + * @throws IllegalStateException if invoked from the SWT UI thread |
| 52 | + * @throws IOException if the HTTP call fails |
| 53 | + */ |
| 54 | + public String infer(String prompt) throws IOException, InterruptedException { |
| 55 | + if (Display.getCurrent() != null) { |
| 56 | + throw new IllegalStateException("LlmModel.infer must not be called from the UI thread"); //$NON-NLS-1$ |
| 57 | + } |
| 58 | + String body = "{\"model\":\"" + escape(model) + "\",\"prompt\":\"" + escape(prompt) + "\",\"stream\":false}"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 59 | + HttpRequest request = HttpRequest.newBuilder(URI.create(url)) |
| 60 | + .timeout(Duration.ofMinutes(2)) |
| 61 | + .header("Content-Type", "application/json") //$NON-NLS-1$ //$NON-NLS-2$ |
| 62 | + .POST(HttpRequest.BodyPublishers.ofString(body)) |
| 63 | + .build(); |
| 64 | + HttpResponse<String> response = HttpClient.newHttpClient() |
| 65 | + .send(request, HttpResponse.BodyHandlers.ofString()); |
| 66 | + if (response.statusCode() / 100 != 2) { |
| 67 | + throw new IOException("LLM request failed: HTTP " + response.statusCode() + " - " + response.body()); //$NON-NLS-1$ //$NON-NLS-2$ |
| 68 | + } |
| 69 | + return response.body(); |
| 70 | + } |
| 71 | + |
| 72 | + private static String escape(String s) { |
| 73 | + StringBuilder sb = new StringBuilder(s.length() + 8); |
| 74 | + for (int i = 0; i < s.length(); i++) { |
| 75 | + char c = s.charAt(i); |
| 76 | + switch (c) { |
| 77 | + case '"' -> sb.append("\\\""); //$NON-NLS-1$ |
| 78 | + case '\\' -> sb.append("\\\\"); //$NON-NLS-1$ |
| 79 | + case '\n' -> sb.append("\\n"); //$NON-NLS-1$ |
| 80 | + case '\r' -> sb.append("\\r"); //$NON-NLS-1$ |
| 81 | + case '\t' -> sb.append("\\t"); //$NON-NLS-1$ |
| 82 | + default -> { |
| 83 | + if (c < 0x20) { |
| 84 | + sb.append(String.format("\\u%04x", (int) c)); //$NON-NLS-1$ |
| 85 | + } else { |
| 86 | + sb.append(c); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + return sb.toString(); |
| 92 | + } |
| 93 | +} |
0 commit comments