Update src/promptings/DebateCoder.py
Browse files15 unmodified lines1617181920212234 unmodified lines5758596061626338 unmodified lines10210310410510610710817 unmodified lines126127128129130131132302 unmodified lines43543643743843944044111 unmodified lines45345445545645745845917 unmodified lines47747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953014 unmodified lines5455465475485495505515525535545555565572 unmodified lines56056156256356456556656743 unmodified lines61161261361461561615 unmodified linesexcept ImportError:print("Warning: Could not import Pangu model. Debugging might fail if Pangu is not defined.")from models.Qwen import Qwenfrom .Base import BaseStrategyfrom models.Base import BaseModelfrom results.Results import Results34 unmodified linesdef log_response(self, response: str, stage: str, item: dict):"""记录响应到日志文件"""log_file = os.path.join(self.log_dir, f"DebateCoder_{self.model.__class__.__name__}_responses.log")try:with open(log_file, "a", encoding="utf-8") as f:38 unmodified lines"""生成初始计划(第一轮)"""role_prompt = self.get_agent_role_prompt(role)problem_description = self.data.get_prompt(item)messages = [{"role": "system",17 unmodified linesprint(f"\n{'='*60}")print(f"[Round 1] {role} - Initial Planning")print(f"{'='*60}")response, pr_tok, com_tok = self.gpt_chat(messages)item['api_calls'] = item.get('api_calls', 0) + 1302 unmodified linesreturn code_strdef Reviewer_pangu1b(self,problem_prompt: str, code: str, test_log: str, task_id: str = "unknown") -> Tuple[str, int, int]:"""Reviewer 角色:分析代码失败原因并提供修复计划。11 unmodified linesreviewer_input = [{"role": "user","content": f"You are an expert programmer. The following code was generated to solve a problem but failed sample test cases.\n\n## Problem:\n{problem_prompt}\n\n## Generated Code:\n```\n{code}\n```\n\n## Test Report:\n{test_log}\n\nPlease analyze why the code failed and provide a specific plan to fix it. Do not generate the full code, just the analysis and fix plan."}]17 unmodified linesreturn "Code failed sample tests. Please check logic and edge cases.", 0, 0def Reviewer_qwen32b(self,problem_prompt: str, code: str, test_log: str, task_id: str = "unknown") -> Tuple[str, int, int]:"""Reviewer 角色:分析代码失败原因并提供修复计划。Args:problem_prompt (str): 题目描述。code (str):生成的代码。test_log (str): 测试失败的日志报告。task_id (str): 当前任务的ID,用于日志打印 (对应原代码中的 i)。Returns:Tuple[str, int, int]: 返回 (分析结果, prompt_token消耗, completion_token消耗)"""# 构造 Promptreviewer_input = [{"role": "user","content": f"You are an expert programmer. The following code was generated to solve a problem but failed sample test cases.\n\n## Problem:\n{problem_prompt}\n\n## Generated Code:\n```\n{code}\n```\n\n## Test Report:\n{test_log}\n\nPlease analyze why the code failed and provide a specific plan to fix it. Do not generate the full code, just the analysis and fix plan."}]print(f"Input for Reviewer analysis: {task_id}")try:qwen_model = Qwen()analysis, q_pr_tok, q_com_tok = qwen_model.prompt(reviewer_input)print(f"Reviewer Analysis: {analysis}", flush=True)return analysis, q_pr_tok, q_com_tokexcept NameError:print("Error: Qwen model class not found. Skipping detailed analysis.")return "Code failed sample tests. Please check logic and edge cases.", 0, 0except Exception as e:print(f"Error during Reviewer analysis: {e}")return "Code failed sample tests. Please check logic and edge cases.", 0, 0def debugging(self, plan: list, code: str, item: dict, algorithm_prompt: str) -> str:passed = Falseplanning, _, _ = planplan_code_response = f"## Planning: {planning}\n## Code:\n```\n{code}\n```"if type(self.data) == APPSDataset or type(self.data) == CodeContestDataset or type(self.data) == XCodeDataset:std_input_prompt = "## Note: Strictly follow the input and output format. The input should be taken from Standard input and output should be given to standard output. If you are writing a function then after the function definition take input using `input()` function then call the function with specified parameters and finally print the output of the function. Do not add extra print statement otherwise it will failed the test cases."14 unmodified linesproblem_prompt = self.data.get_prompt(item)# 调用 Revieweranalysis, pr_cost, com_cost = self.Reviewer_qwen32b(problem_prompt=problem_prompt,code=code,test_log=test_log,task_id=i)# 更新 Token 计数self.pr_tok += pr_costself.com_tok += com_cost2 unmodified linesinput_for_improving_code = [{"role": "user","content": f"Given a competitive programming problem you have generated {self.language} code to solve the problem. But the generated code can not pass sample test cases.\n\nHere is an analysis of the failure and a fix plan provided by an expert:\n{analysis}\n\nImprove your code to solve the problem correctly based on this analysis.\n{algorithm_prompt}\n## Problem to be solved:\n{self.data.get_prompt(item)}\n{plan_code_response}\n## Test Report:\n{test_log}\n## Modified Planning:\n## Let's think step by step to modify {self.language} Code for solving this problem.\n\n----------------\nImportant:\n{std_input_prompt}\n## Your response must contain the modified planning and then the {self.language} code inside ``` block to solve this problem."}]print("\n\n________________________")43 unmodified linesdef run_single_pass(self, item: dict):"""执行单个问题的多智能体辩论流程"""print("\n" + "="*80)print(f"Processing: {item.get('task_id', item.get('name', 'unknown'))}")print("="*80)15 unmodified lines16171819202134 unmodified lines5657585960616238 unmodified lines10110210310410510617 unmodified lines124125126127128129130302 unmodified lines43343443543643743843911 unmodified lines45145245345445545645717 unmodified lines47547647747847948048148248348448514 unmodified lines5005015025035045055065075085095105115125132 unmodified lines51651751851952052152243 unmodified lines56656756856957057157257357415 unmodified linesexcept ImportError:print("Warning: Could not import Pangu model. Debugging might fail if Pangu is not defined.")from .Base import BaseStrategyfrom models.Base import BaseModelfrom results.Results import Results34 unmodified linesdef log_response(self, response: str, stage: str, item: dict):"""记录响应到日志文件"""log_file = os.path.join(self.log_dir, f"MultiAgentDebate_{self.model.__class__.__name__}_responses.log")try:with open(log_file, "a", encoding="utf-8") as f:38 unmodified lines"""生成初始计划(第一轮)"""role_prompt = self.get_agent_role_prompt(role)problem_description = self.data.get_prompt(item)messages = [{"role": "system",17 unmodified linesprint(f"\n{'='*60}")print(f"[Round 1] {role} - Initial Planning")print(f"{'='*60}")print("messages:", messages)response, pr_tok, com_tok = self.gpt_chat(messages)item['api_calls'] = item.get('api_calls', 0) + 1302 unmodified linesreturn code_strdef Reviewer_pangu1b(self,problem_prompt: str, plan: str, code: str, test_log: str, task_id: str = "unknown") -> Tuple[str, int, int]:"""Reviewer 角色:分析代码失败原因并提供修复计划。11 unmodified linesreviewer_input = [{"role": "user","content": f"You are an expert programmer. The following code was generated to solve a problem but failed sample test cases.\n\n## Problem:\n{problem_prompt}\n\n## Plan:\n{plan}\n \n## Generated Code:\n```\n{code}\n```\n\n## Test Report:\n{test_log}\n\nPlease analyze why the code failed and provide a specific plan to fix it. Do not generate the full code, just the analysis and fix plan."}]17 unmodified linesreturn "Code failed sample tests. Please check logic and edge cases.", 0, 0def debugging(self, plan: list, code: str, item: dict, algorithm_prompt: str) -> str:passed = Falseplanning, _, _ = plan# plan_code_response = f"## Planning: {planning}\n## Code:\n```\n{code}\n```"if type(self.data) == APPSDataset or type(self.data) == CodeContestDataset or type(self.data) == XCodeDataset:std_input_prompt = "## Note: Strictly follow the input and output format. The input should be taken from Standard input and output should be given to standard output. If you are writing a function then after the function definition take input using `input()` function then call the function with specified parameters and finally print the output of the function. Do not add extra print statement otherwise it will failed the test cases."14 unmodified linesproblem_prompt = self.data.get_prompt(item)# 调用 Revieweranalysis, pr_cost, com_cost = self.Reviewer_pangu1b(problem_prompt=problem_prompt,plan = planning,code=code,test_log=test_log,task_id=i)print(f"pr_cost: {pr_cost}, com_cost: {com_cost}")# 更新 Token 计数self.pr_tok += pr_costself.com_tok += com_cost2 unmodified linesinput_for_improving_code = [{"role": "user","content": f" You are an expert competitive programmer. Your task is to fix the provided {self.language} code based on the Expert Analysis. The original code failed sample test cases.\n\n### Problem Description: \n{self.data.get_prompt(item)}\n ### Original Code (Buggy): \n```\n{code}\n```\n### Expert Analysis & Fix Plan:\n{analysis}\n\nImprove your code to solve the problem correctly based on this analysis.\n### Requirement: 1. Read the Expert Analysis carefully. 2. {std_input_prompt} 3. Generate the corrected code.\n\n----------------\nImportant:\n{std_input_prompt}\n## Your response must contain the modified planning and then the {self.language} code inside ``` block to solve this problem." }]print("\n\n________________________")43 unmodified linesdef run_single_pass(self, item: dict):"""执行单个问题的多智能体辩论流程"""self.pr_tok = 0self.com_tok = 0print("\n" + "="*80)print(f"Processing: {item.get('task_id', item.get('name', 'unknown'))}")print("="*80)
src/promptings/DebateCoder.py+12-54