diff --git a/src/main/java/org/myrobotlab/service/LLM.java b/src/main/java/org/myrobotlab/service/LLM.java index 1b889d3f76..9e0b48f358 100644 --- a/src/main/java/org/myrobotlab/service/LLM.java +++ b/src/main/java/org/myrobotlab/service/LLM.java @@ -81,6 +81,8 @@ public class LLM extends Service implements TextListener, TextPublish OllamaAPI ollamaAPI; List> userMessages = new ArrayList<>(); + + List userTextMessages = new ArrayList<>(); public void addInput(String key, Object value) { inputs.put(key, value); @@ -221,12 +223,46 @@ public Response getResponseStream(String text) { URL url = new URL(config.url); ollamaAPI = new OllamaAPI(String.format("%s://%s:%d", url.getProtocol(), url.getHost(), url.getPort())); } + + + + // 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)); + + String systemContent = config.system; + + // 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()); + } + } + + userTextMessages.add(text); + + if (config.maxHistory > 0) { + while (userTextMessages.size() > config.maxHistory) { + userTextMessages.remove(0); + } + } else { + userTextMessages.clear(); + } + + String finalText = systemContent + " " + text; + + // sentence chunking stream processing final StringBuilder[] sentenceBuilder = { new StringBuilder() }; final int[] lastProcessedLength = { 0 }; // Track the length of already // processed text - OllamaResult result = ollamaAPI.generate("llama3", text, false, new OptionsBuilder().build(), (s) -> { + OllamaResult result = ollamaAPI.generate("llama3", finalText, false, new OptionsBuilder().build(), (s) -> { // Append only the new portion of the text String newText = s.substring(lastProcessedLength[0]); sentenceBuilder[0].append(newText);