CodeChain: Towards Modular Code Generation Through Chain of Self-revisions with Representative Sub-modules
Hung Le, Hailin Chen, Amrita Saha, Akash Gokul, Doyen Sahoo, Shafiq Joty
摘要
Large Language Models (LLMs) have already become quite proficient at solving simpler programming tasks like those in HumanEval or MBPP benchmarks. However, solving more complex and competitive programming tasks is still quite challenging for these models -possibly due to their tendency to generate solutions as monolithic code blocks instead of decomposing them into logical sub-tasks and sub-modules. On the other hand, experienced programmers instinctively write modularized code with abstraction for solving complex tasks, often reusing previously developed modules. To address this gap, we propose CodeChain, a novel framework for inference that elicits modularized code generation through a chain of self-revisions, each being guided by some representative sub-modules generated in previous iterations. Concretely, CodeChain first instructs the LLM to generate modularized codes through chain-of-thought prompting. Then it applies a chain of self-revisions by iterating the two steps: 1) extracting and clustering the generated sub-modules and selecting the cluster representatives as the more generic and re-usable implementations, and 2) augmenting the original chain-of-thought prompt with these selected module-implementations and instructing the LLM to re-generate new modularized solutions. We find that by naturally encouraging the LLM to reuse the previously developed and verified sub-modules, CodeChain can significantly boost both modularity as well as correctness of the generated solutions, achieving relative pass@1 improvements of 35% on APPS and 76% on CodeContests. It is shown to be effective on both OpenAI LLMs as well as opensourced LLMs like WizardCoder. We also conduct comprehensive ablation studies with different methods of prompting, number of clusters, model sizes, program qualities, etc., to provide useful insights that underpin CodeChain's success 1 .
问 Lune
几分钟读透这篇 Paper,不用再花一下午。
Lune 能读完全文,也能读引用它的顶会 Paper,讲清它的结论、证据和局限,每一点都注明出自哪篇 Paper。不用硬啃,也不靠猜。
已有 15 篇顶会 Paper 引用了它。别等审稿人问起,先弄清它们发现了什么。
脉络
- LLM-Assisted Code Cleaning For Training Accurate Code Generators 引用了 Reflexion: language agents with verbal reinforcement learning
- LLM-Assisted Code Cleaning For Training Accurate Code Generators 引用了 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- LLM-Assisted Code Cleaning For Training Accurate Code Generators 引用了 Training language models to follow instructions with human feedback
- INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness 引用了 Reflexion: language agents with verbal reinforcement learning
- INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness 引用了 Language Models are Few-Shot Learners
- INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and Helpfulness 引用了 Training language models to follow instructions with human feedback
- ExploraCoder: Advancing Code Generation for Multiple Unseen APIs via Planning and Chained Exploration 引用了 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Reflexion: language agents with verbal reinforcement learning 引用了 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Large Language Models are Zero-Shot Reasoners 引用了 Language Models are Few-Shot Learners
- Large Language Models are Zero-Shot Reasoners 引用了 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Large Language Models are Zero-Shot Reasoners 引用了 Training language models to follow instructions with human feedback
- AutoIOT: LLM-Driven Automated Natural Language Programming for AIoT Applications 引用了 Language Models are Few-Shot Learners
- AutoIOT: LLM-Driven Automated Natural Language Programming for AIoT Applications 引用了 Chain-of-Thought Prompting Elicits Reasoning in Large Language Models
- Chain-of-Thought Prompting Elicits Reasoning in Large Language Models 引用了 Training language models to follow instructions with human feedback
- Training language models to follow instructions with human feedback 引用了 Language Models are Few-Shot Learners
引用它的顶会 Paper15
- LLM-Assisted Code Cleaning For Training Accurate Code GeneratorsNaman Jain, Tianjun Zhang, Wei-Lin Chiang, Joseph E. Gonzalez 等ICLR 2024 · 49 次引用
- AutoIOT: LLM-Driven Automated Natural Language Programming for AIoT ApplicationsLeming Shen, Qiang Yang, Yuanqing Zheng, Mo LiMobiCom 2025 · 14 次引用
- ExploraCoder: Advancing Code Generation for Multiple Unseen APIs via Planning and Chained ExplorationYunkun Wang, Yue Zhang, Zhen Qin, Chen Zhi 等ACL 2025 · 13 次引用
- INDICT: Code Generation with Internal Dialogues of Critiques for Both Security and HelpfulnessHung Le, Doyen Sahoo, Yingbo Zhou, Caiming Xiong 等NeurIPS 2024 · 12 次引用
- Enhancing Code Generation via Bidirectional Comment-Level Mutual GroundingYifeng Di, Tianyi ZhangICSE 2025 · 3 次引用
它引用的 Paper21
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah 等NeurIPS 2020 · 64,255 次引用
- Training language models to follow instructions with human feedbackLong Ouyang, Jeffrey Wu, Xu Jiang, Diogo Almeida 等NeurIPS 2022 · 24,707 次引用
- Chain-of-Thought Prompting Elicits Reasoning in Large Language ModelsJason Wei, Xuezhi Wang, Dale Schuurmans, Maarten Bosma 等NeurIPS 2022 · 22,562 次引用
- Large Language Models are Zero-Shot ReasonersTakeshi Kojima, Shixiang Shane Gu, Machel Reid, Yutaka Matsuo 等NeurIPS 2022 · 8,168 次引用
- Reflexion: language agents with verbal reinforcement learningNoah Shinn, Federico Cassano, Ashwin Gopinath, Karthik Narasimhan 等NeurIPS 2023 · 5,828 次引用
相关 Paper
- MoT: Modularization-of-Thought Prompting for Effective Code GenerationRuwei Pan, Hongyu ZhangISSTA 2026
- Beyond Accuracy: Evaluating Self-Consistency of Code Large Language Models with IdentityChainMarcus J. Min, Yangruibo Ding, Luca Buratti, Saurabh Pujar 等ICLR 2024 · 39 次引用
- What Makes Large Language Models Reason in (Multi-Turn) Code Generation?Kunhao Zheng, Juliette Decugis, Jonas Gehring, Taco Cohen 等ICLR 2025
- A Problem-Oriented Perspective and Anchor Verification for Code OptimizationTong Ye, Tengfei Ma, Xuhong Zhang, Hang Yu 等ICLR 2026 · 3 次引用
- Think Like Human Developers: Harnessing Community Knowledge for Structured Code ReasoningChengran Yang, Zhensu Sun, Hong Jin Kang, Jieke Shi 等ICSE 2026
