Abstract
Large Language Models (LLMs) have demonstrated remarkable capabilities in natural language processing, yet they often struggle with complex symbolic and arithmetic reasoning. This paper proposes Code Prompting, a neural-symbolic method that encourages LLMs to generate Python code as an intermediate reasoning step. By utilizing the structured and unambiguous nature of code, this approach mitigates the "unreasonable task reduction" often seen in standard Chain-of-Thought (CoT) prompting. Experimental results across various benchmarks show that Code Prompting significantly outperforms CoT, particularly in symbolic reasoning tasks where accuracy improved from 2.8% to nearly 100%.
Introduction
While Chain-of-Thought (CoT) prompting has become a standard for enhancing LLM reasoning, it relies on natural language rationales which are inherently ambiguous. LLMs frequently fail to plan ahead in CoT, attempting to solve complex problems in fewer steps than required. This paper, authored by researchers at Peking University, introduces Code Prompting to address these limitations by treating code as a formal "mind map" for the model's logic.
Methodology
Code Prompting is a two-stage neural-symbolic framework designed to structure the model's internal reasoning process.
The process involves two primary stages:
- Code Generation: The LLM is prompted to generate Python code representing the logic needed to solve the problem. This can be achieved through zero-shot or few-shot prompting.
- Based-on-code Solution: The final answer is derived either by the LLM "mentally" executing its own code (Self-contained) or by passing the code to an external Python interpreter for execution.
To further enhance performance, the authors implemented Self-debugging, where interpreter errors are fed back to the model for correction, and Equation Instruction, which utilizes libraries like sympy for algebraic word problems.
Key Insights
The effectiveness of Code Prompting stems from several structural advantages over natural language rationales:
- Task Decomposition: Code naturally breaks down complex operations into discrete, manageable lines (e.g., using loops for repetitive tasks), preventing the model from skipping critical steps.
- Disambiguation: Formal programming logic eliminates the "fuzziness" of human language, preventing hallucinated logic or ambiguous interpretations of terms.
- Symbolic Grounding: By mapping natural language problems to symbolic code, the model maintains a more stable state throughout the reasoning process.
Key Results
The researchers evaluated the method on symbolic reasoning (Last Letter Concatenation, Coin Flip) and arithmetic reasoning (GSM8K, SVAMP, etc.) benchmarks.
The most striking improvements were observed in symbolic tasks. For the last letter concatenation task with 12 words ($l=12$), zero-shot CoT achieved only 2.8% accuracy, whereas zero-shot Code Prompting with an interpreter reached 99.8%. In arithmetic tasks like GSM8K, an ensemble of CoT and Code Prompting achieved an accuracy of 87.95% using gpt-3.5-turbo.
Conclusion and Future Work
Code Prompting represents a significant step toward more reliable neural-symbolic AI. While the method shows great promise, challenges remain in translating highly complex word problems into executable equations. Future research will focus on optimizing code annotations and exploring the narrowing gap between zero-shot and few-shot performance in increasingly code-aware models. In conclusion, providing LLMs with a structured programming environment allows them to bypass the limitations of natural language reasoning for complex logic.
Summary updated