wt002 commited on
Commit
3363a47
·
verified ·
1 Parent(s): 02e0a0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -35
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
- """Determine if agent should continue or finish"""
128
- last_msg = state['history'][-1]
129
-
130
- # Stop conditions
131
- if state['iterations'] >= 5:
132
- return "end"
133
- if "FINAL ANSWER:" in last_msg.get('content', ''):
134
  return "end"
135
- if last_msg['role'] == 'tool':
136
- return "reason"
137
- return "continue"
 
 
 
 
 
 
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
- """Execute selected tool and update state"""
213
- last_action = state['history'][-1]
214
- tool_name = last_action['tool']
215
- tool_input = last_action['input']
216
-
217
- # Tool mapping
218
- tools = {
219
- "duckduckgo_search": duckduckgo_search,
220
- "wikipedia_search": wikipedia_search,
221
- "arxiv_search": arxiv_search,
222
- "document_qa": document_qa,
223
- "python_execution": python_execution
224
- }
225
-
226
- # Execute tool
227
- tool_result = tools[tool_name].invoke(tool_input)
228
-
229
- # Update state
230
- state['history'].append(ToolMessage(
231
- content=tool_result,
232
- tool_call_id=tool_name
233
- ))
234
- state['context'] = f"Tool Result ({tool_name}): {tool_result}"
235
- state['iterations'] += 1
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()