Skip to main content
QUICK REVIEW

[论文解读] What Makes Good In-context Demonstrations for Code Intelligence Tasks with LLMs?

Shuzheng Gao, Xin-Cheng Wen|arXiv (Cornell University)|Apr 15, 2023
Software Engineering Research参考文献 57被引用 5
一句话总结

本文研究了如何使用大型语言模型(LLMs)为代码智能任务设计有效的上下文演示。通过系统评估演示选择、排序以及示例数量在三个任务(代码摘要、错误修复和程序合成)中的影响,发现优化后的演示可使标准方法的F1分数提升高达175.96%。

ABSTRACT

Pre-trained models of source code have gained widespread popularity in many code intelligence tasks. Recently, with the scaling of the model and corpus size, large language models have shown the ability of in-context learning (ICL). ICL employs task instructions and a few examples as demonstrations, and then inputs the demonstrations to the language models for making predictions. This new learning paradigm is training-free and has shown impressive performance in various natural language processing and code intelligence tasks. However, the performance of ICL heavily relies on the quality of demonstrations, e.g., the selected examples. It is important to systematically investigate how to construct a good demonstration for code-related tasks. In this paper, we empirically explore the impact of three key factors on the performance of ICL in code intelligence tasks: the selection, order, and number of demonstration examples. We conduct extensive experiments on three code intelligence tasks including code summarization, bug fixing, and program synthesis. Our experimental results demonstrate that all the above three factors dramatically impact the performance of ICL in code intelligence tasks. Additionally, we summarize our findings and provide takeaway suggestions on how to construct effective demonstrations, taking into account these three perspectives. We also show that a carefully-designed demonstration based on our findings can lead to substantial improvements over widely-used demonstration construction methods, e.g., improving BLEU-4, EM, and EM by at least 9.90%, 175.96%, and 50.81% on code summarization, bug fixing, and program synthesis, respectively

研究动机与目标

  • 为填补当前对如何为大型语言模型的代码智能任务构建高质量上下文演示(ICL)的理解空白。
  • 探究演示选择、排序和示例数量对代码相关任务中ICL性能的影响。
  • 为研究人员和从业者提供可操作的、基于证据的指导,以提升代码摘要、错误修复和程序合成中ICL的有效性。
  • 证明优化后的演示显著优于随机或启发式基线方法,尤其在低资源设置下表现更优。
  • 在多个LLM和代码数据集上验证研究发现的泛化能力,同时识别当前评估范围的局限性。

提出的方法

  • 在三个代码智能任务(代码摘要、错误修复和程序合成)上,使用三种LLM进行了广泛的实证实验。
  • 评估了多种演示选择策略,包括随机选择、基于相似度的检索(使用BM-25和语义嵌入)以及基于多样性的选择。
  • 比较了不同的排序策略:随机排序、按相似度降序排列(相似度优先)以及按相似度升序排列(反向相似度排序)。
  • 调整演示示例的数量(从1到10个)以评估其对各类任务中模型性能的影响。
  • 使用BLEU-4、精确匹配(EM)和F1等标准指标评估性能,并进行显著性检验。
  • 基于实证发现,提出了一套系统化的演示设计框架,整合了选择、排序和示例数量的优化。
Figure 1 : An example of in-context learning on code summarization task.
Figure 1 : An example of in-context learning on code summarization task.

实验结果

研究问题

  • RQ1在代码智能任务中,哪种演示选择方法最为有效?
  • RQ2演示示例的排序方式如何影响代码相关任务中的ICL性能?
  • RQ3演示示例的数量在多大程度上影响ICL在代码智能任务中的性能?
  • RQ4研究发现的泛化能力在不同LLM和编程语言之间如何?

主要发现

  • 使用语义嵌入(如SBERT)的基于相似度的选择方法,在演示选择方面显著优于随机选择或基于BM-25的检索。
  • 按与输入查询的相似度降序排列演示示例可提升性能,而反向相似度排序(升序)通常表现较差。
  • 最优的演示示例数量因任务而异,一般在5–8个示例时达到性能峰值,尽管在6–8个示例后性能趋于平稳。
  • 通过精心设计的演示策略——结合最优选择、基于相似度的排序以及合适的示例数量——在错误修复任务中EM指标提升175.96%,在程序合成任务中提升50.81%,在代码摘要任务中提升9.90%,均优于基线方法。
  • 优化演示带来的性能增益在多个LLM上保持一致,表明研究发现具有较强的泛化能力。
  • 研究揭示ICL性能对演示设计极为敏感,即使使用大模型,若采用简单选择或随机排序,仍会导致次优结果。
Figure 2 : Illustration of design space of in-context demonstrations.
Figure 2 : Illustration of design space of in-context demonstrations.

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。