redhairedshanks1 commited on
Commit
6427bef
·
1 Parent(s): f1e4b62

Update services/pipeline_executor.py

Browse files
Files changed (1) hide show
  1. services/pipeline_executor.py +45 -40
services/pipeline_executor.py CHANGED
@@ -195,40 +195,13 @@ Execute each component by calling the corresponding tool."""
195
  # Add to conversation (Bedrock converse API format)
196
  conversation_history.append({"role": "assistant", "content": [{"text": assistant_message}]})
197
 
198
- # Check for Final Answer
199
- if "Final Answer:" in assistant_message or "final answer" in assistant_message.lower():
200
- # Done!
201
- if tool_results:
202
- structured_result = {
203
- "status": "completed",
204
- "components_executed": tool_results,
205
- "summary": {
206
- "total_tools_called": len(tool_results),
207
- "tools": list(tool_results.keys())
208
- },
209
- "final_output": assistant_message
210
- }
211
-
212
- yield {
213
- "type": "final",
214
- "data": structured_result,
215
- "executor": "bedrock"
216
- }
217
- else:
218
- yield {
219
- "type": "error",
220
- "error": "Bedrock completed but no tools were called",
221
- "executor": "bedrock"
222
- }
223
- return
224
-
225
- # Parse for Action and Action Input
226
  action_match = re.search(r'Action:\s*(\w+)', assistant_message)
227
- action_input_match = re.search(r'Action Input:\s*(\{.*?\})', assistant_message, re.DOTALL)
228
 
229
  if action_match and action_input_match:
230
  tool_name = action_match.group(1)
231
- action_input_str = action_input_match.group(1)
232
 
233
  try:
234
  # Parse JSON input
@@ -265,28 +238,60 @@ Execute each component by calling the corresponding tool."""
265
  observation_message = f"Observation: {observation}"
266
  conversation_history.append({"role": "user", "content": [{"text": observation_message}]})
267
 
 
 
 
268
  else:
269
  # Unknown tool
270
  error_msg = f"Unknown tool: {tool_name}"
271
  conversation_history.append({"role": "user", "content": [{"text": f"Error: {error_msg}"}]})
 
272
 
273
  except json.JSONDecodeError as e:
274
  # Invalid JSON
275
  error_msg = f"Invalid JSON in Action Input: {e}"
276
  conversation_history.append({"role": "user", "content": [{"text": f"Error: {error_msg}"}]})
277
- else:
278
- # No action found - agent might be confused or done
279
- if iteration > 0 and not has_called_tools:
280
- # Agent isn't calling tools properly
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
  yield {
282
  "type": "error",
283
- "error": "Bedrock didn't call tools in correct format. Falling back to CrewAI.",
284
- "executor": "bedrock",
285
- "debug_output": assistant_message[:500]
286
  }
287
- return
288
- elif iteration > 0:
289
- # Has called some tools but stopped - might be done
 
 
 
 
 
 
 
 
 
 
 
290
  structured_result = {
291
  "status": "completed",
292
  "components_executed": tool_results,
 
195
  # Add to conversation (Bedrock converse API format)
196
  conversation_history.append({"role": "assistant", "content": [{"text": assistant_message}]})
197
 
198
+ # Parse for Action and Action Input FIRST (before checking Final Answer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  action_match = re.search(r'Action:\s*(\w+)', assistant_message)
200
+ action_input_match = re.search(r'Action Input:\s*(\{.+?\})', assistant_message, re.DOTALL)
201
 
202
  if action_match and action_input_match:
203
  tool_name = action_match.group(1)
204
+ action_input_str = action_input_match.group(1).strip()
205
 
206
  try:
207
  # Parse JSON input
 
238
  observation_message = f"Observation: {observation}"
239
  conversation_history.append({"role": "user", "content": [{"text": observation_message}]})
240
 
241
+ # Continue to next iteration to get Mistral's next action
242
+ continue
243
+
244
  else:
245
  # Unknown tool
246
  error_msg = f"Unknown tool: {tool_name}"
247
  conversation_history.append({"role": "user", "content": [{"text": f"Error: {error_msg}"}]})
248
+ continue
249
 
250
  except json.JSONDecodeError as e:
251
  # Invalid JSON
252
  error_msg = f"Invalid JSON in Action Input: {e}"
253
  conversation_history.append({"role": "user", "content": [{"text": f"Error: {error_msg}"}]})
254
+ continue
255
+
256
+ # Check for Final Answer (only if no action was found)
257
+ if "Final Answer:" in assistant_message or "final answer" in assistant_message.lower():
258
+ # Done!
259
+ if tool_results:
260
+ structured_result = {
261
+ "status": "completed",
262
+ "components_executed": tool_results,
263
+ "summary": {
264
+ "total_tools_called": len(tool_results),
265
+ "tools": list(tool_results.keys())
266
+ },
267
+ "final_output": assistant_message
268
+ }
269
+
270
+ yield {
271
+ "type": "final",
272
+ "data": structured_result,
273
+ "executor": "bedrock"
274
+ }
275
+ else:
276
  yield {
277
  "type": "error",
278
+ "error": "Bedrock completed but no tools were called",
279
+ "executor": "bedrock"
 
280
  }
281
+ return
282
+
283
+ # No action found - agent might be confused or done
284
+ if iteration > 0 and not has_called_tools:
285
+ # Agent isn't calling tools properly
286
+ yield {
287
+ "type": "error",
288
+ "error": "Bedrock didn't call tools in correct format. Falling back to CrewAI.",
289
+ "executor": "bedrock",
290
+ "debug_output": assistant_message[:500]
291
+ }
292
+ return
293
+ elif iteration > 0:
294
+ # Has called some tools but stopped - might be done
295
  structured_result = {
296
  "status": "completed",
297
  "components_executed": tool_results,