【论文精读-代码生成】Structured Chain-of-Thought Prompting for Code Generation

动机

(思维链在大模型自动生成代码领域的扩展应用)
源代码包含丰富的结构信息,任何代码都可以由三种程序结构(即序列、分支和循环结构)组成。直观地说,结构化的中间推理步骤造就了结构化的源代码。因此,我们要求 LLM 使用程序结构构建 CoT,得到 SCoT。然后,LLMs 根据 SCoT 生成最终代码。与 CoT prompting 相比,SCoT prompting 明确约束 LLMs 从源代码的角度思考如何解决需求,进一步提高了 LLMs 在代码生成中的性能。

做了什么

  1. 本文提出一种结构化思维链(SCoT),利用程序结构来构建中间推理步骤。
  2. 提出了一种用于代码生成的SCoT提示技术。它提示大型语言模型首先生成一个SCoT,然后实现代码。
  3. 在三个基准上进行了广泛的实验。定性和定量实验表明,SCoT prompting明显优于SOTA基线(e.g.,思维链提示)。
  4. 讨论了不同程序结构的贡献和SCoT prompting的健壮性。

怎么做的

  1. SCoT提示流程:
    1. 编写示例二元组–<requirement, SCoT> ,要求这些例子涵盖了三个基本的程序结构和输入输出结构。再提出一个新需求,送入llm。我们希望llm从示例中学习并为新需求生成一个SCoT。

1711177966325.png

  1. 生成一个SCoT后,设计第二个用于生成最终代码的prompt:需求和对应的SCoT。提示以三个示例<requirement, S
### Chain-of-Thought Prompting Mechanism in Large Language Models In large language models, chain-of-thought prompting serves as a method to enhance reasoning capabilities by guiding the model through structured thought processes. This approach involves breaking down complex problems into simpler components and providing step-by-step guidance that mirrors human cognitive processing. The creation of these prompts typically includes selecting examples from training datasets where each example represents part of an overall problem-solving process[^2]. By decomposing tasks into multiple steps, this technique encourages deeper understanding and more accurate predictions compared to traditional methods. For instance, when faced with multi-hop question answering or logical deduction challenges, using such chains allows models not only to generate correct answers but also articulate intermediate thoughts leading up to those conclusions. Such transparency facilitates better interpretability while improving performance on various NLP benchmarks. ```python def create_chain_of_thought_prompt(task_description, examples): """ Creates a chain-of-thought prompt based on given task description and examples. Args: task_description (str): Description of the task at hand. examples (list): List containing tuples of input-output pairs used for demonstration purposes. Returns: str: Formatted string representing the final prompt including both instructions and sample cases. """ formatted_examples = "\n".join([f"Input: {ex[0]}, Output: {ex[1]}" for ex in examples]) return f""" Task: {task_description} Examples: {formatted_examples} Now try solving similar questions following above pattern. """ # Example usage examples = [ ("What color do you get mixing red and blue?", "Purple"), ("If it rains tomorrow, will we have our picnic?", "No") ] print(create_chain_of_thought_prompt("Solve logic puzzles", examples)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chaoql

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值