Skip to content

Commit

Permalink
feat(persistence): refine Graph definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Sep 1, 2024
1 parent 41ab466 commit f09aeb6
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions how-tos/persistence.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -162,31 +162,6 @@
" // This is a placeholder for the actual implementation\n",
" return \"Cold, with a low of 13 degrees\";\n",
" }\n",
"}\n",
"\n",
"public recoord ToolInfo( ToolSpecification specification; ToolExecutor executor )\n",
"\n",
" public static List<ToolInfo> of( Object ...objectsWithTools) {\n",
" return fromArray( (Object[])objectsWithTools );\n",
" }\n",
" public static List<ToolInfo> fromArray( Object[] objectsWithTools ) {\n",
" List<ToolInfo> toolSpecifications = new ArrayList<>();\n",
"\n",
" for (Object objectWithTools : objectsWithTools) {\n",
" for (Method method : objectWithTools.getClass().getDeclaredMethods()) {\n",
" if (method.isAnnotationPresent(Tool.class)) {\n",
" ToolSpecification toolSpecification = toolSpecificationFrom(method);\n",
" ToolExecutor executor = new DefaultToolExecutor(objectWithTools, method);\n",
" toolSpecifications.add( new ToolInfo( toolSpecification, executor));\n",
" }\n",
" }\n",
" }\n",
" return unmodifiableList(toolSpecifications);\n",
" }\n",
" public static List<ToolInfo> fromList(List<Object> objectsWithTools ) {\n",
" return fromArray(objectsWithTools.toArray());\n",
" }\n",
"\n",
"}"
]
},
Expand Down Expand Up @@ -279,10 +254,13 @@
},
{
"cell_type": "code",
"execution_count": 88,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import static org.bsc.langgraph4j.StateGraph.START;\n",
"import static org.bsc.langgraph4j.StateGraph.END;\n",
"import org.bsc.langgraph4j.StateGraph;\n",
"import org.bsc.langgraph4j.action.EdgeAction;\n",
"import static org.bsc.langgraph4j.action.AsyncEdgeAction.edge_async;\n",
"import org.bsc.langgraph4j.action.NodeAction;\n",
Expand All @@ -294,15 +272,19 @@
"// Route Message \n",
"EdgeAction<MessageState> routeMessage = state -> {\n",
" \n",
" Optional<AiMessage> lastMessage = state.lastMessage();\n",
" var lastMessage = state.lastMessage();\n",
" \n",
" if ( lastMessage.isPresent()) {\n",
" if ( !lastMessage.isPresent()) {\n",
" return \"exit\";\n",
" }\n",
"\n",
" var message = (AiMessage)lastMessage.get();\n",
"\n",
" // If no tools are called, we can finish (respond to the user)\n",
" if ( !lastMessage.get().hasToolExecutionRequests() ) {\n",
" if ( !message.hasToolExecutionRequests() ) {\n",
" return \"exit\";\n",
" }\n",
" \n",
" // Otherwise if there is, we continue and call the tools\n",
" return \"next\";\n",
"};\n",
Expand All @@ -323,7 +305,8 @@
"// Invoke Tool \n",
"NodeAction<MessageState> invokeTool = state -> {\n",
"\n",
" var lastMessage = (AiMessage)state.lastMessage().orElseThrow( () -> ( new IllegalStateException( \"last message not found!\")) );\n",
" var lastMessage = (AiMessage)state.lastMessage()\n",
" .orElseThrow( () -> ( new IllegalStateException( \"last message not found!\")) );\n",
"\n",
" var executionRequest = lastMessage.toolExecutionRequests().get(0);\n",
"\n",
Expand Down

0 comments on commit f09aeb6

Please sign in to comment.