Automated code completion, aiming at generating subsequent tokens from unfinished code, has been significantly benefited from recent progress in pre-trained Large Language Models (LLMs). However, these models often suffer from coherence issues and hallucinations when dealing with complex code logic or extrapolating beyond their training data. Existing Retrieval Augmented Generation (RAG) techniques partially address these issues by retrieving relevant code with a separate encoding model where the retrieved snippet serves as contextual reference for code completion. However, their retrieval scope is subject to a singular perspective defined by the encoding model, which largely overlooks the complexity and diversity inherent in code semantics. To address this limitation, we propose ProCC, a code completion framework leveraging prompt engineering and the contextual multi-armed bandits algorithm to flexibly incorporate and adapt to multiple perspectives of code. ProCC first employs a prompt-based multi-retriever system which crafts prompt templates to elicit LLM knowledge to understand code semantics with multiple retrieval perspectives. Then, it adopts the adaptive retrieval selection algorithm to incorporate code similarity into the decision-making process to determine the most suitable retrieval perspective for the LLM to complete the code. Experimental results demonstrate that ProCC outperforms state-of-the-art code completion technique by 8.6% on our collected open-source benchmark suite and 10.1% on the private-domain benchmark suite collected from a billion-user e-commerce company in terms of Exact Match. ProCC also allows augmenting fine-tuned techniques in a plug-and-play manner, yielding 5.6% improvement over our studied fine-tuned model.
翻译:自动代码补全旨在从未完成的代码中生成后续令牌,近年来预训练大型语言模型(LLMs)的进步显著推动了该领域的发展。然而,这些模型在处理复杂代码逻辑或推断超出训练数据范围时,常出现连贯性问题和幻觉现象。现有检索增强生成(RAG)技术通过使用独立编码模型检索相关代码,并将检索片段作为上下文参考以辅助代码补全,部分缓解了上述问题。但这类技术受限于编码模型定义的单一视角,很大程度上忽视了代码语义的复杂性和多样性。为解决这一局限,我们提出ProCC框架——一种结合提示工程与上下文多臂老虎机算法的代码补全框架,能够灵活整合并适应代码的多种视角。ProCC首先构建基于提示的多检索系统,通过设计提示模板激发LLM对代码语义的认知,实现多检索视角理解;随后采用自适应检索选择算法,将代码相似度融入决策过程,为LLM确定完成代码的最优检索视角。实验结果表明,在收集的开源基准测试套件上,ProCC在精确匹配指标上超出当前最先进代码补全技术8.6%,在从某十亿用户电商企业收集的私有领域基准测试套件上超出10.1%。此外,ProCC支持以即插即用方式增强微调技术,相较于所研究的微调模型提升5.6%性能。