i-dhilip commited on
Commit
8e8aaf3
·
verified ·
1 Parent(s): fb4f3eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -120,21 +120,50 @@ class AdvancedAgent:
120
  SystemMessage(content=system_prompt),
121
  HumanMessage(content=question)
122
  ]
 
 
123
 
124
  # Run the graph
125
  try:
126
  result = self.graph.invoke({"messages": messages})
127
- # Extract the last AI message as the answer
128
- for msg in reversed(result["messages"]):
129
- if isinstance(msg, AIMessage) and not getattr(msg, "tool_call_id", None):
 
 
 
 
 
 
 
 
 
 
130
  return msg.content
131
 
132
- # Fallback if no valid AI message found
133
- return "I wasn't able to generate a proper response. Please try again."
 
134
  except Exception as e:
135
  print(f"Error running agent graph: {e}")
136
  return f"Sorry, I encountered an error while processing your question: {str(e)}"
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # Function to run and submit all questions
139
  def run_and_submit_all(profile: gr.OAuthProfile | None):
140
  """
 
120
  SystemMessage(content=system_prompt),
121
  HumanMessage(content=question)
122
  ]
123
+
124
+
125
 
126
  # Run the graph
127
  try:
128
  result = self.graph.invoke({"messages": messages})
129
+
130
+ # Extract the final AI message as the answer
131
+ final_messages = result["messages"]
132
+
133
+ # Find the last non-tool AI message
134
+ ai_messages = [msg for msg in final_messages if isinstance(msg, AIMessage)]
135
+ if not ai_messages:
136
+ return "I wasn't able to generate a proper response. Please try again."
137
+
138
+ # Get the last AI message that's not a tool call
139
+ for msg in reversed(ai_messages):
140
+ # Check if this is not a tool call message
141
+ if not hasattr(msg, 'tool_calls') or not msg.tool_calls:
142
  return msg.content
143
 
144
+ # If we only have tool call messages, return the content of the last AI message
145
+ return ai_messages[-1].content if ai_messages else "I wasn't able to generate a proper response. Please try again."
146
+
147
  except Exception as e:
148
  print(f"Error running agent graph: {e}")
149
  return f"Sorry, I encountered an error while processing your question: {str(e)}"
150
 
151
+
152
+
153
+ # # Run the graph
154
+ # try:
155
+ # result = self.graph.invoke({"messages": messages})
156
+ # # Extract the last AI message as the answer
157
+ # for msg in reversed(result["messages"]):
158
+ # if isinstance(msg, AIMessage) and not getattr(msg, "tool_call_id", None):
159
+ # return msg.content
160
+
161
+ # # Fallback if no valid AI message found
162
+ # return "I wasn't able to generate a proper response. Please try again."
163
+ # except Exception as e:
164
+ # print(f"Error running agent graph: {e}")
165
+ # return f"Sorry, I encountered an error while processing your question: {str(e)}"
166
+
167
  # Function to run and submit all questions
168
  def run_and_submit_all(profile: gr.OAuthProfile | None):
169
  """