Prompt工程 高级 Tree-of-Thoughts 思维树 推理增强 Prompt工程

Tree-of-Thoughts思维树:探索式推理的前沿方法

AIEng Hub
阅读约 20 分钟

Tree-of-Thoughts思维树:探索式推理的前沿方法

传统的 Chain-of-Thought(CoT)让模型展示推理步骤,但只探索一条路径。当问题存在多个可能的解法或需要探索性思考时,Tree-of-Thoughts(ToT)通过维护多条推理路径并提供评估+回溯机制,大幅提升了复杂问题的解决能力。

一、从 CoT 到 ToT

1.1 CoT 的局限性

Chain-of-Thought 单路径推理:
问题 → 步骤1 → 步骤2 → 步骤3 → 答案
                          ↓ 如果步骤2错了
                    最终答案错误(无法回溯)

Tree-of-Thoughts 多路径探索:
                        ┌─ 路径A1 → A2 → A3
问题 → 分支点 ──→ 路径B1 → B2 → B3 ──→ 最优答案
             └─ 路径C1 → C2 → C3

1.2 核心对比

特性Chain-of-ThoughtTree-of-Thoughts提升
探索路径1条N条N倍
回溯能力支持
评估机制隐式显式评分
适用问题线性推理需要探索的问题
计算开销中-高需权衡

二、核心原理

2.1 三要素

"""
Tree-of-Thoughts 的三个核心组件:
1. 思维生成(Thought Generation)
   - 从当前状态生成下一步可能的思维
2. 状态评估(State Evaluation)  
   - 评估每个思维的价值
3. 搜索算法(Search Algorithm)
   - BFS / DFS / Best-First 等探索策略
"""

2.2 搜索策略

import copy
from typing import List, Dict, Callable
import json

class TreeOfThoughts:
    """Tree-of-Thoughts 核心实现"""
    
    def __init__(self, 
                 llm: Callable,          # LLM 调用函数
                 thought_generator: Callable,  # 生成下一步思维
                 evaluator: Callable,    # 评估函数
                 search_strategy: str = "bfs",  # bfs / dfs / best_first
                 max_branch: int = 3,    # 每步最大分支数
                 max_steps: int = 5,     # 最大深度
                 beam_width: int = 3):   # BFS 宽度
        self.llm = llm
        self.generate = thought_generator
        self.evaluate = evaluator
        self.search_strategy = search_strategy
        self.max_branch = max_branch
        self.max_steps = max_steps
        self.beam_width = beam_width
    
    def bfs_search(self, problem: str) -> List[str]:
        """
        广度优先搜索(BFS)
        同时维护 beam_width 条最优路径
        """
        # 初始化:从问题开始
        candidates = [{"path": [problem], "score": 0}]
        
        for step in range(self.max_steps):
            new_candidates = []
            
            for candidate in candidates:
                # 生成下一步思维
                thoughts = self.generate(candidate["path"], step, self.max_branch)
                
                for thought in thoughts:
                    new_path = candidate["path"] + [thought]
                    score = self.evaluate(new_path)
                    new_candidates.append({
                        "path": new_path,
                        "score": score
                    })
            
            # 选择评分最高的 beam_width 个候选
            candidates = sorted(
                new_candidates, 
                key=lambda x: x["score"], 
                reverse=True
            )[:self.beam_width]
        
        return candidates
    
    def dfs_search(self, problem: str, max_depth: int = 5) -> List[str]:
        """
        深度优先搜索(DFS)
        深入探索一条路径,必要时回溯
        """
        best_solution = None
        best_score = -float('inf')
        
        def dfs(current_path, depth):
            nonlocal best_solution, best_score
            
            if depth >= max_depth:
                score = self.evaluate(current_path)
                if score > best_score:
                    best_solution = copy.deepcopy(current_path)
                    best_score = score
                return
            
            thoughts = self.generate(current_path, depth, self.max_branch)
            
            for thought in sorted(thoughts, key=lambda t: 
                self.evaluate(current_path + [t]), reverse=True):
                
                new_path = current_path + [thought]
                
                # 剪枝:评分太低则回溯
                if self.evaluate(new_path) < -1.0:
                    continue
                
                dfs(new_path, depth + 1)
        
        dfs([problem], 0)
        return best_solution

2.3 评估函数设计

def evaluate_state(thought_path: List[str], llm) -> float:
    """
    评估当前推理路径的质量
    
    返回: -1 到 1 的分数
      > 0.5: 高置信度,路径正确
      0 ~ 0.5: 可能正确
      -0.5 ~ 0: 可疑
      < -0.5: 明显错误
    """
    
    # 方式 1: LLM 自我评估
    eval_prompt = f"""
    评估以下推理路径的质量,给出 -1 到 1 的评分:
    
    问题:{thought_path[0]}
    推理步骤:
    {'. '.join(f'步骤{i+1}: {t}' for i, t in enumerate(thought_path[1:]))}
    
    评分标准:
    - 逻辑一致性:推理是否自洽
    - 逐步合理性:每一步是否合理
    - 最终可行性:路径能否导向正确答案
    
    只输出评分数字(-1到1之间的小数):
    """
    
    response = llm(eval_prompt)
    
    try:
        score = float(response.strip())
        return max(-1.0, min(1.0, score))
    except:
        return 0.0


# 方式 2: 投票法
def evaluate_by_voting(thought_path: List[str], llm, num_votes=5) -> float:
    """
    通过多次采样取平均分,提升评估可靠性
    """
    scores = []
    for _ in range(num_votes):
        score = evaluate_state(thought_path, llm)
        scores.append(score)
    
    return sum(scores) / len(scores)

三、实战案例

3.1 24点游戏

# 使用 ToT 解决 24 点游戏
problem = "用数字 3, 3, 8, 8 得到 24"

# 思维生成模板
thought_prompt = """
问题:{problem}
当前路径:{path}

请列举 {num_branches} 种不同的下一步计算方式。
每种方式包含具体计算步骤。

格式:
1. 计算: [具体算式]
   中间结果: [值]
2. ...
"""

# BFS 搜索过程示意
"""
Step 1 (分支):
  ├─ 8 + 8 = 16, 剩余: 16, 3, 3  (评分: 0.3)
  ├─ 8 × 3 = 24, 剩余: 24, 3, 8  (评分: 0.5) ✓ 有希望
  └─ 8 / 3 ≈ 2.67, 剩余: 3, 3, 2.67 (评分: 0.2)

Step 2 (从评分最高分支继续):
  └─ 8 × 3 = 24, 剩余: 24, 3, 8
     ├─ 24 + 3 - 8 = 19  (评分: -0.2)
     ├─ 24 - 8 - 3 = 13  (评分: -0.3)
     └─ 8 / (3 - 8/3)    (评分: 0.9) ★ 接近答案!

Step 3:
  └─ 8 / (3 - 8/3) = 8 / (1/3) = 24  (评分: 1.0) ✓
"""

# 最终解答
solution = """
8 ÷ (3 - 8 ÷ 3) = 24

验证:
8 ÷ 3 ≈ 2.667
3 - 2.667 = 0.333
8 ÷ 0.333 = 24.0  ✓
"""

3.2 创意写作

# 使用 ToT 规划文章结构
problem = "写一篇关于 AI 伦理的科普文章(500字)"

# 第一步:规划框架
"""
Step 1 - 规划框架:
  ├─ 框架A: 引入→挑战→案例→建议→总结  (评分: 0.7)
  ├─ 框架B: 问题→分析→讨论→展望      (评分: 0.6)
  └─ 框架C: 故事→思考→行动→反思      (评分: 0.8) ★

Step 2 - 细化框架C:
  └─ 故事→思考→行动→反思
     ├─ 细化A: AI诊断故事→伦理困境→解决方案→思考
     ├─ 细化B: 自动驾驶故事→道德决策→监管建议→展望
     └─ 细化C: AI面试故事→偏见问题→技术方案→讨论

Step 3 - 选择最优路径:
  └─ 框架C + 细化B: 评分最高,生成全文
"""

四、ToT 变体对比

变体搜索策略评估方式适用场景计算开销
BFS ToT广度优先LLM 评分方案探索、规划
DFS ToT深度优先+回溯LLM 评分深入推理、证明
Best-First ToT优先队列价值评估优化问题
MCTS ToT蒙特卡洛树搜索模拟评估游戏、策略极高
Graph-of-Thoughts图搜索LLM 评分复杂推理网络极高

五、实际应用

5.1 代码调试

problem = "以下代码有bug,请找出并修复:\n\ndef process(items):\n    result = []\n    for i in range(len(items)):\n        if items[i] == items[i+1]:\n            result.append(items[i])\n    return result"

# ToT 搜索过程
"""
路径探索:
├─ 路径A: 索引越界 → 检查 i+1 < len → 修复 ✓
├─ 路径B: 逻辑错误 → 应该比较相邻值 → 重新理解需求
└─ 路径C: 类型错误 → items 可能是不同类型 → 需要类型检查
"""

5.2 数学证明

# ToT Prompt 模板
tot_prompt = """
请使用 Tree-of-Thoughts 方法解决以下问题:

问题:{problem}

请分步思考:

[思维分支 1]
可能性:{branch_1}
评估:{evaluation_1}

[思维分支 2]
可能性:{branch_2}
评估:{evaluation_2}

[思维分支 3]
可能性:{branch_3}
评估:{evaluation_3}

经过评估,选择最优路径:{selected_path}
最终答案:{final_answer}
"""

六、最佳实践

6.1 参数调优

参数建议值对性能的影响说明
最大分支数3-5线性增加太多分支稀释评估质量
搜索深度3-6指数增加根据问题复杂度调整
Beam Width (BFS)2-4线性增加平衡探索与利用
评估采样次数3-5线性增加提升评估可靠性

6.2 适用场景判断

✅ 适合 ToT 的问题特征:
- 有多种可能的解题路径
- 需要探索不同的策略
- 可以评估中间状态的好坏
- 问题有可验证的答案

❌ 不适合 ToT 的问题特征:
- 简单直接的问答(CoT 就够用)
- 对延迟极其敏感的场景
- 没有明确的评估标准
- 问题极其简单

总结

Tree-of-Thoughts 通过多路径探索和显式评估机制,在需要探索性推理的任务上显著优于传统 Chain-of-Thought。核心设计要点:

  1. 搜索策略决定了探索的效率:BFS 适合广泛探索,DFS 适合深入推理
  2. 评估函数是质量的保证:好的评估是有效搜索的前提
  3. 计算开销需要权衡:根据问题复杂度动态调整搜索深度和宽度