arXiv:2503.09020cs.SEcs.AI2025-03被引 2

让大模型生成代码更符合编码规范,提升可维护性。

Enhancing the Non-Functional Quality Compliance of LLM-Generated Code through Quality-Aware Preference Learning

  • 用成对的差/优代码训练模型,引导其生成高质量代码。
  • 在Qwen2.5-Coder上使质量得分提升75.7%,且保持功能正确。
  • 适合关注代码可读性与工程规范的开发者和团队使用。

大型语言模型(LLMs)已被广泛用于商业代码补全系统,显著提升了编码效率。然而,即使功能正确的代码也可能存在风格差、可维护性低等非功能性质量问题。为此,我们提出一种质量感知偏好学习框架,指导LLM生成符合标准的代码。该方法包含三个阶段:首先构建成对的质量违规与合规代码数据集;其次设计自适应标记加权机制,突出质量敏感区域;最后引入混合优化目标,结合排序损失、语言建模损失与KL散度,实现有效对比优化。在DeepSeek-Coder和Qwen2.5-Coder上的实验表明,该方法显著提升目标非功能性质量标准的合规性,同时保持功能正确,在MBPP-sanitized数据集上使质量互评分数(QRS)相对提升75.7%。微调7B模型耗时不足三小时,具备良好实用性。消融实验与用户研究进一步验证了框架的有效性。

原文摘要 · Abstract (English)

Large Language Models (LLMs) have been widely adopted in commercial code completion engines, significantly enhancing coding efficiency and productivity. However, even functionally correct LLM-generated code may exhibit non-functional quality issues that violate coding standards and best practices, such as poor style and limited maintainability. To address this, we propose a framework for quality-aware preference learning that guides LLMs toward generating criteria-compliant code. Our approach consists of three phases. First, we construct a dataset of paired criteria-violating and criteria-compliant samples, where each pair contains code exhibiting a specific non-functional quality issue and its repaired version that resolves the issue. Second, we design an adaptive token weighting mechanism to emphasize quality-sensitive code regions. Third, we introduce a hybrid optimization objective that combines ranking loss with language modeling loss and KL divergence to enable effective comparative optimization. Extensive experiments on DeepSeek-Coder and Qwen2.5-Coder show that our method substantially improves compliance with the targeted non-functional quality criteria while maintaining functional correctness, achieving a 75.7% relative increase in Quality Reciprocal Score (QRS) on MBPP-sanitized for Qwen2.5-Coder. Fine-tuning a 7B model requires less than three hours, indicating strong practical viability. Ablation studies and a user study further support the effectiveness of the proposed framework.

代码生成质量评估大模型优化

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