arXiv:2511.07584cs.SEcs.AI2025-11被引 8

用知识图谱和约束求解提升代码生成的语义准确性

SemanticForge: Repository-Level Code Generation through Semantic Knowledge Graphs and Constraint Satisfaction

  • 构建静态-动态知识图谱融合模型,统一程序语义表示
  • 自然语言生成图查询准确率达73%,显著优于传统方法
  • 实时约束验证与增量更新,适合大型项目持续开发

大语言模型虽推动代码自动化生成,但常因逻辑幻觉(控制流/数据流错误)和结构幻觉(类型不匹配、签名错误、架构不一致)而失效。根源在于缺乏可查询的全局代码库语义表示。本文提出SemanticForge,引入四项关键技术:(1) 新型自动校正算法,融合编译时与运行时程序语义,构建双源知识图谱;(2) 基于神经网络的自然语言到结构化图查询生成方法,精度达73%,较传统检索提升22个百分点;(3) 集成SMT求解的束搜索算法,在生成过程中实时验证约束,而非事后检查;(4) 增量维护算法,以$O(|ΔR| \cdot \log n)$时间复杂度更新知识图谱并保持语义等价性。

原文摘要 · Abstract (English)

Large language models (LLMs) have transformed software development by enabling automated code generation, yet they frequently suffer from systematic errors that limit practical deployment. We identify two critical failure modes: \textit{logical hallucination} (incorrect control/data-flow reasoning) and \textit{schematic hallucination} (type mismatches, signature violations, and architectural inconsistencies). These errors stem from the absence of explicit, queryable representations of repository-wide semantics. This paper presents \textbf{SemanticForge}, which introduces four fundamental algorithmic advances for semantically-aware code generation: (1) a novel automatic reconciliation algorithm for dual static-dynamic knowledge graphs, unifying compile-time and runtime program semantics; (2) a neural approach that learns to generate structured graph queries from natural language, achieving 73\% precision versus 51\% for traditional retrieval; (3) a novel beam search algorithm with integrated SMT solving, enabling real-time constraint verification during generation rather than post-hoc validation; and (4) an incremental maintenance algorithm that updates knowledge graphs in $O(|ΔR| \cdot \log n)$ time while maintaining semantic equivalence.

代码生成知识图谱约束求解LLM

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