-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToolCallExample.java
More file actions
47 lines (37 loc) · 1.66 KB
/
Copy pathToolCallExample.java
File metadata and controls
47 lines (37 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package base;
import java.util.concurrent.StructuredTaskScope;
import java.util.concurrent.StructuredTaskScope.Subtask;
public class ToolCallExample {
// Tool 1: Weather API - 200ms =
static String callWeatherTool() throws InterruptedException {
Thread.sleep(200);
return "Weather: 25C";
}
// Tool 2: DB Query - 300ms
static String callDatabaseTool() throws InterruptedException {
Thread.sleep(300);
return "User: Yugi from Hyd";
}
// Tool 3: LLM call - 500ms
static String callLlmTool() throws InterruptedException {
Thread.sleep(500);
throw new RuntimeException("LLM timeout!");
}
public static void main(String[] args) throws Exception {
System.out.println("Starting 3 tools parallel...");
long start = System.currentTimeMillis();
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
Subtask<String> weather = scope.fork(ToolCallExample::callWeatherTool);
Subtask<String> db = scope.fork(ToolCallExample::callDatabaseTool);
Subtask<String> llm = scope.fork(ToolCallExample::callLlmTool);
scope.join();
scope.throwIfFailed();
String result = weather.get() + " | " + db.get() + " | " + llm.get();
System.out.println("All success: " + result);
} catch (Exception e) {
long end = System.currentTimeMillis();
System.out.println("FAILED in " + (end - start) + "ms");
System.out.println("Error: " + e.getMessage());
}
}
}