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.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));
|
||||||
}
|
}
|
||||||
|
|
||||||
checkIfResponceMessage(response);
|
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(result -> {
|
||||||
qurryOllama().thenAccept(this::handleResponse);
|
checkIfResponceMessage(response);
|
||||||
|
qurryOllama().thenAccept(this::handleResponse);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -705,21 +710,23 @@ 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) {
|
||||||
if(!(call instanceof JSONObject jsonObject)) return;
|
return CompletableFuture.runAsync(() -> {
|
||||||
if(!jsonObject.has("function")) return;
|
if (!(call instanceof JSONObject jsonObject)) return;
|
||||||
|
if (!jsonObject.has("function")) return;
|
||||||
|
|
||||||
JSONObject function = jsonObject.getJSONObject("function");
|
JSONObject function = jsonObject.getJSONObject("function");
|
||||||
OllamaFunctionTool func = findTool(function);
|
OllamaFunctionTool func = findTool(function);
|
||||||
|
|
||||||
if(func == null) {
|
if (func == null) {
|
||||||
reportToolNotFound(function);
|
reportToolNotFound(function);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user