Made tool calling async as well, not yet tested

This commit is contained in:
2026-06-28 17:58:44 +02:00
parent caf60eddef
commit 9c7ba9cb4f
@@ -23,6 +23,7 @@ import java.nio.file.Path;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@@ -688,13 +689,17 @@ public class Core {
message.getJSONArray("tool_calls") message.getJSONArray("tool_calls")
)); ));
List<CompletableFuture<Void>> futures = new ArrayList<>();
// Process each tool call // Process each tool call
for(Object call : message.getJSONArray("tool_calls")) { for(Object call : message.getJSONArray("tool_calls")) {
processToolCall(call); futures.add(processToolCall(call));
} }
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(result -> {
checkIfResponceMessage(response); checkIfResponceMessage(response);
qurryOllama().thenAccept(this::handleResponse); qurryOllama().thenAccept(this::handleResponse);
});
} }
/** /**
@@ -705,7 +710,8 @@ public class Core {
* *
* @param call a JSONObject representing the tool call * @param call a JSONObject representing the tool call
*/ */
private void processToolCall(Object call) { private CompletableFuture<Void> processToolCall(Object call) {
return CompletableFuture.runAsync(() -> {
if (!(call instanceof JSONObject jsonObject)) return; if (!(call instanceof JSONObject jsonObject)) return;
if (!jsonObject.has("function")) return; if (!jsonObject.has("function")) return;
@@ -720,6 +726,7 @@ public class Core {
JSONObject arguments = function.getJSONObject("arguments"); JSONObject arguments = function.getJSONObject("arguments");
renderToolCalling(func.renderCalling(function), function, arguments); renderToolCalling(func.renderCalling(function), function, arguments);
executeToolCall(func, arguments); executeToolCall(func, arguments);
});
} }
/** /**