Skip to content

Commit f09aeb6

Browse files
committed
feat(persistence): refine Graph definition
1 parent 41ab466 commit f09aeb6

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

how-tos/persistence.ipynb

+13-30
Original file line numberDiff line numberDiff line change
@@ -162,31 +162,6 @@
162162
" // This is a placeholder for the actual implementation\n",
163163
" return \"Cold, with a low of 13 degrees\";\n",
164164
" }\n",
165-
"}\n",
166-
"\n",
167-
"public recoord ToolInfo( ToolSpecification specification; ToolExecutor executor )\n",
168-
"\n",
169-
" public static List<ToolInfo> of( Object ...objectsWithTools) {\n",
170-
" return fromArray( (Object[])objectsWithTools );\n",
171-
" }\n",
172-
" public static List<ToolInfo> fromArray( Object[] objectsWithTools ) {\n",
173-
" List<ToolInfo> toolSpecifications = new ArrayList<>();\n",
174-
"\n",
175-
" for (Object objectWithTools : objectsWithTools) {\n",
176-
" for (Method method : objectWithTools.getClass().getDeclaredMethods()) {\n",
177-
" if (method.isAnnotationPresent(Tool.class)) {\n",
178-
" ToolSpecification toolSpecification = toolSpecificationFrom(method);\n",
179-
" ToolExecutor executor = new DefaultToolExecutor(objectWithTools, method);\n",
180-
" toolSpecifications.add( new ToolInfo( toolSpecification, executor));\n",
181-
" }\n",
182-
" }\n",
183-
" }\n",
184-
" return unmodifiableList(toolSpecifications);\n",
185-
" }\n",
186-
" public static List<ToolInfo> fromList(List<Object> objectsWithTools ) {\n",
187-
" return fromArray(objectsWithTools.toArray());\n",
188-
" }\n",
189-
"\n",
190165
"}"
191166
]
192167
},
@@ -279,10 +254,13 @@
279254
},
280255
{
281256
"cell_type": "code",
282-
"execution_count": 88,
257+
"execution_count": null,
283258
"metadata": {},
284259
"outputs": [],
285260
"source": [
261+
"import static org.bsc.langgraph4j.StateGraph.START;\n",
262+
"import static org.bsc.langgraph4j.StateGraph.END;\n",
263+
"import org.bsc.langgraph4j.StateGraph;\n",
286264
"import org.bsc.langgraph4j.action.EdgeAction;\n",
287265
"import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
288266
"import org.bsc.langgraph4j.action.NodeAction;\n",
@@ -294,15 +272,19 @@
294272
"// Route Message \n",
295273
"EdgeAction<MessageState> routeMessage = state -> {\n",
296274
" \n",
297-
" Optional<AiMessage> lastMessage = state.lastMessage();\n",
275+
" var lastMessage = state.lastMessage();\n",
298276
" \n",
299-
" if ( lastMessage.isPresent()) {\n",
277+
" if ( !lastMessage.isPresent()) {\n",
300278
" return \"exit\";\n",
301279
" }\n",
280+
"\n",
281+
" var message = (AiMessage)lastMessage.get();\n",
282+
"\n",
302283
" // If no tools are called, we can finish (respond to the user)\n",
303-
" if ( !lastMessage.get().hasToolExecutionRequests() ) {\n",
284+
" if ( !message.hasToolExecutionRequests() ) {\n",
304285
" return \"exit\";\n",
305286
" }\n",
287+
" \n",
306288
" // Otherwise if there is, we continue and call the tools\n",
307289
" return \"next\";\n",
308290
"};\n",
@@ -323,7 +305,8 @@
323305
"// Invoke Tool \n",
324306
"NodeAction<MessageState> invokeTool = state -> {\n",
325307
"\n",
326-
" var lastMessage = (AiMessage)state.lastMessage().orElseThrow( () -> ( new IllegalStateException( \"last message not found!\")) );\n",
308+
" var lastMessage = (AiMessage)state.lastMessage()\n",
309+
" .orElseThrow( () -> ( new IllegalStateException( \"last message not found!\")) );\n",
327310
"\n",
328311
" var executionRequest = lastMessage.toolExecutionRequests().get(0);\n",
329312
"\n",

0 commit comments

Comments
 (0)