Skip to main content
QUICK REVIEW

[Paper Review] Structured Chain-of-Thought Prompting for Code Generation

Jia Li, Ge Li|arXiv (Cornell University)|May 11, 2023
Software Engineering ResearchComputer Science17 citations
TL;DR

SCoT prompting constrains LLMs to use program structures (sequence, branch, loop, and IO) to produce intermediate reasoning, leading to higher code-generation accuracy than standard CoT prompting across benchmarks and languages.

ABSTRACT

Large Language Models (LLMs) (e.g., ChatGPT) have shown impressive performance in code generation. LLMs take prompts as inputs, and Chain-of-Thought (CoT) prompting is the state-of-the-art prompting technique. CoT prompting asks LLMs first to generate CoTs (i.e., intermediate natural language reasoning steps) and then output the code. However, CoT prompting is designed for natural language generation and has low accuracy in code generation. In this paper, we propose Structured CoTs (SCoTs) and present a novel prompting technique for code generation, named SCoT prompting. Our motivation is source code contains rich structural information and any code can be composed of three program structures (i.e., sequence, branch, and loop structures). Intuitively, structured intermediate reasoning steps make for structured source code. Thus, we ask LLMs to use program structures to build CoTs, obtaining SCoTs. Then, LLMs generate the final code based on SCoTs. Compared to CoT prompting, SCoT prompting explicitly constrains LLMs to think about how to solve requirements from the view of source code and further the performance of LLMs in code generation. We apply SCoT prompting to two LLMs (i.e., ChatGPT and Codex) and evaluate it on three benchmarks (i.e., HumanEval, MBPP, and MBCPP). (1) SCoT prompting outperforms the state-of-the-art baseline - CoT prompting by up to 13.79% in Pass@1. (2) Human evaluation shows human developers prefer programs from SCoT prompting. (3) SCoT prompting is robust to examples and achieves substantial improvements.

Motivation & Objective

  • Motivate improving code generation by aligning reasoning with code structure.
  • Introduce Structured CoT (SCoT) that uses sequence, branch, loop, and IO structures to build intermediate steps.
  • Propose SCoT prompting: first generate SCoT, then implement code, reducing error propagation.
  • Evaluate SCoT prompting on multiple LLMs and programming languages across benchmark datasets.

Proposed method

  • Define Structured Chain-of-Thought (SCoT) comprising sequence, branch, loop, and input-output structures.
  • Design two prompts: one to generate SCoT and another to generate code from the SCoT.
  • Use a two-step generation pipeline with a debugging step to reduce error accumulation.
  • Apply nucleus sampling and fixed prompts to generate multiple candidates per requirement (Pass@k evaluation).
  • Compare SCoT prompting against Zero-shot, Few-shot, and CoT baselines on HumanEval (Python), MBPP (Python), and MBCPP (C++).

Experimental results

Research questions

  • RQ1RQ1: Does SCoT prompting improve code-generation accuracy (Pass@k) versus baselines across benchmarks and LLMs?
  • RQ2RQ2: Do developers prefer programs produced by SCoT prompting over baselines?
  • RQ3RQ3: Is SCoT prompting robust to choice of example seeds and writing styles?
  • RQ4RQ4: What are the contributions of the basic program structures (sequence, branch, loop) and IO structure to SCoT prompting performance.

Key findings

  • SCoT prompting improves Pass@1 over CoT prompting by up to 13.79% on HumanEval, 12.31% on MBPP, and 6.63% on MBCPP.
  • Human evaluators prefer programs generated by SCoT prompting over baselines in correctness, code smell, and maintainability.
  • SCoT prompting yields gains across LLMs (ChatGPT and Codex) and languages (Python, C++), and is robust to example seeds and writing styles.
  • Ablation shows all three basic structures plus the IO structure contribute to improved performance, with basic structures clearly aiding solvable, well-structured intermediate reasoning.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.