arXiv:2601.08773cs.SEcs.AI2026-01被引 4

用代码语法树构建图谱,比大模型生成更准更快地理解代码架构。

Reliable Graph-RAG for Codebases: AST-Derived Graphs vs LLM-Extracted Knowledge Graphs

  • 用 Tree-sitter 和双向遍历生成确定性代码图谱,替代大模型提取知识图谱。
  • 在 Shopizer 上,确定性图谱正确率最高,大模型图谱有377个文件遗漏。
  • 构建速度快、成本低,适合需要精准多跳推理的代码理解场景。

软件工程中的检索增强生成常依赖向量相似性搜索,但难以处理控制器→服务→仓库等多跳架构推理。本文在 Java 代码库(Shopizer,另含 ThingsBoard 与 OpenMRS Core)上对比三种检索流程:(A) 仅向量的无图 RAG,(B) 大模型生成的知识图谱 RAG(LLM-KB),(C) 基于语法树(AST)的确定性知识图谱 RAG(DKB),使用 Tree-sitter 与双向遍历构建。每库测试 15 个架构与代码追踪查询,评估索引时间、查询延迟、语料覆盖率、成本及答案正确性。DKB 索引仅需数秒,而 LLM-KB 耗时更长;在 Shopizer 上,LLM-KB 有 377 个文件被跳过或遗漏,嵌入块覆盖率与图规模低于 DKB。端到端成本方面,DKB 相对基线略高但远低于 LLM-KB,尤其在代码库扩大时差距显著。查询延迟上,无图与 DKB 接近,而 LLM-KB 更慢且波动大。在 Shopizer 测评中,DKB 正确率最高,LLM-KB 次之,向量基线在上游架构查询中表现最差,且幻觉风险最高。总体而言,确定性 AST 图谱在覆盖可靠性与多跳定位上优于大模型提取图谱,且索引成本大幅降低。

原文摘要 · Abstract (English)

Retrieval-Augmented Generation for software engineering often relies on vector similarity search, which captures topical similarity but can fail on multi-hop architectural reasoning such as controller to service to repository chains, interface-driven wiring, and inheritance. This paper benchmarks three retrieval pipelines on Java codebases (Shopizer, with additional runs on ThingsBoard and OpenMRS Core): (A) vector-only No-Graph RAG, (B) an LLM-generated knowledge graph RAG (LLM-KB), and (C) a deterministic AST-derived knowledge graph RAG (DKB) built with Tree-sitter and bidirectional traversal. Using 15 architecture and code-tracing queries per repository, we measure indexing time, query latency, corpus coverage, cost, and answer correctness. DKB builds its graph in seconds, while LLM-KB requires much longer graph generation. LLM-KB also shows indexing incompleteness: on Shopizer, 377 files are skipped or missed, reducing embedded chunk coverage and graph size compared to DKB. End-to-end cost is modest for DKB relative to the vector-only baseline but much higher for LLM-KB, especially as repository scale increases. Query latency is similar for No-Graph and DKB, while LLM-KB is slower and more variable. On the Shopizer question suite, DKB achieves the highest correctness, LLM-KB is close behind, and the vector-only baseline performs worst on upstream architectural queries and has the highest hallucination risk. Overall, deterministic AST-derived graphs provide more reliable coverage and multi-hop grounding than LLM-extracted graphs at substantially lower indexing cost.

代码理解知识图谱RAGAST

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