arXiv:2606.03705cs.AI2026-06

用代码生成方式让大模型在知识图谱上灵活推理,解决问答中的幻觉和扩展性问题。

Code-on-Graph: Iterative Programmatic Reasoning via Large Language Models on Knowledge Graphs

论文配图:Code-on-Graph: Iterative Programmatic Reasoning via Large Language Models on Knowledge Graphs
图 1 · 摘自论文原文
  • 将知识图谱结构转为可调用的Python类,通过生成代码实现动态推理。
  • 在WebQSP等数据集上比现有方法最高提升10.5%准确率。
  • 适合需要精准知识推理的复杂问答场景,如智能客服、医疗诊断。

知识图谱(KG)被广泛用于缓解大语言模型(LLM)存在的知识过时与幻觉问题。现有LLM-KG融合框架通常依赖预定义操作符从知识图谱中检索事实并注入提示词以生成答案,但面临两大瓶颈:1)灵活性不足,预定义操作符范围有限,难以充分表达复杂问题语义;2)可扩展性差,直接将大量事实注入提示词限制了大规模知识处理能力。为此,我们提出Code-on-Graph(CoG),一种基于程序化推理的LLM-KG融合框架。具体而言,在每一步推理中,CoG首先识别对应的知识图谱模式,并将其表示为Python类,作为访问事实的抽象接口;随后生成基于这些类的可执行代码,运行时将检索到的事实实例化为对应类的对象。该设计实现了灵活的代码驱动推理,避免了将大规模事实直接注入提示词。在WebQSP、CWQ和GrailQA上的实验表明,CoG相比先前最优模型最高提升10.5%。

原文摘要 · Abstract (English)

Knowledge Graphs (KGs) are widely used to mitigate the limitations of Large Language Models (LLMs), such as outdated knowledge and hallucinations. Existing LLM-KG integration frameworks typically rely on predefined operators to retrieve factual knowledge from KGs and inject it into prompts for answer generation. This paradigm faces two critical bottlenecks: 1) Inflexibility: The predefined operators are limited in scope and thus lack sufficient compositional expressiveness to fully capture the complex semantics required by KG questions. 2) Unscalability: Direct injection of factual knowledge into prompts limits scalability in handling large-scale factual knowledge. To address these two bottlenecks, we propose Code-on-Graph (CoG), a programmatic reasoning framework for LLM-KG integration. Specifically, given the factual knowledge retrieved at each reasoning step, CoG first identifies the corresponding KG schemas and represents these schemas as Python classes, which serve as abstract interfaces to the retrieved facts. It then generates executable code grounded in these classes, with the retrieved facts instantiated as objects of the corresponding classes during execution. This design enables flexible code-based reasoning while avoiding the direct injection of large-scale factual knowledge into prompts. Experiments on WebQSP, CWQ, and GrailQA demonstrate that CoG outperforms prior state-of-the-art models by up to 10.5%.

知识图谱程序推理大模型

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