基于引用的代码理解:通过混合检索和图增强上下文防止LLM幻觉
Citation-Grounded Code Comprehension: Preventing LLM Hallucination Through Hybrid Retrieval and Graph-Augmented Context
AI总结:
本文提出混合检索与图增强上下文方法,通过系统评估实现92%引用准确率,有效防止LLM幻觉。
摘要:
大型语言模型已成为代码理解的重要工具,使开发者能够通过自然语言接口查询不熟悉的代码库。然而,LLM幻觉,即生成看似合理但事实错误的引用,仍然是可靠开发者辅助的关键障碍。本文通过混合检索和轻量级结构推理来解决实现可验证、基于引用的代码理解的挑战。我们的工作基于系统评估,覆盖30个Python仓库和180个开发者查询,比较检索模式、图扩展策略和引用验证机制。我们发现,引用准确性挑战源于稀疏词法匹配、密集语义相似性和跨文件架构依赖性的相互作用。其中,跨文件证据发现是引用完整性的最大贡献者,但现有系统依赖纯文本相似性而不利用代码结构。我们倡导引用基于生成作为代码理解系统的建筑原则,并通过实现92%的引用准确率且无幻觉来证明这一需求。具体而言,我们开发了一种混合检索系统,结合BM25稀疏匹配、BGE密集嵌入和Neo4j图扩展通过导入关系,比单一模式基线高出14到18个百分点,并在62%的架构查询中发现纯文本相似性所遗漏的跨文件证据。
英文摘要:
Large language models have become essential tools for code comprehension, enabling developers to query unfamiliar codebases through natural language interfaces. However, LLM hallucination, generating plausible but factually incorrect citations to source code, remains a critical barrier to reliable developer assistance. This paper addresses the challenges of achieving verifiable, citation grounded code comprehension through hybrid retrieval and lightweight structural reasoning. Our work is grounded in systematic evaluation across 30 Python repositories with 180 developer queries, comparing retrieval modalities, graph expansion strategies, and citation verification mechanisms. We find that challenges of citation accuracy arise from the interplay between sparse lexical matching, dense semantic similarity, and cross file architectural dependencies. Among these, cross file evidence discovery is the largest contributor to citation completeness, but it is largely overlooked because existing systems rely on pure textual similarity without leveraging code structure. We advocate for citation grounded generation as an architectural principle for code comprehension systems and demonstrate this need by achieving 92 percent citation accuracy with zero hallucinations. Specifically, we develop a hybrid retrieval system combining BM25 sparse matching, BGE dense embeddings, and Neo4j graph expansion via import relationships, which outperforms single mode baselines by 14 to 18 percentage points while discovering cross file evidence missed by pure text similarity in 62 percent of architectural queries.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。