diff --git a/oscopilot/agents/friday_agent.py b/oscopilot/agents/friday_agent.py index 899b501..c612fd5 100644 --- a/oscopilot/agents/friday_agent.py +++ b/oscopilot/agents/friday_agent.py @@ -139,8 +139,19 @@ class FridayAgent(BaseAgent): retrieve_tool_description_pair = self.retriever.retrieve_tool_description_pair(retrieve_tool_name) # decompose task - self.planner.decompose_task(task, retrieve_tool_description_pair) - + # Set up the generation format error handling mechanism + max_retries = 3 + attempts = 0 + while attempts < max_retries: + try: + self.planner.decompose_task(task, retrieve_tool_description_pair) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in planning on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, planning failed.") + return self.planner.sub_task_list def executing(self, tool_name, original_task): @@ -180,11 +191,22 @@ class FridayAgent(BaseAgent): logging.info(result) else: invoke = '' - if node_type == 'API': - api_path = self.executor.extract_API_Path(description) - code = self.executor.api_tool(description, api_path, pre_tasks_info) - else: - code, invoke = self.executor.generate_tool(tool_name, description, node_type, pre_tasks_info, relevant_code) + # Set up the generation format error handling mechanism + max_retries = 3 + attempts = 0 + while attempts < max_retries: + try: + if node_type == 'API': + api_path = self.executor.extract_API_Path(description) + code = self.executor.api_tool(description, api_path, pre_tasks_info) + else: + code, invoke = self.executor.generate_tool(tool_name, description, node_type, pre_tasks_info, relevant_code) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in generating tool code on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, Failed to execute subtask.") # Execute python tool class code state = self.executor.execute_tool(code, invoke, node_type) result = state.result @@ -217,7 +239,18 @@ class FridayAgent(BaseAgent): next_action = tool_node.next_action critique = '' score = 0 - critique, status, score = self.executor.judge_tool(code, description, state, next_action) + # Set up the generation format error handling mechanism + max_retries = 3 + attempts = 0 + while attempts < max_retries: + try: + critique, status, score = self.executor.judge_tool(code, description, state, next_action) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in judging on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, judging failed.") return JudgementResult(status, critique, score) def replanning(self, tool_name, reasoning): @@ -235,7 +268,18 @@ class FridayAgent(BaseAgent): """ relevant_tool_name = self.retriever.retrieve_tool_name(reasoning) relevant_tool_description_pair = self.retriever.retrieve_tool_description_pair(relevant_tool_name) - self.planner.replan_task(reasoning, tool_name, relevant_tool_description_pair) + # Set up the generation format error handling mechanism + max_retries = 3 + attempts = 0 + while attempts < max_retries: + try: + self.planner.replan_task(reasoning, tool_name, relevant_tool_description_pair) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in replanning on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, replanning failed.") return self.planner.sub_task_list def repairing(self, tool_name, code, description, state, critique, status): @@ -263,7 +307,18 @@ class FridayAgent(BaseAgent): while (trial_times < self.executor.max_iter and status == 'Amend'): trial_times += 1 print("current amend times: {}".format(trial_times)) - new_code, invoke = self.executor.repair_tool(code, description, tool_node.node_type, state, critique, pre_tasks_info) + # Set up the generation format error handling mechanism + max_retries = 3 + attempts = 0 + while attempts < max_retries: + try: + new_code, invoke = self.executor.repair_tool(code, description, tool_node.node_type, state, critique, pre_tasks_info) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in generating tool code on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, Failed to generate tool.") critique = '' code = new_code # Run the current code and check for errors @@ -271,7 +326,17 @@ class FridayAgent(BaseAgent): result = state.result logging.info(state) if state.error == None: - critique, status, score = self.executor.judge_tool(code, description, state, next_action) + # Set up the generation format error handling mechanism + attempts = 0 + while attempts < max_retries: + try: + critique, status, score = self.executor.judge_tool(code, description, state, next_action) + break + except Exception as e: + attempts += 1 + logging.error(f"Error in judging on attempt {attempts}: {str(e)}") + if attempts == max_retries: + logging.error("Max retries reached, judging failed.") # The task execution is completed and the loop exits if status == 'Complete': break