yuki-zmstr commited on
Commit
a736a78
·
1 Parent(s): b7fd37b

All early return

Browse files
Files changed (1) hide show
  1. agent/react_agent.py +15 -3
agent/react_agent.py CHANGED
@@ -228,8 +228,15 @@ Available tools:
228
  if json_match:
229
  try:
230
  decision_data = json.loads(json_match.group())
 
 
 
 
 
 
231
  return AgentDecision(**decision_data)
232
- except:
 
233
  pass
234
 
235
  # Fallback parsing
@@ -270,8 +277,6 @@ Available tools:
270
  [HumanMessage(content=formatted_prompt)])
271
  logger.info(f"LLM response: {response.content}")
272
 
273
- # TODO: how to get the agent to ask for more input, without considering it as an error
274
-
275
  # Parse the decision
276
  try:
277
  decision = self._parse_agent_decision(response.content)
@@ -298,7 +303,14 @@ Available tools:
298
  agent_scratchpad += f"Action Input: {decision.action_input}\n"
299
 
300
  try:
 
301
  observation = tool.func(decision.action_input)
 
 
 
 
 
 
302
  except Exception as e:
303
  observation = f"Error: {str(e)}"
304
 
 
228
  if json_match:
229
  try:
230
  decision_data = json.loads(json_match.group())
231
+ logger.info(f"Parsed agent decision: {decision_data}")
232
+
233
+ # Convert action_input to string if it's a dict/object
234
+ if isinstance(decision_data.get('action_input'), (dict, list)):
235
+ decision_data['action_input'] = json.dumps(decision_data['action_input'])
236
+
237
  return AgentDecision(**decision_data)
238
+ except Exception as e:
239
+ logger.error(f"Error parsing decision JSON: {e}")
240
  pass
241
 
242
  # Fallback parsing
 
277
  [HumanMessage(content=formatted_prompt)])
278
  logger.info(f"LLM response: {response.content}")
279
 
 
 
280
  # Parse the decision
281
  try:
282
  decision = self._parse_agent_decision(response.content)
 
303
  agent_scratchpad += f"Action Input: {decision.action_input}\n"
304
 
305
  try:
306
+ logger.info(f"Executing tool: {decision.action}")
307
  observation = tool.func(decision.action_input)
308
+
309
+ # If it's a book recommendation and we got results successfully
310
+ if decision.action == "book_recommendation" and "Here are some book recommendations:" in observation:
311
+ self.chat_history.append(AIMessage(content=observation))
312
+ return observation
313
+
314
  except Exception as e:
315
  observation = f"Error: {str(e)}"
316