|
162 | 162 | " // This is a placeholder for the actual implementation\n",
|
163 | 163 | " return \"Cold, with a low of 13 degrees\";\n",
|
164 | 164 | " }\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", |
190 | 165 | "}"
|
191 | 166 | ]
|
192 | 167 | },
|
|
279 | 254 | },
|
280 | 255 | {
|
281 | 256 | "cell_type": "code",
|
282 |
| - "execution_count": 88, |
| 257 | + "execution_count": null, |
283 | 258 | "metadata": {},
|
284 | 259 | "outputs": [],
|
285 | 260 | "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", |
286 | 264 | "import org.bsc.langgraph4j.action.EdgeAction;\n",
|
287 | 265 | "import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
|
288 | 266 | "import org.bsc.langgraph4j.action.NodeAction;\n",
|
|
294 | 272 | "// Route Message \n",
|
295 | 273 | "EdgeAction<MessageState> routeMessage = state -> {\n",
|
296 | 274 | " \n",
|
297 |
| - " Optional<AiMessage> lastMessage = state.lastMessage();\n", |
| 275 | + " var lastMessage = state.lastMessage();\n", |
298 | 276 | " \n",
|
299 |
| - " if ( lastMessage.isPresent()) {\n", |
| 277 | + " if ( !lastMessage.isPresent()) {\n", |
300 | 278 | " return \"exit\";\n",
|
301 | 279 | " }\n",
|
| 280 | + "\n", |
| 281 | + " var message = (AiMessage)lastMessage.get();\n", |
| 282 | + "\n", |
302 | 283 | " // If no tools are called, we can finish (respond to the user)\n",
|
303 |
| - " if ( !lastMessage.get().hasToolExecutionRequests() ) {\n", |
| 284 | + " if ( !message.hasToolExecutionRequests() ) {\n", |
304 | 285 | " return \"exit\";\n",
|
305 | 286 | " }\n",
|
| 287 | + " \n", |
306 | 288 | " // Otherwise if there is, we continue and call the tools\n",
|
307 | 289 | " return \"next\";\n",
|
308 | 290 | "};\n",
|
|
323 | 305 | "// Invoke Tool \n",
|
324 | 306 | "NodeAction<MessageState> invokeTool = state -> {\n",
|
325 | 307 | "\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", |
327 | 310 | "\n",
|
328 | 311 | " var executionRequest = lastMessage.toolExecutionRequests().get(0);\n",
|
329 | 312 | "\n",
|
|
0 commit comments