arXiv:2505.23953cs.SEcs.AI2025-05中稿 · COMPSAC 2025被引 15

用代码复杂度反馈提升大模型生成代码的准确率

Enhancing LLM-Based Code Generation with Complexity Metrics: A Feedback-Driven Approach

  • 通过复杂度指标识别错误代码,引导模型迭代优化
  • 在HumanEval上使小模型准确率提升35.71%(基线仅12.5%)
  • 适合做代码生成优化或开发智能编程助手的研究者

大型语言模型(如GPT-4)推动了自动代码生成的发展。尽管多数研究聚焦于提升模型生成效果,却很少关注生成代码的特性及其对失败案例的改进作用。本文以代码复杂度为最直接特征,探究其与大模型代码生成成功率(即Pass@1)的关系。基于大规模标准复杂度指标,我们进行实证分析,并通过逻辑回归识别出最具预测性的复杂度指标。在此基础上,提出一种迭代反馈方法:根据前次失败输出的复杂度信息,提示模型生成更正确的代码。我们在HumanEval、MBPP、LeetCode和BigCodeBench等多个基准上验证该方法,涵盖GPT-4o、GPT-3.5 Turbo、Llama 3.1和GPT-o3 mini等多种模型。结果表明,本方法显著提升性能,尤其在较小模型(GPT-3.5 Turbo)上,HumanEval的Pass@1提升达35.71%,远超基线(12.5%)。扩展实验显示,在BigCodeBench上,使用该方法的GPT-4o和GPT-o3 mini分别实现20%和23.07%的性能提升。结果表明,复杂度感知反馈可有效增强直接提示与代理式工作流。

原文摘要 · Abstract (English)

Automatic code generation has gained significant momentum with the advent of Large Language Models (LLMs) such as GPT-4. Although many studies focus on improving the effectiveness of LLMs for code generation, very limited work tries to understand the generated code's characteristics and leverage that to improve failed cases. In this paper, as the most straightforward characteristic of code, we investigate the relationship between code complexity and the success of LLM generated code. Using a large set of standard complexity metrics, we first conduct an empirical analysis to explore their correlation with LLM's performance on code generation (i.e., Pass@1). Using logistic regression models, we identify which complexity metrics are most predictive of code correctness. Building on these findings, we propose an iterative feedback method, where LLMs are prompted to generate correct code based on complexity metrics from previous failed outputs. We validate our approach across multiple benchmarks (i.e., HumanEval, MBPP, LeetCode, and BigCodeBench) and various LLMs (i.e., GPT-4o, GPT-3.5 Turbo, Llama 3.1, and GPT-o3 mini), comparing the results with two baseline methods: (a) zero-shot generation, and (b) iterative execution-based feedback without our code complexity insights. Experiment results show that our approach makes notable improvements, particularly with a smaller LLM (GPT3.5 Turbo), where, e.g., Pass@1 increased by 35.71% compared to the baseline's improvement of 12.5% on the HumanEval dataset. The study expands experiments to BigCodeBench and integrates the method with the Reflexion code generation agent, leading to Pass@1 improvements of 20% (GPT-4o) and 23.07% (GPT-o3 mini). The results highlight that complexity-aware feedback enhances both direct LLM prompting and agent-based workflows.

代码生成复杂度分析反馈机制大模型优化

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。