一句话:通过实证研究揭示 LLM 代码片段适配弱于生成的原因,并提出交互式提示方法显著提升适配性能。
代码片段适配是复用导向且依赖上下文的代码修改预测任务,但 LLM 在此任务上的表现尚不明确。本文首先对三个主流 LLM 进行实证评估,发现其适配能力弱于代码生成,pass@1 下降近 15%,且更多出现上下文相关错误。通过人工检查 200 个案例,将失败原因归纳为需求不清晰、需求错位和上下文误用三类。基于此,提出交互式提示方法:通过丰富上下文与任务分解缓解上下文误用并改善需求理解,借助与人类或 LLM 顾问的反思交互补偿需求不清晰。实验表明,最佳 Human-LLM 交互修复了 202 个缺陷中的 159 个,pass@1 和 pass@5 相比初始指令提示提升超过 40%;考虑人力成本,多智能体交互是性能与泛化性折中的方案。
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 code snippet 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' ability on the adaptation task. Specifically, we enhance the prompt by enriching the context and decomposing the task, which alleviates context misapplication and improves requirement understanding. Besides, we enable LLMs' reflection by requiring them to interact with a human or a LLM counselor, compensating for unclear requirement. Our 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.
DOI 原文 ·
@inproceedings{paperbot8759,
title = {Instruct or Interact? Exploring and Eliciting LLMs' Capability in Code Snippet Adaptation Through Prompt Engineering},
author = {Tanghaoran Zhang and Yue Yu and Xinjun Mao and Shangwen Wang and Kang Yang and Yao Lu and Zhang Zhang and Yuxin Zhao},
booktitle = {ICSE},
year = {2025},
doi = {10.1109/icse55347.2025.00104}
}