Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -123,18 +123,23 @@ def init_state(question: str) -> AgentState:
|
|
123 |
"iterations": 0
|
124 |
}
|
125 |
|
|
|
|
|
126 |
def should_continue(state: AgentState) -> str:
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
if state['iterations'] >= 5:
|
132 |
-
return "end"
|
133 |
-
if "FINAL ANSWER:" in last_msg.get('content', ''):
|
134 |
return "end"
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
|
140 |
def reasoning_node(state: AgentState) -> AgentState:
|
@@ -209,33 +214,37 @@ def reasoning_node(state: AgentState) -> AgentState:
|
|
209 |
|
210 |
|
211 |
def tool_node(state: AgentState) -> AgentState:
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
"
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
|
|
|
|
237 |
return state
|
238 |
|
|
|
239 |
def parse_agent_response(response: str) -> tuple:
|
240 |
"""Extract reasoning, action, and input from response"""
|
241 |
reasoning = response.split("Reasoning:")[1].split("Action:")[0].strip()
|
|
|
123 |
"iterations": 0
|
124 |
}
|
125 |
|
126 |
+
|
127 |
+
|
128 |
def should_continue(state: AgentState) -> str:
|
129 |
+
last_message = state["history"][-1]
|
130 |
+
|
131 |
+
# If the last message is FINAL ANSWER, stop
|
132 |
+
if isinstance(last_message, AIMessage) and "FINAL ANSWER:" in last_message.content:
|
|
|
|
|
|
|
133 |
return "end"
|
134 |
+
|
135 |
+
# If an action_request was just added, continue to tool
|
136 |
+
for msg in reversed(state["history"]):
|
137 |
+
if isinstance(msg, dict) and msg.get("role") == "action_request":
|
138 |
+
return "continue"
|
139 |
+
|
140 |
+
# Otherwise, reason again
|
141 |
+
return "reason"
|
142 |
+
|
143 |
|
144 |
|
145 |
def reasoning_node(state: AgentState) -> AgentState:
|
|
|
214 |
|
215 |
|
216 |
def tool_node(state: AgentState) -> AgentState:
|
217 |
+
from langchain.schema import AIMessage
|
218 |
+
|
219 |
+
# Get the last tool action request
|
220 |
+
tool_call = None
|
221 |
+
for msg in reversed(state["history"]):
|
222 |
+
if isinstance(msg, dict) and msg.get("role") == "action_request":
|
223 |
+
tool_call = msg
|
224 |
+
break
|
225 |
+
|
226 |
+
if not tool_call:
|
227 |
+
raise ValueError("No tool call found in history")
|
228 |
+
|
229 |
+
tool_name = tool_call["tool"]
|
230 |
+
tool_input = tool_call["input"]
|
231 |
+
|
232 |
+
# Look up and invoke the tool
|
233 |
+
tool_fn = next((t for t in BasicAgent().tools if t.__name__ == tool_name), None)
|
234 |
+
if tool_fn is None:
|
235 |
+
raise ValueError(f"Tool '{tool_name}' not found")
|
236 |
+
|
237 |
+
try:
|
238 |
+
tool_output = tool_fn(tool_input)
|
239 |
+
except Exception as e:
|
240 |
+
tool_output = f"[Tool Error] {str(e)}"
|
241 |
+
|
242 |
+
# Store tool result as AIMessage
|
243 |
+
state["history"].append(AIMessage(content=f"[{tool_name} output]\n{tool_output}"))
|
244 |
+
|
245 |
return state
|
246 |
|
247 |
+
|
248 |
def parse_agent_response(response: str) -> tuple:
|
249 |
"""Extract reasoning, action, and input from response"""
|
250 |
reasoning = response.split("Reasoning:")[1].split("Action:")[0].strip()
|