Skip to main content
QUICK REVIEW

[论文解读] CodeCoT: Tackling Code Syntax Errors in CoT Reasoning for Code Generation

Dong Huang, Qingwen Bu|arXiv (Cornell University)|Aug 17, 2023
Experimental Learning in Engineering被引用 5
一句话总结

本文提出 CodeCoT,一种两部分框架——普通 CodeCoT 和自我检测 CodeCoT——通过整合思维链推理与迭代式自我测试,提升大型语言模型的代码生成能力。自我检测 CodeCoT 在使用 gpt-3.5-turbo-0613 模型时,于 HumanEval 基准上实现了 79.27% 的 pass@1 准确率,达到当前最先进水平,显著通过测试驱动的迭代优化提升了代码正确性。

ABSTRACT

Chain-of-thought (CoT) has emerged as a groundbreaking tool in NLP, notably for its efficacy in complex reasoning tasks, such as mathematical proofs. However, its application in code generation faces a distinct challenge, i.e., although the code generated with CoT reasoning is logically correct, it faces the problem of syntax error (e.g., invalid syntax error report) during code execution, which causes the CoT result's pass@1 in HumanEval even lower than the zero-shot result. In this paper, we present Code Chain-of-Thought (CodeCoT) that integrates CoT with a self-examination process for code generation. CodeCoT begins with the LLMs using CoT for initial code development to ensure the generated code follows the correct logic flow. Then, CodeCoT will generate test cases to validate whether the code has syntax errors during the execution. CodeCoT then employs a self-examination phase, in which the generated code is executed against these test cases in the local environment. If the local environment raises error information (e.g., invalid syntax error), CodeCoT will iteratively refine the code based on the feedback information. Within this loop, CodeCoT can make sure their generated codes not only follow the logic flow of the code description, but the syntax error will also be addressed with the self-examination process. Our evaluation results reveal that CodeCoT improves the effectiveness of code generation. For example, CodeCoT increases pass@1 from 75.6% to 79.3% for the HumanEval dataset.

研究动机与目标

  • 解决大型语言模型(LLMs)在代码生成中因推理能力有限且缺乏生成过程中的验证而导致的语法与逻辑错误问题。
  • 通过在生成过程中引入结构化推理与测试用例验证,提升 LLM 生成代码的可靠性与正确性。
  • 探究迭代式自我检查(即模型生成、测试并迭代优化代码)是否在代码生成任务中优于标准 few-shot 提示方法。
  • 评估 few-shot 学习与 CodeCoT 结合的有效性,以确定其对多种 LLM 架构下模型性能的影响。

提出的方法

  • 提出普通 CodeCoT,一种适配于代码生成的标准化思维链提示方法,模型在生成代码前先进行逐步推理。
  • 引入自我检测 CodeCoT,一种增强方法,模型自主生成测试用例,评估代码正确性,并在测试失败时迭代优化代码。
  • 在自我检测过程中集成错误类型检测,以指导代码重生成,提升修复语法与逻辑错误的精确性。
  • 在提示中使用 few-shot 示例,为模型提供任务特定上下文,增强泛化能力与性能表现。
  • 应用迭代优化:在测试失败后,模型根据错误反馈重新生成代码,模拟开发人员的调试工作流程。
  • 使用 HumanEval 等标准基准评估性能,跨 GPT-3.5、Claude 和 PaLM 等多种 LLM 测量 pass@1 与 pass@k 指标。

实验结果

研究问题

  • RQ1思维链提示是否通过明确推理步骤,提升大型语言模型在代码生成中的准确率?
  • RQ2模型自主生成并验证测试用例的自我检查机制,是否相比标准 few-shot 提示能带来更高的代码正确率?
  • RQ3few-shot 示例的数量如何影响不同 LLM 上 CodeCoT 方法的性能表现?
  • RQ4在性能达到平台期或开始下降前,自我检查的最优迭代次数是多少?
  • RQ5模型引入的错误测试用例在优化循环中导致的假阳性或假阴性概率有多大?

主要发现

  • 使用 gpt-3.5-turbo-0613 模型时,自我检测 CodeCoT 在 HumanEval 基准上实现了 79.27% 的 pass@1 准确率,创下代码生成新最先进水平。
  • 与普通 CodeCoT 相比,gpt-3.5-turbo-0613 模型在使用两个 few-shot 示例时,pass@1 准确率提升了 9.76%。
  • 对于 gpt-3.5-turbo-0613 模型,自我检查的性能在四轮迭代后趋于平台期,表明超过此轮次后收益递减。
  • 对于 Claude-instant-1 模型,自我检测 CodeCoT 将 pass@1 从普通方法的 53.00% 提升至 59.76%,表明在小型模型上也具有一致增益。
  • PaLM-2 模型在超过两轮自我检查后无进一步提升,且在两轮后性能略有下降,表明其存在模型特定的限制。
  • 假阳性(正确代码因错误测试失败)风险较低,假阴性(错误代码通过测试)现象也较少,尤其在测试用例多样性提升后更为明显。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。