arXiv:2411.15501cs.SEcs.AI2024-11中稿 · ICSE 2025被引 19

通过交互式提示提升大模型代码片段适配能力

Instruct or Interact? Exploring and Eliciting LLMs' Capability in Code Snippet Adaptation Through Prompt Engineering

  • 设计交互式提示策略,引导大模型更准确理解代码上下文
  • 使代码适配成功率提升超40%,解决159个缺陷
  • 适合需高效重用和修改代码的开发者与自动化工具

代码片段适配是软件开发中的基础任务,不同于自由生成,需根据具体需求和上下文对已有代码进行调整。尽管大语言模型在代码生成上表现优异,但在依赖上下文的代码适配任务中表现较弱。我们对三种主流LLM进行了实证研究,发现其适配性能比生成任务低约15%(pass@1),且存在大量上下文相关错误。人工分析200个案例后,归纳出三类问题:需求不明确、需求错位、上下文误用。为此,我们提出一种交互式提示方法,实验表明该方法显著提升性能,人机协作模式成功修复159个缺陷,pass@1和pass@5提升超40%。为降低人力成本,建议采用多智能体交互,在性能与泛化间取得平衡。本研究为自主代码重用与适配提供了方法支持。

原文摘要 · Abstract (English)

Code snippet adaptation is a fundamental activity in the software development process. Unlike code generation, code snippet adaptation is not a "free creation", which requires developers to tailor a given code snippet in order to fit specific requirements and the code context. Recently, large language models (LLMs) have confirmed their effectiveness in the code generation task with promising results. However, their performance on adaptation, a reuse-oriented and context-dependent code change prediction task, is still unclear. To bridge this gap, we conduct an empirical study to investigate the performance and issues of LLMs on the adaptation task. We first evaluate the adaptation performances of three popular LLMs and compare them to the code generation task. Our result indicates that their adaptation ability is weaker than generation, with a nearly 15% decrease on pass@1 and more context-related errors. By manually inspecting 200 cases, we further investigate the causes of LLMs' sub-optimal performance, which can be classified into three categories, i.e., Unclear Requirement, Requirement Misalignment and Context Misapplication. Based on the above empirical research, we propose an interactive prompting approach to eliciting LLMs' adaptation ability. Experimental result reveals that our approach greatly improve LLMs' adaptation performance. The best-performing Human-LLM interaction successfully solves 159 out of the 202 identified defects and improves the pass@1 and pass@5 by over 40% compared to the initial instruction-based prompt. Considering human efforts, we suggest multi-agent interaction as a trade-off, which can achieve comparable performance with excellent generalization ability. We deem that our approach could provide methodological assistance for autonomous code snippet reuse and adaptation with LLMs.

代码生成大模型提示工程编程助手

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