Skip to content

Commit c9d9994

Browse files
committed
refactor(notebook): refine 'Multi-agent supervisor' implementation
- use NodeAction rather than AsyncNodeAction
1 parent e37369a commit c9d9994

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

how-tos/multi-agent-supervisor.ipynb

+33-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
{
1919
"cell_type": "code",
20-
"execution_count": null,
20+
"execution_count": 1,
2121
"metadata": {},
2222
"outputs": [],
2323
"source": [
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": null,
32+
"execution_count": 2,
3333
"metadata": {},
3434
"outputs": [],
3535
"source": [
@@ -65,7 +65,7 @@
6565
},
6666
{
6767
"cell_type": "code",
68-
"execution_count": 28,
68+
"execution_count": 4,
6969
"metadata": {},
7070
"outputs": [],
7171
"source": [
@@ -101,7 +101,7 @@
101101
},
102102
{
103103
"cell_type": "code",
104-
"execution_count": 29,
104+
"execution_count": 5,
105105
"metadata": {},
106106
"outputs": [],
107107
"source": [
@@ -122,7 +122,7 @@
122122
},
123123
{
124124
"cell_type": "code",
125-
"execution_count": 30,
125+
"execution_count": 6,
126126
"metadata": {},
127127
"outputs": [],
128128
"source": [
@@ -153,7 +153,7 @@
153153
},
154154
{
155155
"cell_type": "code",
156-
"execution_count": 31,
156+
"execution_count": 9,
157157
"metadata": {},
158158
"outputs": [],
159159
"source": [
@@ -184,7 +184,7 @@
184184
},
185185
{
186186
"cell_type": "code",
187-
"execution_count": 32,
187+
"execution_count": 10,
188188
"metadata": {},
189189
"outputs": [],
190190
"source": [
@@ -197,13 +197,12 @@
197197
"import dev.langchain4j.data.message.AiMessage;\n",
198198
"import dev.langchain4j.data.message.UserMessage;\n",
199199
"import dev.langchain4j.model.chat.ChatLanguageModel;\n",
200-
"import org.bsc.langgraph4j.action.AsyncNodeAction;\n",
200+
"import org.bsc.langgraph4j.action.NodeAction;\n",
201201
"import java.util.concurrent.CompletableFuture;\n",
202202
"import static java.lang.String.format;\n",
203203
"\n",
204204
"\n",
205-
"class SupervisorAgent implements AsyncNodeAction<State> {\n",
206-
"\n",
205+
"class SupervisorAgent implements NodeAction<State> {\n",
207206
"\n",
208207
" static class Router {\n",
209208
" @Description(\"Worker to route to next. If no workers needed, route to FINISH.\")\n",
@@ -234,7 +233,7 @@
234233
" }\n",
235234
"\n",
236235
" @Override\n",
237-
" public CompletableFuture<Map<String, Object>> apply(State state) {\n",
236+
" public Map<String, Object> apply(State state) throws Exception { \n",
238237
" \n",
239238
" var message = state.lastMessage().orElseThrow();\n",
240239
"\n",
@@ -248,7 +247,7 @@
248247
" \n",
249248
" var result = service.evaluate( m, text );\n",
250249
" \n",
251-
" return CompletableFuture.completedFuture(Map.of( \"next\", result.next ));\n",
250+
" return Map.of( \"next\", result.next );\n",
252251
" }\n",
253252
"}\n"
254253
]
@@ -264,11 +263,11 @@
264263
},
265264
{
266265
"cell_type": "code",
267-
"execution_count": 33,
266+
"execution_count": 11,
268267
"metadata": {},
269268
"outputs": [],
270269
"source": [
271-
"class ResearchAgent implements AsyncNodeAction<State> {\n",
270+
"class ResearchAgent implements NodeAction<State> {\n",
272271
" static class Tools {\n",
273272
"\n",
274273
" @Tool(\"\"\"\n",
@@ -295,15 +294,15 @@
295294
" .build();\n",
296295
" }\n",
297296
" @Override\n",
298-
" public CompletableFuture<Map<String, Object>> apply(State state) {\n",
297+
" public Map<String, Object> apply(State state) throws Exception {\n",
299298
" var message = state.lastMessage().orElseThrow();\n",
300299
" var text = switch( message.type() ) {\n",
301300
" case USER -> ((UserMessage)message).singleText();\n",
302301
" case AI -> ((AiMessage)message).text();\n",
303302
" default -> throw new IllegalStateException(\"unexpected message type: \" + message.type() );\n",
304303
" };\n",
305304
" var result = service.search( text );\n",
306-
" return CompletableFuture.completedFuture(Map.of( \"messages\", AiMessage.from(result) ));\n",
305+
" return Map.of( \"messages\", AiMessage.from(result) );\n",
307306
"\n",
308307
" }\n",
309308
"}\n"
@@ -320,11 +319,11 @@
320319
},
321320
{
322321
"cell_type": "code",
323-
"execution_count": 34,
322+
"execution_count": 12,
324323
"metadata": {},
325324
"outputs": [],
326325
"source": [
327-
"class CoderAgent implements AsyncNodeAction<State> {\n",
326+
"class CoderAgent implements NodeAction<State> {\n",
328327
" static class Tools {\n",
329328
"\n",
330329
" @Tool(\"\"\"\n",
@@ -344,22 +343,22 @@
344343
"\n",
345344
" final Service service;\n",
346345
"\n",
347-
" public CoderAgent( ChatLanguageModel model ) {\n",
346+
" public CoderAgent( ChatLanguageModel model ) throws Exception {\n",
348347
" service = AiServices.builder( Service.class )\n",
349348
" .chatLanguageModel(model)\n",
350349
" .tools( new Tools() )\n",
351350
" .build();\n",
352351
" }\n",
353352
" @Override\n",
354-
" public CompletableFuture<Map<String, Object>> apply(State state) {\n",
353+
" public Map<String, Object> apply(State state) {\n",
355354
" var message = state.lastMessage().orElseThrow();\n",
356355
" var text = switch( message.type() ) {\n",
357356
" case USER -> ((UserMessage)message).singleText();\n",
358357
" case AI -> ((AiMessage)message).text();\n",
359358
" default -> throw new IllegalStateException(\"unexpected message type: \" + message.type() );\n",
360359
" };\n",
361360
" var result = service.evaluate( text );\n",
362-
" return CompletableFuture.completedFuture(Map.of( \"messages\", AiMessage.from(result) ));\n",
361+
" return Map.of( \"messages\", AiMessage.from(result) );\n",
363362
"\n",
364363
" }\n",
365364
"}\n"
@@ -374,7 +373,7 @@
374373
},
375374
{
376375
"cell_type": "code",
377-
"execution_count": 35,
376+
"execution_count": 13,
378377
"metadata": {},
379378
"outputs": [],
380379
"source": [
@@ -407,20 +406,25 @@
407406
},
408407
{
409408
"cell_type": "code",
410-
"execution_count": 36,
409+
"execution_count": 14,
411410
"metadata": {},
412411
"outputs": [],
413412
"source": [
414413
"import org.bsc.langgraph4j.StateGraph;\n",
415414
"import static org.bsc.langgraph4j.StateGraph.END;\n",
416415
"import static org.bsc.langgraph4j.StateGraph.START;\n",
417416
"import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
417+
"import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;\n",
418+
"\n",
418419
"\n",
420+
"var supervisor = new SupervisorAgent(model);\n",
421+
"var coder = new CoderAgent(modelWithTool);\n",
422+
"var researcher = new ResearchAgent(modelWithTool);\n",
419423
"\n",
420424
"var workflow = new StateGraph<>( State.SCHEMA, new StateSerializer() )\n",
421-
".addNode( \"supervisor\", new SupervisorAgent(model) )\n",
422-
".addNode( \"coder\", new CoderAgent(modelWithTool) )\n",
423-
".addNode( \"researcher\", new ResearchAgent(modelWithTool) )\n",
425+
".addNode( \"supervisor\", node_async(supervisor)) \n",
426+
".addNode( \"coder\", node_async(coder) )\n",
427+
".addNode( \"researcher\",node_async(researcher) )\n",
424428
".addEdge( START, \"supervisor\")\n",
425429
".addConditionalEdges( \"supervisor\",\n",
426430
" edge_async( state ->\n",
@@ -445,7 +449,7 @@
445449
},
446450
{
447451
"cell_type": "code",
448-
"execution_count": 37,
452+
"execution_count": 15,
449453
"metadata": {},
450454
"outputs": [
451455
{
@@ -471,7 +475,7 @@
471475
},
472476
{
473477
"cell_type": "code",
474-
"execution_count": 38,
478+
"execution_count": 16,
475479
"metadata": {},
476480
"outputs": [
477481
{
@@ -506,7 +510,7 @@
506510
},
507511
{
508512
"cell_type": "code",
509-
"execution_count": 39,
513+
"execution_count": 17,
510514
"metadata": {},
511515
"outputs": [
512516
{

0 commit comments

Comments
 (0)