arXiv:2505.14394cs.AI2025-05被引 10

用知识图谱提升代码库级生成的上下文准确性

Knowledge Graph Based Repository-Level Code Generation

  • 将代码库建模为知识图谱,捕捉文件间依赖关系
  • 在EvoCodeBench上显著优于基线方法
  • 适合需要理解全局代码结构的开发场景

大型语言模型(LLMs)已能根据自然语言生成代码,但在动态代码库中常因上下文不准而表现不佳。现有代码检索方法在结果质量与上下文相关性方面普遍不足。本文提出一种基于知识图谱的代码检索框架,将代码库表示为图结构,以捕获其结构与关联信息,从而实现更精准的上下文感知代码生成。该框架采用混合检索策略,追踪跨文件模块依赖,生成更稳健且与现有代码一致的代码。我们在仓库级代码生成基准EvoCodeBench上进行了评估,结果表明该方法显著优于基线。研究显示,基于知识图谱的代码生成有望推动更具鲁棒性和上下文敏感性的编程辅助工具发展。

原文摘要 · Abstract (English)

Recent advancements in Large Language Models (LLMs) have transformed code generation from natural language queries. However, despite their extensive knowledge and ability to produce high-quality code, LLMs often struggle with contextual accuracy, particularly in evolving codebases. Current code search and retrieval methods frequently lack robustness in both the quality and contextual relevance of retrieved results, leading to suboptimal code generation. This paper introduces a novel knowledge graph-based approach to improve code search and retrieval leading to better quality of code generation in the context of repository-level tasks. The proposed approach represents code repositories as graphs, capturing structural and relational information for enhanced context-aware code generation. Our framework employs a hybrid approach for code retrieval to improve contextual relevance, track inter-file modular dependencies, generate more robust code and ensure consistency with the existing codebase. We benchmark the proposed approach on the Evolutionary Code Benchmark (EvoCodeBench) dataset, a repository-level code generation benchmark, and demonstrate that our method significantly outperforms the baseline approach. These findings suggest that knowledge graph based code generation could advance robust, context-sensitive coding assistance tools.

代码生成知识图谱上下文感知

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。