wt002 commited on
Commit
ecfb0b9
·
verified ·
1 Parent(s): 576528c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -600,7 +600,7 @@ class BasicAgent:
600
  "vector_store": self.vector_store
601
  },
602
  "reasoning": "",
603
- "iterations": 0, # Initialize iterations to 0
604
  "history": [HumanMessage(content=question)],
605
  "final_answer": None,
606
  "current_task": "Understand the question and plan the next step.",
@@ -609,7 +609,8 @@ class BasicAgent:
609
  }
610
 
611
  try:
612
- final_state = self.workflow.invoke(state, {"recursion_limit": 20}) # Increased recursion limit for LangGraph
 
613
 
614
  if final_state.get("final_answer") is not None:
615
  answer = final_state["final_answer"]
@@ -617,18 +618,26 @@ class BasicAgent:
617
  return answer
618
  else:
619
  print(f"--- ERROR: Agent finished without setting 'final_answer' for question: {question} ---")
620
- if final_state["history"]:
621
- last_message = final_state["history"][-1].content
 
 
 
 
 
622
  print(f"Last message in history: {last_message}")
623
  return f"Agent could not fully answer. Last message: {last_message}"
624
  else:
625
  return "Agent finished without providing a final answer and no history messages."
 
626
  except Exception as e:
627
  print(f"--- FATAL ERROR during agent execution: {e} ---")
 
628
  return f"An unexpected error occurred during agent execution: {str(e)}"
629
 
630
 
631
 
 
632
  def run_and_submit_all( profile: gr.OAuthProfile | None):
633
  """
634
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
600
  "vector_store": self.vector_store
601
  },
602
  "reasoning": "",
603
+ "iterations": 0,
604
  "history": [HumanMessage(content=question)],
605
  "final_answer": None,
606
  "current_task": "Understand the question and plan the next step.",
 
609
  }
610
 
611
  try:
612
+ # The invoke method returns an iterator, so we need to consume it to get the final state
613
+ final_state = self.workflow.invoke(state, {"recursion_limit": 20})
614
 
615
  if final_state.get("final_answer") is not None:
616
  answer = final_state["final_answer"]
 
618
  return answer
619
  else:
620
  print(f"--- ERROR: Agent finished without setting 'final_answer' for question: {question} ---")
621
+ # --- FIX START ---
622
+ # Safely get the history from the final_state.
623
+ # If 'history' key is missing or its value is None, default to an empty list.
624
+ current_history = final_state.get("history", [])
625
+
626
+ if current_history: # This checks if the list is not empty
627
+ last_message = current_history[-1].content
628
  print(f"Last message in history: {last_message}")
629
  return f"Agent could not fully answer. Last message: {last_message}"
630
  else:
631
  return "Agent finished without providing a final answer and no history messages."
632
+ # --- FIX END ---
633
  except Exception as e:
634
  print(f"--- FATAL ERROR during agent execution: {e} ---")
635
+ # In case of an unexpected error, return a helpful message
636
  return f"An unexpected error occurred during agent execution: {str(e)}"
637
 
638
 
639
 
640
+
641
  def run_and_submit_all( profile: gr.OAuthProfile | None):
642
  """
643
  Fetches all questions, runs the BasicAgent on them, submits all answers,