Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,7 +53,7 @@ def agent_node(state: OrderState) -> OrderState:
|
|
| 53 |
|
| 54 |
# Ensure we always have at least a system message
|
| 55 |
if not state.get("messages", []):
|
| 56 |
-
return defaults | state | {"messages": []}
|
| 57 |
|
| 58 |
try:
|
| 59 |
# Prepend system instruction if not already present
|
|
@@ -69,6 +69,34 @@ def agent_node(state: OrderState) -> OrderState:
|
|
| 69 |
# Fallback if LLM processing fails
|
| 70 |
return defaults | state | {"messages": [AIMessage(content=f"I'm having trouble processing that. {str(e)}")]}
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
def maybe_route_to_tools(state: OrderState) -> str:
|
| 73 |
"""Route between chat and tool nodes."""
|
| 74 |
if not (msgs := state.get("messages", [])):
|
|
@@ -84,8 +112,11 @@ def maybe_route_to_tools(state: OrderState) -> str:
|
|
| 84 |
if any(tool["name"] in tool_node.tools_by_name.keys() for tool in msg.tool_calls):
|
| 85 |
print("from agent GOTO tools node")
|
| 86 |
return "tools"
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
print("tool call failed,
|
| 89 |
return "human"
|
| 90 |
|
| 91 |
def human_node(state: OrderState) -> OrderState:
|
|
@@ -127,11 +158,13 @@ graph_builder = StateGraph(OrderState)
|
|
| 127 |
graph_builder.add_node("agent", agent_node)
|
| 128 |
graph_builder.add_node("human", human_node)
|
| 129 |
graph_builder.add_node("tools", tool_node)
|
|
|
|
| 130 |
|
| 131 |
# Add edges and routing
|
| 132 |
graph_builder.add_conditional_edges("agent", maybe_route_to_tools)
|
| 133 |
graph_builder.add_conditional_edges("human", maybe_exit_human_node)
|
| 134 |
graph_builder.add_edge("tools", "agent")
|
|
|
|
| 135 |
graph_builder.add_edge(START, "human")
|
| 136 |
|
| 137 |
# Compile the graph
|
|
|
|
| 53 |
|
| 54 |
# Ensure we always have at least a system message
|
| 55 |
if not state.get("messages", []):
|
| 56 |
+
return defaults | state | {"messages": [SystemMessage(content=SYSINT), new_output]}
|
| 57 |
|
| 58 |
try:
|
| 59 |
# Prepend system instruction if not already present
|
|
|
|
| 69 |
# Fallback if LLM processing fails
|
| 70 |
return defaults | state | {"messages": [AIMessage(content=f"I'm having trouble processing that. {str(e)}")]}
|
| 71 |
|
| 72 |
+
def interactive_tools_node(state: OrderState) -> OrderState:
|
| 73 |
+
"""Handles interactive tool calls."""
|
| 74 |
+
logging.info("order node")
|
| 75 |
+
tool_msg = state.get("messages", [])[-1]
|
| 76 |
+
order = state.get("order", [])
|
| 77 |
+
outbound_msgs = []
|
| 78 |
+
|
| 79 |
+
for tool_call in tool_msg.tool_calls:
|
| 80 |
+
tool_name = tool_call["name"]
|
| 81 |
+
tool_args = tool_call["args"]
|
| 82 |
+
|
| 83 |
+
if tool_name == "wikipedia_search":
|
| 84 |
+
page = wikipedia.page(title)
|
| 85 |
+
response = page.content[:300]
|
| 86 |
+
|
| 87 |
+
else:
|
| 88 |
+
raise NotImplementedError(f'Unknown tool call: {tool_name}')
|
| 89 |
+
|
| 90 |
+
outbound_msgs.append(
|
| 91 |
+
ToolMessage(
|
| 92 |
+
content=response,
|
| 93 |
+
name=tool_name,
|
| 94 |
+
tool_call_id=tool_call["id"],
|
| 95 |
+
)
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return {"messages": outbound_msgs, "order": order, "finished": False}
|
| 99 |
+
|
| 100 |
def maybe_route_to_tools(state: OrderState) -> str:
|
| 101 |
"""Route between chat and tool nodes."""
|
| 102 |
if not (msgs := state.get("messages", [])):
|
|
|
|
| 112 |
if any(tool["name"] in tool_node.tools_by_name.keys() for tool in msg.tool_calls):
|
| 113 |
print("from agent GOTO tools node")
|
| 114 |
return "tools"
|
| 115 |
+
else:
|
| 116 |
+
logging.info("from chatbot GOTO order node")
|
| 117 |
+
return "interactive_tools"
|
| 118 |
|
| 119 |
+
print("tool call failed, quitting")
|
| 120 |
return "human"
|
| 121 |
|
| 122 |
def human_node(state: OrderState) -> OrderState:
|
|
|
|
| 158 |
graph_builder.add_node("agent", agent_node)
|
| 159 |
graph_builder.add_node("human", human_node)
|
| 160 |
graph_builder.add_node("tools", tool_node)
|
| 161 |
+
graph_builder.add_node("interactive_tools", interactive_tools_node)
|
| 162 |
|
| 163 |
# Add edges and routing
|
| 164 |
graph_builder.add_conditional_edges("agent", maybe_route_to_tools)
|
| 165 |
graph_builder.add_conditional_edges("human", maybe_exit_human_node)
|
| 166 |
graph_builder.add_edge("tools", "agent")
|
| 167 |
+
graph_builder.add_edge("interactive_tools", "agent")
|
| 168 |
graph_builder.add_edge(START, "human")
|
| 169 |
|
| 170 |
# Compile the graph
|