Skip to content

Commit 6f058c8

Browse files
committed
fixed addInputs and history etc
1 parent 9213c6b commit 6f058c8

File tree

1 file changed

+37
-1
lines changed
  • src/main/java/org/myrobotlab/service

1 file changed

+37
-1
lines changed

src/main/java/org/myrobotlab/service/LLM.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public class LLM extends Service<LLMConfig> implements TextListener, TextPublish
8181
OllamaAPI ollamaAPI;
8282

8383
List<LinkedHashMap<String, Object>> userMessages = new ArrayList<>();
84+
85+
List<String> userTextMessages = new ArrayList<>();
8486

8587
public void addInput(String key, Object value) {
8688
inputs.put(key, value);
@@ -221,12 +223,46 @@ public Response getResponseStream(String text) {
221223
URL url = new URL(config.url);
222224
ollamaAPI = new OllamaAPI(String.format("%s://%s:%d", url.getProtocol(), url.getHost(), url.getPort()));
223225
}
226+
227+
228+
229+
// Create and format date and time strings
230+
LocalDateTime currentDateTime = LocalDateTime.now();
231+
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
232+
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a");
233+
DateTimeFormatter fullDateFormatter = DateTimeFormatter.ofPattern("EEEE MMMM d'th' yyyy h:mm a");
224234

235+
inputs.put("Date", currentDateTime.format(dateFormatter));
236+
inputs.put("Time", currentDateTime.format(timeFormatter));
237+
inputs.put("DateTime", currentDateTime.format(fullDateFormatter));
238+
239+
String systemContent = config.system;
240+
241+
// Replace placeholders in system content
242+
for (Map.Entry<String, Object> entry : inputs.entrySet()) {
243+
if (entry.getValue() != null) {
244+
systemContent = systemContent.replace(String.format("{{%s}}", entry.getKey()), entry.getValue().toString());
245+
}
246+
}
247+
248+
userTextMessages.add(text);
249+
250+
if (config.maxHistory > 0) {
251+
while (userTextMessages.size() > config.maxHistory) {
252+
userTextMessages.remove(0);
253+
}
254+
} else {
255+
userTextMessages.clear();
256+
}
257+
258+
String finalText = systemContent + " " + text;
259+
260+
// sentence chunking stream processing
225261
final StringBuilder[] sentenceBuilder = { new StringBuilder() };
226262
final int[] lastProcessedLength = { 0 }; // Track the length of already
227263
// processed text
228264

229-
OllamaResult result = ollamaAPI.generate("llama3", text, false, new OptionsBuilder().build(), (s) -> {
265+
OllamaResult result = ollamaAPI.generate("llama3", finalText, false, new OptionsBuilder().build(), (s) -> {
230266
// Append only the new portion of the text
231267
String newText = s.substring(lastProcessedLength[0]);
232268
sentenceBuilder[0].append(newText);

0 commit comments

Comments
 (0)