Abstract
In recent years, large language models (LLMs) have excelled in natural language processing tasks but face significant challenges in complex reasoning tasks such as mathematical reasoning and code generation. To address these limitations, we propose KG-Augmented Executable Chain-of-Thought (KGA-ECoT), a novel framework that enhances code generation through knowledge graphs and improves mathematical reasoning via executable code. KGA-ECoT decomposes problems into a Structured Task Graph, leverages efficient GraphRAG for precise knowledge retrieval from mathematical libraries, and generates verifiable code to ensure computational accuracy. Evaluations on multiple mathematical reasoning benchmarks demonstrate that KGA-ECoT significantly outperforms existing prompting methods, achieving absolute accuracy improvements ranging from several to over ten percentage points. Further analysis confirms the critical roles of GraphRAG in enhancing code quality and external code execution in ensuring precision. These findings collectively establish KGA-ECoT as a robust and highly generalizable framework for complex mathematical reasoning tasks.
Introduction
Large language models (LLMs) have demonstrated remarkable capabilities in natural language processing, yet they frequently encounter significant hurdles in complex reasoning tasks, particularly in mathematics and code generation. These challenges stem from three primary issues: reasoning drift, where intermediate errors cascade; knowledge gaps regarding specific technical libraries like SymPy; and a lack of verifiability in traditional textual Chain-of-Thought (CoT) reasoning where logic cannot be easily validated.
To address these limitations, we present KG-Augmented Executable Chain-of-Thought (KGA-ECoT). This framework shifts the paradigm from linear text generation to a structured "plan-retrieve-execute" workflow, integrating knowledge graphs (KGs) for precise information retrieval and verifiable code execution to ensure computational accuracy.
The KGA-ECoT Framework
KGA-ECoT operates through a multi-stage pipeline designed to mirror engineering problem-solving, moving beyond word-by-word prediction to a structured architectural approach.
1. Structured Task Graph
The process begins by decomposing a natural language problem into a Structured Task Graph. The model extracts specific conditions, defines a goal, and identifies subtasks. Crucially, it models the dependencies between these tasks, ensuring that the logical flow is established before any code is written. This prevents the "reasoning drift" common in linear CoT.
2. GraphRAG and Hierarchical Embedding
To bridge the gap between user queries and technical documentation, KGA-ECoT utilizes a directed acyclic knowledge graph of mathematical libraries (e.g., SymPy).
A novel hierarchical embedding technique using feature propagation allows the system to perform context-aware retrieval. By fusing textual descriptions with the graph's topology, the model can accurately identify the most relevant functions and their usage patterns within the library's hierarchy, ensuring the model uses the correct "tools" for the task.
3. Executable Code Generation and Verification
Based on the task graph and retrieved knowledge, the model generates Python code. This code is not merely a textual output but is executed within a secure Docker sandbox. This "execution-as-verification" step acts as a truth anchor, eliminating calculation errors and providing immediate feedback. If execution fails, error logs are fed back into the model for iterative self-correction.
Experimental Evaluation
The framework was rigorously tested on three major benchmarks: GSM8K (elementary arithmetic), MATH-500 (advanced algebra and geometry), and SVAMP (reasoning robustness). Testing spanned several model backbones, including DeepSeek (7B, 14B) and LLaMA 3/3.1 (3B, 8B).
Key Results
KGA-ECoT consistently outperformed standard CoT and other advanced prompting baselines:
- MATH-500: Achieved 46.60% accuracy with Llama 3.1-8B, a significant jump from the 39.40% baseline.
- SVAMP: Reached 84.67% accuracy, demonstrating high resistance to superficial pattern matching.
- Ablation Success: Removing either the GraphRAG component or the execution module resulted in performance drops of 5-10%, confirming that the synergy between structured knowledge and execution is the primary driver of success.
Discussion and Insights
The success of KGA-ECoT highlights several critical insights for the future of AI reasoning:
- Code as a Truth Anchor: Forcing reasoning into an executable format provides a level of determinism that prevents the "hallucinations" common in purely textual models.
- Planning vs. Linear Generation: Transitioning to a "Task Graph" allows models to handle non-linear dependencies, mirroring how a human engineer might architect a solution.
- Context-Aware Retrieval: Hierarchical embeddings solve the semantic heterogeneity problem, making Retrieval-Augmented Generation (RAG) significantly more effective for technical domains where structure matters as much as content.
Limitations and Future Work
While KGA-ECoT marks a significant advancement, it currently focuses primarily on the SymPy library. Future iterations will aim to expand the Knowledge Graph to include broader scientific stacks like NumPy or SciPy. Additionally, while the "plan-retrieve-execute" loop is highly accurate, it introduces higher computational overhead, suggesting a need for adaptive systems that determine when this heavy-duty reasoning is truly necessary versus "cheaper" methods.
Summary updated