Skip to main content
QUICK REVIEW

[论文解读] GraphCoder: Enhancing Repository-Level Code Completion via Code Context Graph-based Retrieval and Language Model

Wei Liu, Ailun Yu|arXiv (Cornell University)|Jun 11, 2024
Software Engineering ResearchComputer Science被引用 3
一句话总结

GraphCoder 通过使用代码上下文图(CCG)对控制流和数据/控制依赖关系进行建模,提升了仓库级代码补全的准确性,实现了比基线方法更高的代码精确匹配(+6.06)和标识符精确匹配(+6.23),同时通过基于图的粗到精检索流程与预训练语言模型结合,显著降低了检索时间和空间开销。

ABSTRACT

The performance of repository-level code completion depends upon the effective leverage of both general and repository-specific knowledge. Despite the impressive capability of code LLMs in general code completion tasks, they often exhibit less satisfactory performance on repository-level completion due to the lack of repository-specific knowledge in these LLMs. To address this problem, we propose GraphCoder, a retrieval-augmented code completion framework that leverages LLMs' general code knowledge and the repository-specific knowledge via a graph-based retrieval-generation process. In particular, GraphCoder captures the context of completion target more accurately through code context graph (CCG) that consists of control-flow, data- and control-dependence between code statements, a more structured way to capture the completion target context than the sequence-based context used in existing retrieval-augmented approaches; based on CCG, GraphCoder further employs a coarse-to-fine retrieval process to locate context-similar code snippets with the completion target from the current repository. Experimental results demonstrate both the effectiveness and efficiency of GraphCoder: Compared to baseline retrieval-augmented methods, GraphCoder achieves higher exact match (EM) on average, with increases of +6.06 in code match and +6.23 in identifier match, while using less time and space.

研究动机与目标

  • 为解决代码大模型在仓库级代码补全中因缺乏仓库特定知识而导致的局限性。
  • 通过使用结构化的代码上下文图(CCG)替代基于序列的表示方法,提升相关代码片段检索的准确性。
  • 通过利用语句级图索引和按距离衰减的结构相似性,降低检索开销并提升效率。
  • 通过检索增强生成(RAG)将仓库特定上下文集成到基于大模型的代码补全中,提升代码生成质量。
  • 在不同仓库和编程语言中评估 GraphCoder 的有效性和效率,特别是在代码重复率较低的条件下。

提出的方法

  • 构建一个语句级多图形式的代码上下文图(CCG),包含三种边类型:控制流、数据依赖和控制依赖。
  • 采用粗到精的检索策略:首先基于序列相似性过滤候选,再通过按距离衰减的结构相似性进行重排序,优先选择语义相关性高的语句。
  • 使用按距离衰减的度量方法,根据语句与补全目标的结构距离,动态加权其重要性。
  • 将检索到的代码片段整合到预训练代码大模型(如 GPT-3.5、StarCoder、CodeGen)的提示词中,用于生成代码补全。
  • 以语句为单位进行代码存储与索引,而非按行,从而减少存储开销并提升检索效率。
  • 在所有方法中统一使用提示词模板,以确保在令牌消耗和生成开销方面的公平比较。

实验结果

研究问题

  • RQ1GraphCoder 基于图的检索与基于序列的检索相比,在代码补全准确性方面表现如何?
  • RQ2使用代码上下文图(CCG)在仓库级代码补全中,对检索有效性与效率的提升程度如何?
  • RQ3GraphCoder 在仓库中代码重复率不同的情况下表现如何?
  • RQ4结构相似性(按距离衰减)对检索质量与生成准确性的影响是什么?
  • RQ5与现有基于 RAG 的代码补全方法相比,GraphCoder 在计算开销(令牌数、时间、空间)方面表现如何?

主要发现

  • 与基线 RAG 方法相比,GraphCoder 在代码精确匹配上提升了 +6.06,在标识符精确匹配上提升了 +6.23。
  • 通过使用语句级图索引而非行级存储,框架显著降低了检索时间和存储空间开销。
  • GraphCoder 的输入和输出令牌数少于 RepoCoder 及其他 RAG 基线方法,尽管检索准确率更高,但令牌使用量未显著增加。
  • 按距离衰减的结构相似性度量通过优先选择与补全目标在结构上更接近且语义相关的语句,有效提升了检索质量。
  • 即使在代码重复率较低的仓库中,GraphCoder 仍保持较强的性能表现,尽管在极低重复率下召回率有所下降,如消融实验所示。
  • 该方法在不同编程语言(Python 和 Java)中具有良好的泛化能力,且可通过基于 tree-sitter 的 AST 解析扩展至其他语言以构建 CCG。

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。