用动态依赖图提升代码生成的上下文检索效率与准确性。
Effective and Efficient Context Retrieval via Partial Dependency Graph for Repository-Level Code Generation

- 基于大模型动态构建局部依赖图,按需推理
- 在两个评测集上准确率提升超50%,速度加快7.4倍
- 适合需要高效精准代码生成的开发场景
基于大模型的仓库级代码生成需处理复杂的代码依赖关系。由于上下文窗口有限且缺乏对仓库特性的理解,现有方法多采用检索增强生成(RAG),但传统相似性检索常遗漏关键依赖代码。近期图结构检索虽有改进,但依赖人工规则和静态全局图,灵活性差、维护成本高。受开发者隐式构建部分依赖图并逐步探索行为启发,我们提出DyRetriever:利用大模型先选定入口函数,再沿代码依赖图进行多跳推理,通过语义判断函数是否有助于目标函数生成,无需人工规则,适应性强。不预先构建全局图,而是在需要时动态构建并使用后丢弃,显著降低开销。将DyRetriever与相似性检索结合,构建DyCoder,在CoderEval和DevEval上相较现有RAG方法分别实现25.63%和59.73%的Pass@1相对提升,且比基于静态图的方法快7.4倍。
原文摘要 · Abstract (English)
LLM-based repository-level code generation aims to generate code using the context available in a software repository, requiring LLMs to reason over complex code dependencies. Due to limited context windows and insufficient repository-specific understanding, LLMs typically rely on retrieval-augmented generation (RAG) to incorporate relevant code. Early RAG approaches primarily employ similarity-based retrieval, which often fails to retrieve code snippets that the target function depends on. Recent work introduces graph-based retrieval to model such dependencies, but typically relies on manually designed rules and static global graphs, leading to limited flexibility and high construction and maintenance costs. In contrast, human developers collect helpful context by implicitly constructing a partial dependency graph and iteratively inspecting along it. Inspired by this behavior, we propose DyRetriever, an efficient context retrieval method via partial dependency graphs. DyRetriever uses an LLM to first select a set of entry-point functions and then perform multi-hop reasoning along the code dependency graph. During multi-hop reasoning, it uses the LLM's semantic understanding to validate whether a function can help generate the target function, eliminating manually designed rules and enabling flexibility across scenarios. Instead of statically constructing a global dependency graph, DyRetriever builds a partial graph on demand and discards it after use, reducing construction and maintenance costs. We integrate DyRetriever with a similarity-based code retriever to build DyCoder and evaluate it on CoderEval and DevEval. Experimental results show that DyCoder achieves relative Pass@1 improvements of 25.63% and 59.73% on CoderEval and DevEval, respectively, compared with existing RAG-based methods, while being 7.4x faster than baselines based on static dependency graph construction.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。