TL;DR
Chain of Code (CoC) is a new reasoning framework that prompts language models to solve problems by writing code and then "emulating" its execution. By using the LM to simulate the output of non-executable semantic functions (like is_sarcastic()), CoC combines the rigorous logic of programming with the intuitive power of natural language, outperforming Chain of Thought (CoT) by 12% on the challenging BIG-Bench Hard benchmark.
What Problem Does This Paper Address?
Large Language Models (LLMs) have two distinct ways of "thinking": Chain of Thought (CoT), which uses natural language steps, and Program of Thoughts (PoT), which writes and executes code.
CoT is great for "fuzzy" semantic reasoning (e.g., "Is this joke funny?") but often trips over math and logic. PoT is perfect for math and logic but fails when a problem requires human-like intuition that can't be easily written into a Python function. For example, if you ask a model to count sarcastic sentences in a text, it can't write a standard Python library function for detect_sarcasm(string). This paper addresses the gap between these two worlds: how can we get the structural benefits of code for problems that are fundamentally linguistic?
Methods
The authors introduce Chain of Code (CoC), which operates in two main phases:
- Generation: The LM is prompted to write a program (often containing pseudocode or "helper" functions) to solve the query.
- Execution (The "LMulator"): The code is run line-by-line.
- If a line is valid Python (e.g.,
x = 5 + 2), a standard Python interpreter executes it and updates the program state. - If a line is non-executable or "semantic" (e.g.,
if is_sarcastic(sentence):), the LM acts as an emulator (the "LMulator"). It predicts the expected output based on the current program state and updates the variables accordingly.
This "interweaving" allows the model to maintain a precise state (like a counter in a loop) while using its linguistic knowledge to handle the "messy" parts of the task.
Datasets
The researchers evaluated CoC across several diverse benchmarks:
- BIG-Bench Hard (BBH): A suite of 23 difficult tasks ranging from logical deduction to movie recommendations.
- GSM8K: A standard benchmark for grade-school math word problems.
- Robotics Tasks: A custom set of 7 tabletop manipulation tasks where a robot must reason about semantic concepts (e.g., "Sort these items into compost vs. recycle") to generate actions.
Key Insights and Ideas
- Code as a Scaffold: Code provides a superior syntactic structure for reasoning. It forces the model to break problems into discrete, logical steps (loops, conditionals, state updates) that are more reliable than free-form text.
- The "LMulator" Advantage: The LM doesn't need to know how to implement a complex function; it only needs to know what the result of that function should be in a specific context.
- Cross-Task Generalization: Surprisingly, CoC works even when the model is given examples from completely different tasks. This suggests that "thinking in code" is a generalizable skill the model can apply once it understands the format of the reasoning.
- Scaling Benefits: Unlike CoT, which primarily shows benefits in very large models, CoC improves performance even for smaller models, making structured reasoning more accessible.
Key Results
- State-of-the-Art Performance: On BIG-Bench Hard, CoC achieved 84.3% accuracy using
text-davinci-003, a significant jump over CoT's 72.6%. - Surpassing Humans: CoC outperformed the average human rater on the BBH suite.
- GPT-4 Synergy: When applied to GPT-4, CoC reached a staggering 91.2% on BBH.
- Robotics Success: In the robotics domain, CoC allowed a robot to successfully navigate tasks involving new objects and languages by emulating the semantic understanding of ingredients.
Limitations and Future Work
- Context Window: CoC requires generating code, execution traces, and state updates, which consumes more tokens and context space than simple direct answering.
- State Tracking Complexity: The current implementation tracks state by parsing strings into Python dictionaries. This works for simple types (ints, strings, lists) but struggles with complex, custom Python objects.
- Security: Running LM-generated code in a real interpreter requires strict sandboxing to prevent the model from accidentally (or maliciously) executing harmful commands.
- Future Direction: The authors suggest fine-tuning models specifically to be better "LMulators"—training them to be more accurate at predicting the line-by-line state changes of a program.
Summary updated