跳到内容

抛弃普通思维链

Plan and Solve1 改进了零样本思维链(CoT)提示的使用,该提示为大型语言模型添加了更详细的指令。

Plan and Solve 提示

[用户提示]

让我们首先理解问题,提取相关变量及其对应的数值,并制定完整的计划。然后,让我们执行计划,计算中间变量(注意正确的数值计算和常识),逐步解决问题,并显示答案。

[模型响应]

因此答案(阿拉伯数字)是

这是一个两步过程,指导 LLM 更关注计算和中间结果,以尽可能确保其正确执行。

  1. 生成推理:在第一步中,我们使用用户查询提示模型,并使用 plan and solve 提示预热模型,以便在生成中间推理过程之前明确制定解决问题的计划。
  2. 提取答案:一旦获得模型的推理,我们就会从包含模型思维链的新提示中提取答案。

我们可以使用 instructor 来实现这一点,如下所示。

import instructor
from openai import OpenAI
from pydantic import BaseModel

client = instructor.from_openai(OpenAI())


class Reasoning(BaseModel):
    chain_of_thought: str


class Response(BaseModel):
    correct_answer: str


def generate_reasoning(query: str):
    return client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"""
                <user query>
                {query}
                </user query>

                Let's first understand the problem,
                extract relevant variables and their
                corresponding numerals, and make a
                complete plan. Then, let's carry out
                the plan, calculate intermediate
                variables (pay attention to correct
                numerical calculation and commonsense),
                solve the problem step by step, and
                show the answer.
                """,
            },
        ],
        response_model=Reasoning,
        model="gpt-4o",
    )


def extract_answer(query: str, reasoning: Reasoning):
    return client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": f"""
                <user query>
                    {query}
                </user query>

                Let's first understand the problem,
                extract relevant variables and their
                corresponding numerals, and make a
                complete plan. Then, let's carry out
                the plan, calculate intermediate
                variables (pay attention to correct
                numerical calculation and commonsense),
                solve the problem step by step, and
                show the answer.

                <reasoning>
                {reasoning.chain_of_thought}
                </reasoning>

                Therefore the answer (arabic numerals) is
                """,
            }
        ],
        model="gpt-4o",
        response_model=Response,
    )


if __name__ == "__main__":
    query = (
        "In a dance class of 20 students, 20% enrolled "
        "in contemporary dance, 25% of the remaining "
        "enrolled in jazz dance and the rest enrolled "
        "in hip-hop dance. What percentage of the entire "
        "students enrolled in hip-hop dance?"
    )

    reasoning = generate_reasoning(query)
    print(reasoning.model_dump_json(indent=2))
    """
    {
    "chain_of_thought": "Let's first break down the
    problem:\n\n1. Total number of students = 20\n2.
    Percentage enrolled in contemporary dance = 20%\n\n
    Step-by-Step Plan:\n1. Calculate the number of
    students enrolled in contemporary dance.\n2.
    Calculate the remaining students after contemporary
    dance enrollment.\n3. Calculate the percentage and
    number of students from the remaining who enrolled in
    jazz dance.\n4. Determine the remaining students who
    enrolled in hip-hop dance.\n5. Finally, calculate the
    percentage of the entire students who enrolled in
    hip-hop dance.\n\nLet's carry out the plan:\n\n1.
    Number of students enrolled in contemporary dance =
    20% of 20 = (20/100) * 20 = 4\n2. Remaining students
    after contemporary = 20 - 4 = 16\n3. Percentage of
    remaining students enrolled in jazz dance = 25%\n
    Number of students enrolled in jazz dance = 25% of 16
    = (25/100) * 16 = 4\n4. Remaining students after
    contemporary and jazz = 16 - 4 = 12\n5. The number of
    students enrolled in hip-hop dance = 12\n6.
    Percentage of entire students enrolled in hip-hop =
    (Number of hip-hop students / Total students) *
    100\n   Percentage = (12 / 20) * 100 = 60%\n\nThus,
    60% of the entire students enrolled in hip-hop dance."
    }
    """

    response = extract_answer(query, reasoning)
    print(response.model_dump_json(indent=2))
    """
    {
      "correct_answer": "60"
    }
    """

参考资料

1: Plan-and-Solve Prompting: 通过大型语言模型改进零样本思维链推理