forked from neurodock/NeuroDock
Made tool calling async as well, not yet tested
This commit is contained in:
@@ -23,6 +23,7 @@ import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -688,13 +689,17 @@ public class Core {
|
||||
message.getJSONArray("tool_calls")
|
||||
));
|
||||
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>();
|
||||
|
||||
// Process each tool call
|
||||
for(Object call : message.getJSONArray("tool_calls")) {
|
||||
processToolCall(call);
|
||||
futures.add(processToolCall(call));
|
||||
}
|
||||
|
||||
checkIfResponceMessage(response);
|
||||
qurryOllama().thenAccept(this::handleResponse);
|
||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(result -> {
|
||||
checkIfResponceMessage(response);
|
||||
qurryOllama().thenAccept(this::handleResponse);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -705,21 +710,23 @@ public class Core {
|
||||
*
|
||||
* @param call a JSONObject representing the tool call
|
||||
*/
|
||||
private void processToolCall(Object call) {
|
||||
if(!(call instanceof JSONObject jsonObject)) return;
|
||||
if(!jsonObject.has("function")) return;
|
||||
private CompletableFuture<Void> processToolCall(Object call) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
if (!(call instanceof JSONObject jsonObject)) return;
|
||||
if (!jsonObject.has("function")) return;
|
||||
|
||||
JSONObject function = jsonObject.getJSONObject("function");
|
||||
OllamaFunctionTool func = findTool(function);
|
||||
JSONObject function = jsonObject.getJSONObject("function");
|
||||
OllamaFunctionTool func = findTool(function);
|
||||
|
||||
if(func == null) {
|
||||
reportToolNotFound(function);
|
||||
return;
|
||||
}
|
||||
if (func == null) {
|
||||
reportToolNotFound(function);
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject arguments = function.getJSONObject("arguments");
|
||||
renderToolCalling(func.renderCalling(function), function, arguments);
|
||||
executeToolCall(func, arguments);
|
||||
JSONObject arguments = function.getJSONObject("arguments");
|
||||
renderToolCalling(func.renderCalling(function), function, arguments);
|
||||
executeToolCall(func, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user