Update src/promptings/DebateCoder.py
Browse files15 unmodified lines1617181919202134 unmodified lines565758605960616238 unmodified lines10110210310510410510617 unmodified lines124125126129127128129130302 unmodified lines43343443543843643743843911 unmodified lines45145245345645445545645717 unmodified lines47547647748048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152247847948048152748248348448514 unmodified lines5005015025485035045055065075085095545105115125132 unmodified lines51651751856356451952052152243 unmodified lines56656756856957057157257357415 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 lines def 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") 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 lines print(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) + 1 302 unmodified lines return code_str
def Reviewer_pangu1b(self,problem_prompt: str, code: str, test_log: str, task_id: str = "unknown") -> Tuple[str, int, int]: def Reviewer_pangu1b(self,problem_prompt: str, plan: str, code: str, test_log: str, task_id: str = "unknown") -> Tuple[str, int, int]: """ Reviewer 角色:分析代码失败原因并提供修复计划。 11 unmodified lines reviewer_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." "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 lines return "Code failed sample tests. Please check logic and edge cases.", 0, 0
def 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消耗) """ # 构造 Prompt reviewer_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_tok
except NameError: print("Error: Qwen model class not found. Skipping detailed analysis.") return "Code failed sample tests. Please check logic and edge cases.", 0, 0 except Exception as e: print(f"Error during Reviewer analysis: {e}") return "Code failed sample tests. Please check logic and edge cases.", 0, 0
def debugging(self, plan: list, code: str, item: dict, algorithm_prompt: str) -> str: passed = False planning, _, _ = plan plan_code_response = f"## Planning: {planning}\n## Code:\n```\n{code}\n```" # 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 lines problem_prompt = self.data.get_prompt(item)
# 调用 Reviewer analysis, pr_cost, com_cost = self.Reviewer_qwen32b( analysis, 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_cost self.com_tok += com_cost2 unmodified lines input_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." } "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 lines
def run_single_pass(self, item: dict): """执行单个问题的多智能体辩论流程""" self.pr_tok = 0 self.com_tok = 0 print("\n" + "="*80) print(f"Processing: {item.get('task_id', item.get('name', 'unknown'))}") print("="*80)src/promptings/DebateCoder.py+12-54