Optimizes the SOP for the given list of cases. Args: case_list (list[Case]): List of cases to be optimized. solution (Solution): The solution to be optimized. save_path (Path): Path to save the results. parallel_max_num (int): Maximum
(
self,
case_list: list[Case],
solution: Solution,
save_path: Path,
parallel_max_num)
| 60 | self.logger = logging.getLogger(logger_name) if logger_name else logging.getLogger(__name__) |
| 61 | |
| 62 | def optimize( |
| 63 | self, |
| 64 | case_list: list[Case], |
| 65 | solution: Solution, |
| 66 | save_path: Path, |
| 67 | parallel_max_num): |
| 68 | """ |
| 69 | Optimizes the SOP for the given list of cases. |
| 70 | |
| 71 | Args: |
| 72 | case_list (list[Case]): List of cases to be optimized. |
| 73 | solution (Solution): The solution to be optimized. |
| 74 | save_path (Path): Path to save the results. |
| 75 | parallel_max_num (int): Maximum number of parallel processes. |
| 76 | |
| 77 | Returns: |
| 78 | tuple: The updated solution and optimization status. |
| 79 | """ |
| 80 | self.logger.info("Start to optimize SOP") |
| 81 | |
| 82 | # 1. Perform backward pass for each case to get the necessary information |
| 83 | for case in case_list: |
| 84 | OptimUtils.node_eval(case, solution, self.llm_eval, self.logger) |
| 85 | self.backward(case, solution, save_path / "backward") |
| 86 | |
| 87 | # 2. Construct the prompt and get the optimization method |
| 88 | prompt = prompt_formatter.formulate_prompt_for_sop_optim(self.meta_optim, solution.sop, case_list) |
| 89 | _, content = self.llm_eval.get_response(chat_messages=None, system_prompt="", |
| 90 | last_prompt=prompt, stream=False) |
| 91 | |
| 92 | # print("in optimize sop, prompt is: ", prompt) |
| 93 | # print("in optimize sop, response is : ", content) |
| 94 | |
| 95 | # 3. Extract results and attempt to optimize the SOP |
| 96 | extracted_dict = OptimUtils.extract_data_from_response(content, self.meta_optim["extract_key"]) |
| 97 | result = extracted_dict["result"] |
| 98 | analyse = extracted_dict["analyse"] |
| 99 | solution, op_status = SOPOptimizer.try_optim_with_llm_result(solution, result, self.logger) |
| 100 | |
| 101 | # Default optimized solution: controller's transit_type is llm, transit_system_prompt and transit_last_prompt are empty |
| 102 | if op_status: |
| 103 | for node in solution.sop.nodes.values(): |
| 104 | node.controller.update({"transit_type": "llm", "transit_system_prompt": "", "transit_last_prompt": ""}) |
| 105 | |
| 106 | # 4. Save the final solution and optimization information |
| 107 | optim_info = { |
| 108 | "optim_status": op_status, |
| 109 | "result": result, |
| 110 | "analyse": analyse, |
| 111 | "prompt": prompt, |
| 112 | "response": content, |
| 113 | } |
| 114 | with open(save_path / "sop_optim_info.json", "w", encoding="utf-8") as f: |
| 115 | json.dump(optim_info, f, ensure_ascii=False, indent=4) |
| 116 | |
| 117 | # Reload the saved solution to avoid mismatches between config and actual data |
| 118 | solution.dump(save_path) |
| 119 | solution = Solution(config=SolutionConfig(f"{save_path}/solution.json")) |
no test coverage detected