|
17 | 17 | },
|
18 | 18 | {
|
19 | 19 | "cell_type": "code",
|
20 |
| - "execution_count": null, |
| 20 | + "execution_count": 1, |
21 | 21 | "metadata": {},
|
22 | 22 | "outputs": [],
|
23 | 23 | "source": [
|
|
29 | 29 | },
|
30 | 30 | {
|
31 | 31 | "cell_type": "code",
|
32 |
| - "execution_count": null, |
| 32 | + "execution_count": 2, |
33 | 33 | "metadata": {},
|
34 | 34 | "outputs": [],
|
35 | 35 | "source": [
|
|
65 | 65 | },
|
66 | 66 | {
|
67 | 67 | "cell_type": "code",
|
68 |
| - "execution_count": 28, |
| 68 | + "execution_count": 4, |
69 | 69 | "metadata": {},
|
70 | 70 | "outputs": [],
|
71 | 71 | "source": [
|
|
101 | 101 | },
|
102 | 102 | {
|
103 | 103 | "cell_type": "code",
|
104 |
| - "execution_count": 29, |
| 104 | + "execution_count": 5, |
105 | 105 | "metadata": {},
|
106 | 106 | "outputs": [],
|
107 | 107 | "source": [
|
|
122 | 122 | },
|
123 | 123 | {
|
124 | 124 | "cell_type": "code",
|
125 |
| - "execution_count": 30, |
| 125 | + "execution_count": 6, |
126 | 126 | "metadata": {},
|
127 | 127 | "outputs": [],
|
128 | 128 | "source": [
|
|
153 | 153 | },
|
154 | 154 | {
|
155 | 155 | "cell_type": "code",
|
156 |
| - "execution_count": 31, |
| 156 | + "execution_count": 9, |
157 | 157 | "metadata": {},
|
158 | 158 | "outputs": [],
|
159 | 159 | "source": [
|
|
184 | 184 | },
|
185 | 185 | {
|
186 | 186 | "cell_type": "code",
|
187 |
| - "execution_count": 32, |
| 187 | + "execution_count": 10, |
188 | 188 | "metadata": {},
|
189 | 189 | "outputs": [],
|
190 | 190 | "source": [
|
|
197 | 197 | "import dev.langchain4j.data.message.AiMessage;\n",
|
198 | 198 | "import dev.langchain4j.data.message.UserMessage;\n",
|
199 | 199 | "import dev.langchain4j.model.chat.ChatLanguageModel;\n",
|
200 |
| - "import org.bsc.langgraph4j.action.AsyncNodeAction;\n", |
| 200 | + "import org.bsc.langgraph4j.action.NodeAction;\n", |
201 | 201 | "import java.util.concurrent.CompletableFuture;\n",
|
202 | 202 | "import static java.lang.String.format;\n",
|
203 | 203 | "\n",
|
204 | 204 | "\n",
|
205 |
| - "class SupervisorAgent implements AsyncNodeAction<State> {\n", |
206 |
| - "\n", |
| 205 | + "class SupervisorAgent implements NodeAction<State> {\n", |
207 | 206 | "\n",
|
208 | 207 | " static class Router {\n",
|
209 | 208 | " @Description(\"Worker to route to next. If no workers needed, route to FINISH.\")\n",
|
|
234 | 233 | " }\n",
|
235 | 234 | "\n",
|
236 | 235 | " @Override\n",
|
237 |
| - " public CompletableFuture<Map<String, Object>> apply(State state) {\n", |
| 236 | + " public Map<String, Object> apply(State state) throws Exception { \n", |
238 | 237 | " \n",
|
239 | 238 | " var message = state.lastMessage().orElseThrow();\n",
|
240 | 239 | "\n",
|
|
248 | 247 | " \n",
|
249 | 248 | " var result = service.evaluate( m, text );\n",
|
250 | 249 | " \n",
|
251 |
| - " return CompletableFuture.completedFuture(Map.of( \"next\", result.next ));\n", |
| 250 | + " return Map.of( \"next\", result.next );\n", |
252 | 251 | " }\n",
|
253 | 252 | "}\n"
|
254 | 253 | ]
|
|
264 | 263 | },
|
265 | 264 | {
|
266 | 265 | "cell_type": "code",
|
267 |
| - "execution_count": 33, |
| 266 | + "execution_count": 11, |
268 | 267 | "metadata": {},
|
269 | 268 | "outputs": [],
|
270 | 269 | "source": [
|
271 |
| - "class ResearchAgent implements AsyncNodeAction<State> {\n", |
| 270 | + "class ResearchAgent implements NodeAction<State> {\n", |
272 | 271 | " static class Tools {\n",
|
273 | 272 | "\n",
|
274 | 273 | " @Tool(\"\"\"\n",
|
|
295 | 294 | " .build();\n",
|
296 | 295 | " }\n",
|
297 | 296 | " @Override\n",
|
298 |
| - " public CompletableFuture<Map<String, Object>> apply(State state) {\n", |
| 297 | + " public Map<String, Object> apply(State state) throws Exception {\n", |
299 | 298 | " var message = state.lastMessage().orElseThrow();\n",
|
300 | 299 | " var text = switch( message.type() ) {\n",
|
301 | 300 | " case USER -> ((UserMessage)message).singleText();\n",
|
302 | 301 | " case AI -> ((AiMessage)message).text();\n",
|
303 | 302 | " default -> throw new IllegalStateException(\"unexpected message type: \" + message.type() );\n",
|
304 | 303 | " };\n",
|
305 | 304 | " 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", |
307 | 306 | "\n",
|
308 | 307 | " }\n",
|
309 | 308 | "}\n"
|
|
320 | 319 | },
|
321 | 320 | {
|
322 | 321 | "cell_type": "code",
|
323 |
| - "execution_count": 34, |
| 322 | + "execution_count": 12, |
324 | 323 | "metadata": {},
|
325 | 324 | "outputs": [],
|
326 | 325 | "source": [
|
327 |
| - "class CoderAgent implements AsyncNodeAction<State> {\n", |
| 326 | + "class CoderAgent implements NodeAction<State> {\n", |
328 | 327 | " static class Tools {\n",
|
329 | 328 | "\n",
|
330 | 329 | " @Tool(\"\"\"\n",
|
|
344 | 343 | "\n",
|
345 | 344 | " final Service service;\n",
|
346 | 345 | "\n",
|
347 |
| - " public CoderAgent( ChatLanguageModel model ) {\n", |
| 346 | + " public CoderAgent( ChatLanguageModel model ) throws Exception {\n", |
348 | 347 | " service = AiServices.builder( Service.class )\n",
|
349 | 348 | " .chatLanguageModel(model)\n",
|
350 | 349 | " .tools( new Tools() )\n",
|
351 | 350 | " .build();\n",
|
352 | 351 | " }\n",
|
353 | 352 | " @Override\n",
|
354 |
| - " public CompletableFuture<Map<String, Object>> apply(State state) {\n", |
| 353 | + " public Map<String, Object> apply(State state) {\n", |
355 | 354 | " var message = state.lastMessage().orElseThrow();\n",
|
356 | 355 | " var text = switch( message.type() ) {\n",
|
357 | 356 | " case USER -> ((UserMessage)message).singleText();\n",
|
358 | 357 | " case AI -> ((AiMessage)message).text();\n",
|
359 | 358 | " default -> throw new IllegalStateException(\"unexpected message type: \" + message.type() );\n",
|
360 | 359 | " };\n",
|
361 | 360 | " 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", |
363 | 362 | "\n",
|
364 | 363 | " }\n",
|
365 | 364 | "}\n"
|
|
374 | 373 | },
|
375 | 374 | {
|
376 | 375 | "cell_type": "code",
|
377 |
| - "execution_count": 35, |
| 376 | + "execution_count": 13, |
378 | 377 | "metadata": {},
|
379 | 378 | "outputs": [],
|
380 | 379 | "source": [
|
|
407 | 406 | },
|
408 | 407 | {
|
409 | 408 | "cell_type": "code",
|
410 |
| - "execution_count": 36, |
| 409 | + "execution_count": 14, |
411 | 410 | "metadata": {},
|
412 | 411 | "outputs": [],
|
413 | 412 | "source": [
|
414 | 413 | "import org.bsc.langgraph4j.StateGraph;\n",
|
415 | 414 | "import static org.bsc.langgraph4j.StateGraph.END;\n",
|
416 | 415 | "import static org.bsc.langgraph4j.StateGraph.START;\n",
|
417 | 416 | "import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
|
| 417 | + "import static org.bsc.langgraph4j.action.AsyncNodeAction.node_async;\n", |
| 418 | + "\n", |
418 | 419 | "\n",
|
| 420 | + "var supervisor = new SupervisorAgent(model);\n", |
| 421 | + "var coder = new CoderAgent(modelWithTool);\n", |
| 422 | + "var researcher = new ResearchAgent(modelWithTool);\n", |
419 | 423 | "\n",
|
420 | 424 | "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", |
424 | 428 | ".addEdge( START, \"supervisor\")\n",
|
425 | 429 | ".addConditionalEdges( \"supervisor\",\n",
|
426 | 430 | " edge_async( state ->\n",
|
|
445 | 449 | },
|
446 | 450 | {
|
447 | 451 | "cell_type": "code",
|
448 |
| - "execution_count": 37, |
| 452 | + "execution_count": 15, |
449 | 453 | "metadata": {},
|
450 | 454 | "outputs": [
|
451 | 455 | {
|
|
471 | 475 | },
|
472 | 476 | {
|
473 | 477 | "cell_type": "code",
|
474 |
| - "execution_count": 38, |
| 478 | + "execution_count": 16, |
475 | 479 | "metadata": {},
|
476 | 480 | "outputs": [
|
477 | 481 | {
|
|
506 | 510 | },
|
507 | 511 | {
|
508 | 512 | "cell_type": "code",
|
509 |
| - "execution_count": 39, |
| 513 | + "execution_count": 17, |
510 | 514 | "metadata": {},
|
511 | 515 | "outputs": [
|
512 | 516 | {
|
|
0 commit comments