Update app.py
Browse files
app.py
CHANGED
|
@@ -46,16 +46,29 @@ graph.add_node("tool", invoke_tool)
|
|
| 46 |
graph.add_edge("tool", END)
|
| 47 |
graph.set_entry_point("agent")
|
| 48 |
|
|
|
|
| 49 |
def router(state):
|
| 50 |
tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
|
| 51 |
-
if len(tool_calls):
|
| 52 |
-
return "
|
| 53 |
else:
|
| 54 |
-
return "end"
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
app_graph
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# Save graph visualization as an image
|
| 61 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|
|
|
|
| 46 |
graph.add_edge("tool", END)
|
| 47 |
graph.set_entry_point("agent")
|
| 48 |
|
| 49 |
+
# Define the router function
|
| 50 |
def router(state):
|
| 51 |
tool_calls = state['messages'][-1].additional_kwargs.get("tool_calls", [])
|
| 52 |
+
if len(tool_calls):
|
| 53 |
+
return "tools"
|
| 54 |
else:
|
| 55 |
+
return "end"
|
| 56 |
+
|
| 57 |
+
# Add graph nodes and edges
|
| 58 |
+
app_graph = StateGraph(MessagesState)
|
| 59 |
+
app_graph.add_node("assistant", assistant)
|
| 60 |
+
app_graph.add_node("tools", ToolNode(tools))
|
| 61 |
+
|
| 62 |
+
# Add conditional routing logic
|
| 63 |
+
app_graph.add_edge(START, "assistant")
|
| 64 |
+
app_graph.add_conditional_edges("assistant", router, {
|
| 65 |
+
"tools": "tools",
|
| 66 |
+
"end": END
|
| 67 |
+
})
|
| 68 |
+
app_graph.add_edge("tools", "assistant")
|
| 69 |
+
|
| 70 |
+
# Compile the graph
|
| 71 |
+
react_graph = app_graph.compile()
|
| 72 |
|
| 73 |
# Save graph visualization as an image
|
| 74 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmpfile:
|