Tree search methods have demonstrated impressive performance in code generation. Previous methods combine tree search with reflection that summarizes past mistakes to achieve iterative improvement. However, these methods face significant challenges. First, they search directly within the code language space, neglecting the underlying reasoning process critical for effective code generation. Second, reflection-based approaches merely accumulate historical errors in memory without providing correct reasoning pathways, making it difficult for subsequent search iterations to identify optimal solutions, resulting in decreased search quality. In this work, we propose RethinkMCTS, a framework that systematically explores and refines the reasoning process for code generation. Specifically, we employ MCTS to search for thoughts before code generation and integrate MCTS with a refinement mechanism called rethink, which incorporates fine-grained code execution feedback to refine erroneous thoughts during the search. It ensures the search path aligns with better reasoning, improving overall search quality. Through extensive experiments, we demonstrate that RethinkMCTS outperforms previous search-based and feedback-enhanced code generation baselines.
翻译:树搜索方法在代码生成任务中已展现出令人印象深刻的性能。先前的方法将树搜索与总结过往错误的反思机制相结合,以实现迭代改进。然而,这些方法面临着显著挑战。首先,它们直接在代码语言空间中进行搜索,忽视了对于有效代码生成至关重要的底层推理过程。其次,基于反思的方法仅仅是在记忆中累积历史错误,并未提供正确的推理路径,这使得后续搜索迭代难以识别最优解,导致搜索质量下降。在本工作中,我们提出了RethinkMCTS,一个系统性地探索并精细化代码生成推理过程的框架。具体而言,我们采用MCTS在代码生成前搜索思维,并将MCTS与一种称为“再思考”的精细化机制相结合。该机制整合了细粒度的代码执行反馈,以在搜索过程中精细化错误思维。这确保了搜索路径与更优的推理过程保持一致,从而提升了整体搜索质量。通过大量实验,我们证明RethinkMCTS优于先前基于搜索的以及反馈增强的代码生成基线方法。