Enhancing Large Language Models in Coding Through Multi-Perspective Self-Consistency

Baizhou Huang,Shuai Lu,Weizhu Chen,Xiaojun Wan,Nan Duan
2024-07-02
Abstract:Large language models (LLMs) have exhibited remarkable ability in code generation. However, generating the correct solution in a single attempt still remains a challenge. Prior works utilize verification properties in software engineering to verify and re-rank solutions in a majority voting manner. But the assumption behind them that generated verification properties have better qualities than solutions may not always hold. In this paper, we treat them equally as different perspectives of LLMs' reasoning processes. We propose the Multi-Perspective Self-Consistency (MPSC) framework incorporating both inter- and intra-consistency across outputs from multiple perspectives. Specifically, we prompt LLMs to generate diverse outputs from three perspectives, Solution, Specification and Test case, constructing a 3-partite graph. With two measure functions of consistency, we embed both inter- and intra-consistency information into the graph. The optimal choice of solutions is then determined based on analysis in the graph. MPSC significantly boosts performance of foundation models (ChatGPT in this paper) on various benchmarks, including HumanEval (+15.91%), MBPP (+6.43%) and CodeContests (+9.37%), even surpassing GPT-4.
Computation and Language,Artificial Intelligence,Software Engineering
What problem does this paper attempt to address?
The problem this paper attempts to address is: Although large language models (LLMs) perform well in code generation tasks, generating the correct solution in a single attempt remains challenging. Existing methods introduce verification attributes from software engineering to validate and reorder generated solutions, but these methods assume that the generated verification attributes are of higher quality than the generated solutions, which is not always the case. To overcome this issue, the paper proposes the Multi-Perspective Self-Consistency (MPSC) framework. This framework treats solutions, specifications, and test cases as outputs from different perspectives and integrates internal consistency and cross-perspective consistency information by constructing a 3-partite graph. Ultimately, the optimal solution is selected based on the analysis of the graph. Specifically, the main contributions of the MPSC framework include: 1. **Multi-Perspective Generation**: Generating diverse outputs from three different perspectives (solutions, specifications, and test cases). 2. **Consistency Measurement**: Evaluating internal consistency and cross-perspective consistency through two metric functions. 3. **Graph Construction and Optimization**: Representing the generated outputs as vertices of a graph and connecting vertices from different perspectives with edges to construct a 3-partite graph. Then, selecting the most consistent solution by optimizing an objective function. Experimental results show that the MPSC framework significantly improves the performance of base models (such as ChatGPT) on multiple code generation benchmarks, even surpassing GPT-4.