arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2602.11671cs.SE
收藏

不要将代码视为自然语言:对仓库级代码生成及更广泛领域的启示

Do Not Treat Code as Natural Language: Implications for Repository-Level Code Generation and Beyond

FPT Software AI Center(FPT软件人工智能中心);Hanoi University of Science and Technology(河内科学技术大学);The University of Melbourne(墨尔本大学)

Minh Le-Anh, Huyen Nguyen, Khanh An Tran, Nam Le Hai, Linh Ngo Van, Nghi D. Q. Bui, Bach Le

AI总结:

Hydra通过结构化代码生成框架提升仓库级代码生成性能,超越现有方法并实现更高效依赖检索。

摘要:

大型语言模型用于代码(CodeLLMs)在独立的代码补全和生成任务中表现出色,有时甚至超越人类表现,但在仓库级设置中效果下降,因为需要跨文件依赖和结构上下文。现有的检索增强生成(RAG)方法通常借鉴自然语言处理的策略,依赖基于分块的索引和相似性检索。分块导致代码单元之间的连贯性丧失,并忽略了结构关系,而基于相似性的方法经常遗漏功能相关依赖,如辅助函数、类或全局变量。为了解决这些限制,我们提出了Hydra,一个仓库级代码生成框架,将代码视为结构化的代码而非自然语言。我们的方法引入(i)一种结构感知的索引策略,将仓库表示为函数、类和变量的层次树,保留代码结构和依赖关系;(ii)一种轻量级的依赖感知检索器(DAR),明确识别和检索目标函数所需的真实依赖;(iii)一种混合检索机制,结合DAR和基于相似性的检索,提供必要的构建块和实用使用示例。在挑战性的DevEval和RepoExec基准测试中,广泛实验表明Hydra在开源和闭源CodeLLMs上均达到最先进的性能。值得注意的是,我们的方法在仓库级代码生成中建立了新的状态,比最强基线在Pass@1上高出超过5%,甚至使较小的模型能够匹配或超越依赖现有检索器的更大模型的性能。

英文摘要:

Large language models for code (CodeLLMs) have demonstrated remarkable success in standalone code completion and generation, sometimes even surpassing human performance, yet their effectiveness diminishes in repository-level settings where cross-file dependencies and structural context are essential. Existing Retrieval-Augmented Generation (RAG) approaches often borrow strategies from NLP, relying on chunking-based indexing and similarity-based retrieval. Chunking results in the loss of coherence between code units and overlooks structural relationships, while similarity-driven methods frequently miss functionally relevant dependencies such as helper functions, classes, or global variables. To address these limitations, we present Hydra, a repository-level code generation framework that treats code as structured code rather than natural language. Our approach introduces (i) a structure-aware indexing strategy that represents repositories as hierarchical trees of functions, classes, and variables, preserving code structure and dependencies, (ii) a lightweight dependency-aware retriever (DAR) that explicitly identifies and retrieves the true dependencies required by a target function, and (iii) a hybrid retrieval mechanism that combines DAR with similarity-based retrieval to provide both essential building blocks and practical usage examples. Extensive experiments on the challenging DevEval and RepoExec benchmarks, both requiring function implementation from real-world repositories with complex large repository context, show that Hydra achieves state-of-the-art performance across open- and closed-source CodeLLMs. Notably, our method establishes a new state of the art in repository-level code generation, surpassing strongest baseline by over 5% in Pass@1 and even enabling smaller models to match or exceed the performance of much larger ones that rely on existing retrievers.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑