Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
fixed api-key handling
Michael Peter Christen79c4c4e3dd0e
diff --git a/htroot/LLMSelection_p.html b/htroot/LLMSelection_p.htmlindex 493e91fc8..c94e4e85b 100644--- a/htroot/LLMSelection_p.html+++ b/htroot/LLMSelection_p.html@@ -1222,7 +1222,7 @@</dd><dt class="TableCellDark">api_key</dt>- <dd><input type="text" name="apikey" id="apikey" value="" disabled=true size="30" maxlength="60" class="form-control"/> (not required for Ollama or LMStudio)+ <dd><input type="text" name="apikey" id="apikey" value="" disabled=true size="30" maxlength="120" class="form-control"/> (not required for Ollama or LMStudio)</dd><dt class="TableCellDark">max_tokens</dt>diff --git a/source/net/yacy/ai/LLM.java b/source/net/yacy/ai/LLM.javaindex 6f882ea94..df0e82242 100644--- a/source/net/yacy/ai/LLM.java+++ b/source/net/yacy/ai/LLM.java@@ -128,11 +128,14 @@ public class LLM {// API Helper Methods- private static String sendPostRequest(final String urls, final JSONObject data) throws IOException, URISyntaxException {+ private static String sendPostRequest(final String urls, final JSONObject data, final String apiKey) throws IOException, URISyntaxException {final URL url = new URI(urls).toURL();final HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/json");+ if (apiKey != null && !apiKey.isEmpty()) {+ conn.setRequestProperty("Authorization", "Bearer " + apiKey);+ }conn.setDoOutput(true);try (OutputStream os = conn.getOutputStream()) {@@ -208,7 +211,7 @@ public class LLM {final JSONObject data = new JSONObject();try {data.put("name", name);- sendPostRequest(this.hoststub + "/api/show", data);+ sendPostRequest(this.hoststub + "/api/show", data, this.api_key);return true;} catch (JSONException | URISyntaxException | IOException e) {return false;@@ -220,7 +223,7 @@ public class LLM {try {data.put("name", name);data.put("stream", false);- final String response = sendPostRequest(this.hoststub + "/api/pull", data);+ final String response = sendPostRequest(this.hoststub + "/api/pull", data, this.api_key);// this sends {"status": "success"} in case of successfinal JSONObject responseObject = new JSONObject(response);final String status = responseObject.optString("status", "");@@ -281,7 +284,7 @@ public class LLM {data.put("response_format", response_format);}- final String response = sendPostRequest(this.hoststub + "/v1/chat/completions", data);+ final String response = sendPostRequest(this.hoststub + "/v1/chat/completions", data, this.api_key);final JSONObject responseObject = new JSONObject(response);final JSONArray choices = responseObject.getJSONArray("choices");final JSONObject choice = choices.getJSONObject(0);diff --git a/source/net/yacy/ai/ToolCallProtocol.java b/source/net/yacy/ai/ToolCallProtocol.javaindex 8f526beaa..68c575723 100644--- a/source/net/yacy/ai/ToolCallProtocol.java+++ b/source/net/yacy/ai/ToolCallProtocol.java@@ -34,6 +34,7 @@ import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;+import java.util.logging.Logger;import javax.servlet.ServletOutputStream;@@ -121,6 +122,8 @@ public final class ToolCallProtocol {final int status = conn.getResponseCode();if (status == 200) {handleInitialStreamAndContinue(out, conn, llm4Chat, preparedBody, messages, initialMetadata);+ } else {+ Logger.getLogger("ToolCallProtocoll").severe(status + " " + conn.getResponseMessage());}return status;}@@ -359,7 +362,7 @@ public final class ToolCallProtocol {final HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setRequestProperty("Content-Type", "application/json");- if (!llm4Chat.llm.api_key.isEmpty()) {+ if (llm4Chat.llm.api_key != null && !llm4Chat.llm.api_key.isEmpty()) {conn.setRequestProperty("Authorization", "Bearer " + llm4Chat.llm.api_key);}conn.setDoOutput(true);