关于 thought-based-reasoning
thought-based-reasoning 是一项介绍大模型多步推理提示方法的 Agent 技能,涵盖思维链、自洽性、分支搜索、问题拆解、工具调用和代码辅助计算。
主要功能
- 01思维链提示
- 02自洽性投票
- 03树状思路搜索
- 04由简到繁的问题拆解
- 05ReAct 与代码辅助推理
适用场景
- 01解决多步逻辑问题
- 02比较不同推理路径
- 03借助外部信息为回答提供依据
- 04用代码核验计算结果
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ neolabhq/ context-engineering-kit --skill thought-based-reasoning安装后在 Claude Code 中输入/thought-based-reasoning确认已加载。
详细介绍
面向大模型的思维推理技术
概述
思维链(CoT)提示及其变体会鼓励大模型在给出最终答案前生成中间推理步骤,从而显著提升其在复杂推理任务中的表现。这些技术通过将隐式推理显式化,改变了模型处理问题的方式。
快速参考
| 技术 | 适用场景 | 复杂度 | 准确率提升 |
|---|---|---|---|
| 零样本 CoT | 快速推理,没有可用示例 | 低 | +20-60% |
| 少样本 CoT | 有优质示例,需要统一格式 | 中 | +30-70% |
| 自洽性 | 高风险决策,需要置信度 | 中 | 比 CoT 高 +10-20% |
| 思维树 | 需要探索的复杂问题 | 高 | 困难任务上 +50-70% |
| 由简到繁 | 带有子问题的多步骤问题 | 中 | +30-80% |
| ReAct | 需要外部信息的任务 | 中 | +15-35% |
| PAL | 数学或计算问题 | 中 | +10-15% |
| 反思 | 迭代改进、从错误中学习 | 高 | +10-20% |
核心技术
1. 思维链(CoT)提示
论文:“Chain of Thought Prompting Elicits Reasoning in Large Language Models”(Wei 等,2022) 引用次数:14,255+
适用场景
- 多步骤算术题或数学应用题
- 需要逻辑推理的常识推理
- 符号推理任务
- 有展示推理过程的优质示例
工作原理
提供包含中间推理步骤的少样本示例,而不只是问答对。模型会学习生成类似的逐步推理过程。
提示词模板
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.
Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?
A: The cafeteria had 23 apples originally. They used 20 to make lunch. So they had 23 - 20 = 3. They bought 6 more apples, so they have 3 + 6 = 9. The answer is 9.
Q: [YOUR QUESTION HERE]
A:
优势
- 显著提升推理任务的准确率
- 中间步骤可解释
- 在大型模型(>100B 参数)上表现良好
局限性
- 需要精心编写优质示例
- 对较小模型效果较差
- 仍可能出现计算错误
2. 零样本思维链
论文:“Large Language Models are Zero-Shot Reasoners”(Kojima 等,2022) 引用次数:5,985+
适用场景
- 没有可用示例
- 需要快速推理
- 跨任务类型的通用推理
- 创建少样本示例前进行原型验证
工作原理
只需在提示词后附加“让我们一步一步思考”(或类似短语)。这会触发模型在没有任何示例的情况下生成推理步骤。
提示词模板
Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?
Let's think step by step.
其他触发短语:
- “让我们一步一步地推导,确保得到正确答案。”
- “让我们分解一下这个问题。”
- “让我们系统地处理这个问题。”
- “首先,让我理解一下这个问题……”
两阶段方法(更稳健)
阶段 1 - 提取推理过程:
Q: [QUESTION]
A: Let's think step by step.
阶段 2 - 提取答案:
[REASONING FROM STAGE 1]
Therefore, the answer is
优势
- 无需编写示例
- 可泛化到不同任务类型
- 易于实现
局限性
- 效果不如少样本 CoT
- 可能生成冗长或无关的推理
- 对具体措辞敏感
3. 自洽性
论文:“Self-Consistency Improves Chain of Thought Reasoning in Language Models”(Wang 等,2022) 引用次数:5,379+
适用场景
- 需要置信度的高风险决策
- 存在多种有效推理路径的问题
- 需要降低输出方差
- 验证推理是否正确
工作原理
采样多个不同的推理路径,然后通过多数投票选出最一致的答案。其直觉是:正确答案可以通过多种推理路径得出。
提示词模板
[Use any CoT prompt - zero-shot or few-shot]
[Generate N samples with temperature > 0]
[Extract final answers from each sample]
[Return the most frequent answer (majority vote)]
实现示例
def self_consistency(prompt, n_samples=5, temperature=0.7):
answers = []
for _ in range(n_samples):
response = llm.generate(prompt, temperature=temperature)
answer = extract_answer(response)
answers.append(answer)
# Majority vote
return Counter(answers).most_common(1)[0][0]
优势
- 相较于单路径 CoT,准确率显著提升
- 提供置信度指标(一致程度)
- 可改善各类任务的表现
局限性
- 计算成本更高(生成次数增加 N 倍)
- 需要可提取的离散答案
- 超过约 10-20 个样本后,收益递减
4. 思维树(ToT)
论文:《Tree of Thoughts: Deliberate Problem Solving with Large Language Models》(Yao 等,2023) 引用次数:3,026+
适用场景
- 需要探索/回溯的复杂问题
- 初始决策至关重要的任务
- 创意问题求解(写作、谜题)
- 单独使用 CoT 时准确率低于 50%
工作原理
将 CoT 泛化为树结构,其中每个节点都是一个“思维”(连贯的语言单元)。使用搜索算法(BFS/DFS)结合自我评估,探索并选择有潜力的推理路径。
提示模板
思维生成:
Given the current state:
[STATE]
Generate 3-5 possible next steps to solve this problem.
状态评估:
Evaluate if the following partial solution is:
- "sure" (definitely leads to solution)
- "maybe" (could potentially work)
- "impossible" (cannot lead to solution)
Partial solution:
[THOUGHTS SO FAR]
BFS/DFS 搜索:
def tree_of_thoughts(problem, max_depth=3, beam_width=3):
queue = [(problem, [])] # (state, thought_path)
while queue:
state, path = queue.pop(0)
if is_solved(state):
return path
# Generate candidate thoughts
thoughts = generate_thoughts(state, k=5)
# Evaluate and keep top-k
evaluated = [(t, evaluate(state, t)) for t in thoughts]
top_k = sorted(evaluated, key=lambda x: x[1])[:beam_width]
for thought, score in top_k:
if score != "impossible":
new_state = apply_thought(state, thought)
queue.append((new_state, path + [thought]))
return None
示例:24 点游戏
Problem: Use 4, 9, 10, 13 to get 24 (use +, -, *, / and each number once)
Thought 1: 13 - 9 = 4 (Now have: 4, 4, 10)
Evaluation: "maybe" - have two 4s and 10, could work
Thought 2: 10 - 4 = 6 (Now have: 4, 6, 13)
Evaluation: "maybe" - 4 * 6 = 24, need to use 13
Thought 3: 4 + 9 = 13 (Now have: 10, 13, 13)
Evaluation: "impossible" - no way to get 24 from these
优势
- 显著提升困难任务的表现(24 点游戏从 4% 提高到 74%)
- 支持回溯与探索
- 通过自我评估及早发现错误
局限性
- 计算成本显著更高
- 需要针对具体任务进行思维分解
- 实现复杂
5. 从少到多提示
论文:《Least-to-Most Prompting Enables Complex Reasoning in Large Language Models》(Zhou 等,2022) 引用次数:1,466+
适用场景
- 难度高于示例的问题
- 组合泛化任务
- 子问题明确的多步骤问题
- 符号操作和类似 SCAN 的任务
工作原理
分为两个阶段:
- 分解:将复杂问题拆解为更简单的子问题
- 依次求解:按顺序解决子问题,并利用之前的答案
提示模板
阶段 1:分解
Q: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice as old as 30 years old, how old is Kody?
To solve "Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice as old as 30 years old, how old is Kody?", we need to first solve:
- "If Mohamed is currently twice as old as 30 years old, how old is Mohamed?"
Then we need to solve:
- "Four years ago, Kody was only half as old as Mohamed. How old is Kody?"
阶段 2:依次求解
Q: If Mohamed is currently twice as old as 30 years old, how old is Mohamed?
A: Mohamed is twice as old as 30, so Mohamed is 30 * 2 = 60 years old.
Q: Four years ago, Kody was only half as old as Mohamed. Mohamed is 60 years old. How old is Kody?
A: Four years ago, Mohamed was 60 - 4 = 56 years old. Kody was half as old, so Kody was 56 / 2 = 28 years old four years ago. Therefore, Kody is currently 28 + 4 = 32 years old.
The answer is 32.
优势
- 对难度更高的问题具有出色的泛化能力
- 在组合任务上表现良好
- 显式的问题分解有助于提升可解释性
局限性
- 需要分两个阶段进行提示
- 面对新颖结构时,分解步骤可能失败
- 设置比单阶段 CoT 更复杂
6. ReAct(推理 + 行动)
论文:《ReAct: Synergizing Reasoning and Acting in Language Models》(Yao 等,2022) 引用次数:5,012+
适用场景
- 需要外部信息的任务(搜索、API)
- 交互式决策环境
- 多跳问答
- 纯推理容易导致幻觉的场景
工作原理
交替进行推理过程(“Thought”)、行动(“Action”)和观察(“Observation”)。推理有助于规划行动;行动则为推理提供新信息。
提示模板
Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?
Thought 1: I need to search Colorado orogeny, find the area that the eastern sector extends into, then find the elevation range of that area.
Action 1: Search[Colorado orogeny]
Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.
Thought 2: It does not mention the eastern sector. So I need to look up eastern sector.
Action 2: Lookup[eastern sector]
Observation 2: The eastern sector extends into the High Plains.
Thought 3: The eastern sector extends into the High Plains. I need to search High Plains and find its elevation range.
Action 3: Search[High Plains]
Observation 3: High Plains refers to one of two distinct land regions: High Plains (United States)...
Thought 4: I need to search High Plains (United States).
Action 4: Search[High Plains (United States)]
Observation 4: The High Plains are a subregion of the Great Plains... elevation of around 1,800 to 7,000 ft (550 to 2,130 m).
Thought 5: The elevation range is 1,800 to 7,000 ft.
Action 5: Finish[1,800 to 7,000 ft]
行动类型
Search[query]- 搜索信息Lookup[keyword]- 在当前上下文中查找关键词Finish[answer]- 返回最终答案
优势
- 通过依托外部知识减少幻觉
- 行动过程清晰可解释
- 通过自适应推理处理异常
局限性
- 需要与外部工具集成
- 编排更复杂
- 必须定义行动空间
7. PAL(程序辅助语言模型)
论文:《PAL: Program-aided Language Models》(Gao 等,2022) 引用次数:608+
适用场景
- 数学/算术推理
- 需要精确计算的问题
- 符号操作
- CoT 容易出现计算错误的场景
工作原理
生成代码(通常为 Python),而非自然语言推理。执行代码以获得答案。LLM 负责分解问题;解释器负责计算。
提示模板
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
# solution in Python:
def solution():
"""Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?"""
tennis_balls_initial = 5
bought_cans = 2
tennis_balls_per_can = 3
tennis_balls_bought = bought_cans * tennis_balls_per_can
tennis_balls_total = tennis_balls_initial + tennis_balls_bought
return tennis_balls_total
Q: The bakers at the Beverly Hills Bakery baked 200 loaves of bread on Monday morning. They sold 93 loaves in the morning and 39 loaves in the afternoon. A grocery store returned 6 unsold loaves. How many loaves of bread did they have left?
# solution in Python:
def solution():
"""The bakers baked 200 loaves. They sold 93 in morning, 39 in afternoon. A store returned 6. How many left?"""
loaves_baked = 200
loaves_sold_morning = 93
loaves_sold_afternoon = 39
loaves_returned = 6
loaves_left = loaves_baked - loaves_sold_morning - loaves_sold_afternoon + loaves_returned
return loaves_left
优势
- 消除算术错误
- 清晰的变量命名有助于提升可解释性
- 利用代码执行进行验证
局限性
- 需要代码解释器
- 不适用于非计算型推理
- 模型必须生成语法正确的代码
8. Auto-CoT
论文:“Automatic Chain of Thought Prompting in Large Language Models”(Zhang 等,2022 年) 引用次数:838+
适用场景
- 没有手工编写的示例
- 想要自动设置少样本 CoT
- 将 CoT 扩展到许多任务
- 零样本 CoT 不够用时
工作原理
- 按多样性对问题进行聚类
- 使用零样本 CoT 为有代表性的问题生成推理链
- 将这些自动生成的推理链用作少样本示例
提示词模板
步骤 1:生成多样化的演示示例
# Cluster questions
clusters = cluster_questions(all_questions, k=8)
# For each cluster, pick representative and generate CoT
demonstrations = []
for cluster in clusters:
question = select_representative(cluster)
reasoning = zero_shot_cot(question) # "Let's think step by step"
demonstrations.append((question, reasoning))
步骤 2:用作少样本示例
Q: [Demo question 1]
A: Let's think step by step. [Generated reasoning 1]
Q: [Demo question 2]
A: Let's think step by step. [Generated reasoning 2]
...
Q: [New question]
A: Let's think step by step.
优势
- 无需手动创建示例
- 多样性采样可提升稳健性
- 性能可媲美手动编写的 CoT
局限性
- 质量取决于零样本 CoT 的质量
- 聚类需要相似度指标
- 部分生成的推理链包含错误
9. Reflexion
论文:“Reflexion: Language Agents with Verbal Reinforcement Learning”(Shinn 等,2023 年) 引用次数:2,179+
适用场景
- 通过多次尝试进行迭代改进
- 无需微调即可从错误中学习
- 复杂的编码或决策任务
- 单次推理不足以完成任务时
工作原理
任务失败后,Agent 会生成一段口头“反思”,分析出错原因。该反思会存储在记忆中,并在后续尝试中用于避免重蹈覆辙。
提示词模板
首次尝试:
Task: [TASK DESCRIPTION]
Thought: [REASONING]
Action: [ACTION]
...
Result: [FAILURE/PARTIAL SUCCESS]
反思:
The previous attempt failed because:
1. [SPECIFIC ERROR ANALYSIS]
2. [WHAT SHOULD HAVE BEEN DONE]
3. [KEY INSIGHT FOR NEXT ATTEMPT]
Reflection: In the next attempt, I should...
后续尝试(带记忆):
Task: [TASK DESCRIPTION]
Previous reflections:
- [REFLECTION 1]
- [REFLECTION 2]
Using these insights, I will now attempt the task again.
Thought: [IMPROVED REASONING]
Action: [BETTER ACTION]
示例:代码生成
Task: Write a function to find the longest palindromic substring.
Attempt 1: [CODE WITH BUG]
Test Result: Failed on "babad" - expected "bab" or "aba", got "b"
Reflection: My solution only checked single characters. I need to:
1. Consider substrings of all lengths
2. Use expand-around-center technique for efficiency
3. Track both start position and maximum length
Attempt 2: [IMPROVED CODE USING REFLECTION]
Test Result: Passed all tests
优势
- 无需更新权重即可从错误中学习
- 在 HumanEval 上达到 91% 的成绩(超过 GPT-4 的 80%)
- 构建关于经验和见解的情景记忆
局限性
- 需要多次尝试
- 长时间会话需要进行记忆管理
- 反思质量会影响改进效果
决策矩阵:选择哪种技术
Need Examples?
/ \
No Yes
| |
Zero-shot CoT Few-shot CoT
| |
Need higher accuracy? Need computation?
/ \ |
Yes No PAL
| |
Self-Consistency Done with CoT
|
Still not enough?
/ \
Yes No
| |
Problem decomposable? Done
/ \
Yes No
| |
Least-to-Most Need exploration?
/ \
Yes No
| |
Tree of Thoughts Need external info?
/ \
Yes No
| |
ReAct Need iteration?
/ \
Yes No
| |
Reflexion Use CoT
最佳实践
1. 从简单开始
先使用零样本 CoT(“Let's think step by step”),如有需要,再逐步尝试更复杂的技术。
2. 根据任务选择技术
- 数学/逻辑:CoT、PAL、Self-Consistency
- 多跳问答:ReAct、Least-to-Most
- 创意/谜题:Tree of Thoughts
- 迭代任务:Reflexion
3. 组合使用技术
这些技术通常可以互相补充:
- ReAct + Self-Consistency:获得稳健的事实性答案
- ToT + PAL:进行复杂的计算探索
- Least-to-Most + Reflexion:解决困难的多步骤问题
4. 提示词工程技巧
- 使用清晰的步骤标记(“步骤 1:”“首先,”等)
- 纳入多样化示例,覆盖边缘情况
- 在各示例中保持格式一致
- 添加验证步骤(“让我验证一下……”)
常见错误
| 错误 | 错误原因 | 修正方法 |
|---|---|---|
| 对简单查询使用 CoT | 会增加不必要的 token 和延迟 | 仅用于多步骤推理 |
| Self-Consistency 的样本过少 | 多数投票需要足够多的样本 | 至少使用 5-10 个样本 |
| 只泛泛地要求“逐步思考”,却不检查输出 | 模型可能会生成无关的推理 | 验证推理质量,而不只是确认是否有推理 |
| 不理解权衡就混用技术 | 增加计算成本,却没有收益 | 了解每种技术何时能带来价值 |
| 没有代码解释器却使用 PAL | 无法执行时,代码生成毫无用处 | 确保有可用的执行环境 |
| 在少样本 CoT 中不测试示例质量 | 示例不佳会导致推理质量差 | 验证示例能否正确解决问题 |
| 对线性问题使用 Tree of Thoughts | 会产生巨大的额外开销,却没有收益 | 仅在需要探索时使用 ToT |
参考文献
-
Wei, J. 等(2022)。“思维链提示能够引导大模型进行推理。” arXiv:2201.11903
-
Kojima, T. 等(2022)。“大模型是零样本推理器。” arXiv:2205.11916
-
Wang, X. 等(2022)。“自一致性提升语言模型的思维链推理能力。” arXiv:2203.11171
-
Yao, S. 等(2023)。“思维树:使用大模型进行审慎的问题求解。” arXiv:2305.10601
-
Zhou, D. 等(2022)。“由易到难的提示使大模型能够进行复杂推理。” arXiv:2205.10625
-
Yao, S. 等(2022)。“ReAct:在语言模型中协同推理与行动。” arXiv:2210.03629
-
Gao, L. 等(2022)。“PAL:程序辅助语言模型。” arXiv:2211.10435
-
Zhang, Z. 等(2022)。“大模型中的自动思维链提示。” arXiv:2210.03493
-
Shinn, N. 等(2023)。“Reflexion:通过语言强化学习的语言 Agent。” arXiv:2303.11366