diff --git a/src/main/java/org/myrobotlab/service/LLM.java b/src/main/java/org/myrobotlab/service/LLM.java index 6487711668..d6cc21b57a 100644 --- a/src/main/java/org/myrobotlab/service/LLM.java +++ b/src/main/java/org/myrobotlab/service/LLM.java @@ -93,77 +93,64 @@ public void clearInputs() { public String createChatCompletionPayload(String model, String systemContent, String userContent, int n, float temperature, int maxTokens) { try { - // Create the map to hold the request parameters - LinkedHashMap requestPayload = new LinkedHashMap<>(); - - // Add model to the map - requestPayload.put("model", model); - - // Create the messages array - LinkedHashMap systemMessage = new LinkedHashMap<>(); - systemMessage.put("role", "system"); - - // Get the current date - LocalDate currentDate = LocalDate.now(); - // Get the current time - LocalTime currentTime = LocalTime.now(); - // Get the current date and time - LocalDateTime currentDateTime = LocalDateTime.now(); - - // Format the date as a string - DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); - String dateString = currentDate.format(dateFormatter); - - // Format the time as a string in 12-hour format with AM/PM - DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a"); - String timeString = currentTime.format(timeFormatter); - - // Format the full date with day of the week - DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofPattern("EEEE MMMM d'th' yyyy h:mm a"); - String fullDateString = currentDateTime.format(fullDateFormatter); - - inputs.put("Date", dateString); - inputs.put("Time", timeString); - inputs.put("DateTime", fullDateString); - - for (String inputKey : inputs.keySet()) { - Object objectValue = inputs.get(inputKey); - if (objectValue != null) { - systemContent = systemContent.replace(String.format("{{%s}}", inputKey), objectValue.toString()); + // Create the map to hold the request parameters + LinkedHashMap requestPayload = new LinkedHashMap<>(); + requestPayload.put("model", model); + + // Create and format date and time strings + LocalDateTime currentDateTime = LocalDateTime.now(); + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a"); + DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofPattern("EEEE MMMM d'th' yyyy h:mm a"); + + inputs.put("Date", currentDateTime.format(dateFormatter)); + inputs.put("Time", currentDateTime.format(timeFormatter)); + inputs.put("DateTime", currentDateTime.format(fullDateFormatter)); + + // Replace placeholders in system content + for (Map.Entry entry : inputs.entrySet()) { + if (entry.getValue() != null) { + systemContent = systemContent.replace(String.format("{{%s}}", entry.getKey()), entry.getValue().toString()); + } } - } - systemMessage.put("content", systemContent); - if (config.maxHistory == 0) { - // history is disabled - userMessages.clear(); - } else if (userMessages.size() > config.maxHistory) { - userMessages.remove(0); - } - LinkedHashMap userMessage = new LinkedHashMap<>(); - userMessage.put("role", "user"); - userMessage.put("content", userContent); - userMessages.add(userMessage); - - List> allMessages = new ArrayList<>(); - allMessages.add(systemMessage); - allMessages.addAll(userMessages); + // Create system message + LinkedHashMap systemMessage = new LinkedHashMap<>(); + systemMessage.put("role", "system"); + systemMessage.put("content", systemContent); + + // Handle message history + LinkedHashMap userMessage = new LinkedHashMap<>(); + userMessage.put("role", "user"); + userMessage.put("content", userContent); + userMessages.add(userMessage); + + if (config.maxHistory > 0) { + while (userMessages.size() > config.maxHistory) { + userMessages.remove(0); + } + } else { + userMessages.clear(); + } - // make history - requestPayload.put("messages", allMessages); + // Combine messages + List> allMessages = new ArrayList<>(); + allMessages.add(systemMessage); + allMessages.addAll(userMessages); + requestPayload.put("messages", allMessages); - // Add n, temperature, and max_tokens to the map - requestPayload.put("n", n); - requestPayload.put("temperature", temperature); - requestPayload.put("max_tokens", maxTokens); + // Add other parameters + requestPayload.put("n", n); + requestPayload.put("temperature", temperature); + requestPayload.put("max_tokens", maxTokens); - return CodecUtils.toJson(requestPayload); + return CodecUtils.toJson(requestPayload); } catch (Exception e) { - error(e); - return null; + error(e); + return null; } - } +} public LinkedHashMap createFunctionDefinition(String name, String description, LinkedHashMap parameters) { LinkedHashMap functionDefinition = new LinkedHashMap<>(); @@ -211,6 +198,9 @@ public Response getResponse(String text) { log.info("curl {} -d '{}'", config.url, json); String msg = http.postJson(config.password, config.url, json); + log.error("url: {}", config.url); + log.error("json: {}", json); + System.out.print(json); Map payload = CodecUtils.fromJson(msg, new StaticType<>() { }); diff --git a/src/main/java/org/myrobotlab/service/config/LLMConfig.java b/src/main/java/org/myrobotlab/service/config/LLMConfig.java index 1e605f286c..2271d1f292 100644 --- a/src/main/java/org/myrobotlab/service/config/LLMConfig.java +++ b/src/main/java/org/myrobotlab/service/config/LLMConfig.java @@ -14,10 +14,6 @@ public class LLMConfig extends ServiceConfig { public int maxTokens = 256; public float temperature = 0.7f; - /** - * number of history items to keep when re-submitting - */ - public int history = 5; // public String url = "https://api.openai.com/v1/chat/completions"; // http://localhost:11434/v1/chat/completions public String url = null; diff --git a/src/main/resources/resource/WebGui/app/service/views/LLMGui.html b/src/main/resources/resource/WebGui/app/service/views/LLMGui.html index 95dbd85e68..8ebd298055 100644 --- a/src/main/resources/resource/WebGui/app/service/views/LLMGui.html +++ b/src/main/resources/resource/WebGui/app/service/views/LLMGui.html @@ -62,8 +62,8 @@ Temperature is how creative the responses should be. - History - + Max History + The number of responses saved for context.