Repository-Level Code Generation Method Enhanced by Context-Dependent Graph Retrieval
摘要
Large language models (LLMs) excel in NL2CODE (natural language to code) tasks, but they are prone to generating hallucinatory or redundant code in repository contexts due to a lack of global perspective. To mitigate the problem, preliminary works have attempted to extract task-relevant context from repositories to aid code generation. Nonetheless, these methods do not delve deeply enough into processing repository context information since they overlook the hierarchical and dependency relationships among contexts. Therefore, we propose a code generation method enhanced by context-dependent graph retrieval. First, we present a method for constructing a Repository Context Dependency Graph (RCDG) designed toencapsulate multiple types of code entities within a repository, along with their interdependencies. Subsequently, we propose a code generation framework enhanced by context dependency graph retrieval. Semantically related context collections are obtained through heterogeneous graph retrieval and Breadth-First Search(BFS). These collected contexts undergo a further refinement process through the use of LLMs, aiming to elevate the relevance of the retrieved contexts. Finally, we automatically construct prompts for the contexts and requirements to effectively utilize the retrieved relevant contexts, guiding the model to generate high-quality code. Experimental results indicate that our method outperforms current state-of the-art methods across multiple metrics.