概念定义

思维树(Tree of Thoughts, ToT)是一种将大语言模型的推理过程组织成树形结构,通过系统性探索、评估和回溯来解决需要复杂规划和搜索的问题的高级推理框架。

详细解释

思维树由普林斯顿大学和谷歌DeepMind在2023年提出,是对思维链(CoT)的重大升级。ToT不再局限于线性推理路径,而是构建了一个思维的树形结构,其中每个节点代表一个中间推理状态,边代表推理步骤。这种结构允许模型进行前瞻性探索、自我评估,并在必要时回溯到之前的状态。 ToT的核心创新在于将经典的搜索算法(如深度优先搜索DFS、广度优先搜索BFS、束搜索Beam Search)与大语言模型的生成和评估能力相结合。2024年的最新进展包括不确定性思维树(TouT)、强化学习集成的ToT控制器,以及简化的单提示ToT实现。 在需要非平凡规划的任务上,ToT展现了显著优势。例如,在”24点游戏”中,GPT-4配合CoT的成功率仅为4%,而使用ToT后成功率飙升至74%。这种巨大的性能提升证明了结构化搜索在复杂推理任务中的价值。

工作原理

思维树搜索机制

问题示例:24点游戏: 4, 5, 6, 10

第一层思维:候选方案探索

思维节点计算步骤评分状态
思维 1.14 × 6 = 240.8✅ 高分,继续探索
思维 1.210 - 6 = 40.5⚠️ 中等,可探索
思维 1.35 + 10 = 150.3⚠️ 低分,谨慎
思维 1.44 + 5 = 90.1❌ 剪枝,舍弃

第二层思维:深入分析

从思维 1.1 扩展
  • 思维 2.1:剩余 24, 5, 10 → 继续探索
  • 思维 2.2:需要 0, 5, 10 → 死胡同
从思维 1.2 扩展
  • 思维 2.3:4 × 5 = 20 → 评分 0.6

第三层:找到解决方案

  • 思维 3.1 ✅:20 + 4 = 24 → 解决!

ToT核心组件

组件功能关键特性
思维生成器候选思维生成• 生成候选思维; • 采样策略; • 多样性控制
状态评估器思维价值评估• 价值估计; • 启发式评分; • 进度判断
搜索算法路径选择• BFS/DFS/Beam; • 剪枝策略; • 回溯机制
记忆模块状态管理• 搜索历史; • 状态缓存; • 路径记录

2024年最新进展

技术创新核心特点应用价值
不确定性思维树 (TouT)• 可靠性评估机制; • 不确定性量化; • 风险感知决策高可靠性任务
RL-ToT控制器• 强化学习优化; • 自适应搜索策略; • 持续学习能力动态环境适应
简化ToT提示• 单提示实现; • PanelGPT框架; • 降低复杂度实用化部署

思维树算法流程图

问题输入

第1层:生成多个候选思维 → 评估每个思维的价值

剪枝:保留高分思维,舍弃低分思维

第2层:从优选思维扩展 → 继续评估新的思维状态

重复:深度或广度优先搜索

找到解决方案 OR 达到最大深度

  <rect width="800" height="700" fill="#f8f9fa" stroke="#e0e0e0"/>
  
  {/* 标题 */}
  <text x="400" y="30" text-anchor="middle" font-size="20" font-weight="bold" fill="#333">
    思维树搜索机制
  </text>
  
  {/* 问题节点 */}
  <g transform="translate(350, 60)">
    <circle cx="50" cy="30" r="25" fill="#2196f3" stroke="#1565c0" stroke-width="2"/>
    <text x="50" y="35" text-anchor="middle" font-size="12" fill="white" font-weight="bold">
      问题
    </text>
    <text x="50" y="70" text-anchor="middle" font-size="11" fill="#333">
      24点游戏: 4, 5, 6, 10
    </text>
  </g>
  
  {/* 第一层思维 */}
  <g transform="translate(100, 150)">
    {/* 思维1 */}
    <g transform="translate(0, 0)">
      <rect x="0" y="0" width="120" height="50" rx="8" fill="#e8f5e9" stroke="#4caf50" stroke-width="2"/>
      <text x="60" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#2e7d32">
        思维 1.1
      </text>
      <text x="60" y="35" text-anchor="middle" font-size="10" fill="#333">
        4 * 6 = 24
      </text>
      <text x="60" y="45" text-anchor="middle" font-size="9" fill="#4caf50">
        评分: 0.8
      </text>
    </g>
    
    {/* 思维2 */}
    <g transform="translate(150, 0)">
      <rect x="0" y="0" width="120" height="50" rx="8" fill="#fff3e0" stroke="#ff9800" stroke-width="2"/>
      <text x="60" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#e65100">
        思维 1.2
      </text>
      <text x="60" y="35" text-anchor="middle" font-size="10" fill="#333">
        10 - 6 = 4
      </text>
      <text x="60" y="45" text-anchor="middle" font-size="9" fill="#ff9800">
        评分: 0.5
      </text>
    </g>
    
    {/* 思维3 */}
    <g transform="translate(300, 0)">
      <rect x="0" y="0" width="120" height="50" rx="8" fill="#f3e5f5" stroke="#9c27b0" stroke-width="2"/>
      <text x="60" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#6a1b9a">
        思维 1.3
      </text>
      <text x="60" y="35" text-anchor="middle" font-size="10" fill="#333">
        5 + 10 = 15
      </text>
      <text x="60" y="45" text-anchor="middle" font-size="9" fill="#9c27b0">
        评分: 0.3
      </text>
    </g>
    
    {/* 思维4(被剪枝) */}
    <g transform="translate(450, 0)">
      <rect x="0" y="0" width="120" height="50" rx="8" fill="#ffebee" stroke="#f44336" stroke-width="1" stroke-dasharray="3,3" opacity="0.5"/>
      <text x="60" y="20" text-anchor="middle" font-size="11" fill="#c62828" opacity="0.5">
        思维 1.4
      </text>
      <text x="60" y="35" text-anchor="middle" font-size="10" fill="#666" opacity="0.5">
        4 + 5 = 9
      </text>
      <text x="60" y="45" text-anchor="middle" font-size="9" fill="#f44336" opacity="0.5">
        评分: 0.1 ✗
      </text>
    </g>
  </g>
  
  {/* 第二层思维(从思维1.1扩展) */}
  <g transform="translate(50, 250)">
    <g transform="translate(0, 0)">
      <rect x="0" y="0" width="110" height="45" rx="8" fill="#c8e6c9" stroke="#388e3c" stroke-width="2"/>
      <text x="55" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#1b5e20">
        思维 2.1
      </text>
      <text x="55" y="32" text-anchor="middle" font-size="9" fill="#333">
        24, 5, 10
      </text>
      <text x="55" y="42" text-anchor="middle" font-size="8" fill="#388e3c">
        继续探索
      </text>
    </g>
    
    <g transform="translate(130, 0)">
      <rect x="0" y="0" width="110" height="45" rx="8" fill="#ffccbc" stroke="#d84315" stroke-width="1" opacity="0.6"/>
      <text x="55" y="18" text-anchor="middle" font-size="10" fill="#bf360c" opacity="0.6">
        思维 2.2
      </text>
      <text x="55" y="32" text-anchor="middle" font-size="9" fill="#666" opacity="0.6">
        需要: 0, 5, 10
      </text>
      <text x="55" y="42" text-anchor="middle" font-size="8" fill="#d84315" opacity="0.6">
        死胡同
      </text>
    </g>
  </g>
  
  {/* 第二层思维(从思维1.2扩展) */}
  <g transform="translate(250, 250)">
    <g transform="translate(0, 0)">
      <rect x="0" y="0" width="110" height="45" rx="8" fill="#ffe0b2" stroke="#f57c00" stroke-width="2"/>
      <text x="55" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#e65100">
        思维 2.3
      </text>
      <text x="55" y="32" text-anchor="middle" font-size="9" fill="#333">
        4 * 5 = 20
      </text>
      <text x="55" y="42" text-anchor="middle" font-size="8" fill="#f57c00">
        评分: 0.6
      </text>
    </g>
  </g>
  
  {/* 第三层(解决方案) */}
  <g transform="translate(250, 320)">
    <rect x="0" y="0" width="110" height="45" rx="8" fill="#a5d6a7" stroke="#2e7d32" stroke-width="3"/>
    <text x="55" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#1b5e20">
      思维 3.1 ✓
    </text>
    <text x="55" y="32" text-anchor="middle" font-size="9" fill="#333">
      20 + 4 = 24
    </text>
    <text x="55" y="42" text-anchor="middle" font-size="8" fill="#2e7d32" font-weight="bold">
      解决!
    </text>
  </g>
  
  {/* 连接线 */}
  <defs>
    <marker id="arrowhead" markerWidth="10" markerHeight="10" refX="9" refY="3" orient="auto">
      <polygon points="0 0, 10 3, 0 6" fill="#666"/>
    </marker>
  </defs>
  
  {/* 主要路径 */}
  <path d="M 400 95 L 160 145" stroke="#4caf50" stroke-width="2" marker-end="url(#arrowhead)"/>
  <path d="M 400 95 L 310 145" stroke="#ff9800" stroke-width="2" marker-end="url(#arrowhead)"/>
  <path d="M 400 95 L 460 145" stroke="#9c27b0" stroke-width="2" marker-end="url(#arrowhead)"/>
  <path d="M 400 95 L 610 145" stroke="#f44336" stroke-width="1" stroke-dasharray="3,3" opacity="0.5"/>
  
  <path d="M 160 200 L 105 245" stroke="#388e3c" stroke-width="2" marker-end="url(#arrowhead)"/>
  <path d="M 160 200 L 235 245" stroke="#d84315" stroke-width="1" opacity="0.6"/>
  <path d="M 310 200 L 305 245" stroke="#f57c00" stroke-width="2" marker-end="url(#arrowhead)"/>
  <path d="M 305 295 L 305 315" stroke="#2e7d32" stroke-width="3" marker-end="url(#arrowhead)"/>
  
  {/* 算法组件 */}
  <g transform="translate(50, 420)">
    <rect x="0" y="0" width="700" height="120" rx="10" fill="#f5f5f5" stroke="#666" stroke-width="1"/>
    <text x="350" y="25" text-anchor="middle" font-size="14" font-weight="bold" fill="#333">
      ToT核心组件
    </text>
    
    {/* 思维生成器 */}
    <g transform="translate(20, 35)">
      <rect x="0" y="0" width="150" height="70" rx="5" fill="#e3f2fd" stroke="#2196f3"/>
      <text x="75" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#1565c0">
        思维生成器
      </text>
      <text x="75" y="35" text-anchor="middle" font-size="9" fill="#333">• 生成候选思维</text>
      <text x="75" y="48" text-anchor="middle" font-size="9" fill="#333">• 采样策略</text>
      <text x="75" y="61" text-anchor="middle" font-size="9" fill="#333">• 多样性控制</text>
    </g>
    
    {/* 状态评估器 */}
    <g transform="translate(190, 35)">
      <rect x="0" y="0" width="150" height="70" rx="5" fill="#f3e5f5" stroke="#9c27b0"/>
      <text x="75" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#6a1b9a">
        状态评估器
      </text>
      <text x="75" y="35" text-anchor="middle" font-size="9" fill="#333">• 价值估计</text>
      <text x="75" y="48" text-anchor="middle" font-size="9" fill="#333">• 启发式评分</text>
      <text x="75" y="61" text-anchor="middle" font-size="9" fill="#333">• 进度判断</text>
    </g>
    
    {/* 搜索算法 */}
    <g transform="translate(360, 35)">
      <rect x="0" y="0" width="150" height="70" rx="5" fill="#e8f5e9" stroke="#4caf50"/>
      <text x="75" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#2e7d32">
        搜索算法
      </text>
      <text x="75" y="35" text-anchor="middle" font-size="9" fill="#333">• BFS/DFS/Beam</text>
      <text x="75" y="48" text-anchor="middle" font-size="9" fill="#333">• 剪枝策略</text>
      <text x="75" y="61" text-anchor="middle" font-size="9" fill="#333">• 回溯机制</text>
    </g>
    
    {/* 记忆模块 */}
    <g transform="translate(530, 35)">
      <rect x="0" y="0" width="150" height="70" rx="5" fill="#fff3e0" stroke="#ff9800"/>
      <text x="75" y="20" text-anchor="middle" font-size="11" font-weight="bold" fill="#e65100">
        记忆模块
      </text>
      <text x="75" y="35" text-anchor="middle" font-size="9" fill="#333">• 搜索历史</text>
      <text x="75" y="48" text-anchor="middle" font-size="9" fill="#333">• 状态缓存</text>
      <text x="75" y="61" text-anchor="middle" font-size="9" fill="#333">• 路径记录</text>
    </g>
  </g>
  
  {/* 2024新进展 */}
  <g transform="translate(50, 560)">
    <rect x="0" y="0" width="700" height="100" rx="10" fill="#f0f4f8" stroke="#90a4ae" stroke-width="1"/>
    <text x="350" y="20" text-anchor="middle" font-size="13" font-weight="bold" fill="#333">
      2024年最新进展
    </text>
    
    <g transform="translate(20, 30)">
      <rect x="0" y="0" width="210" height="55" rx="5" fill="#e1f5fe" stroke="#03a9f4"/>
      <text x="105" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#01579b">
        不确定性思维树 (TouT)
      </text>
      <text x="105" y="32" text-anchor="middle" font-size="9" fill="#333">• 可靠性评估机制</text>
      <text x="105" y="44" text-anchor="middle" font-size="9" fill="#333">• 不确定性量化</text>
      <text x="105" y="56" text-anchor="middle" font-size="9" fill="#333">• 风险感知决策</text>
    </g>
    
    <g transform="translate(250, 30)">
      <rect x="0" y="0" width="210" height="55" rx="5" fill="#fce4ec" stroke="#e91e63"/>
      <text x="105" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#c2185b">
        RL-ToT控制器
      </text>
      <text x="105" y="32" text-anchor="middle" font-size="9" fill="#333">• 强化学习优化</text>
      <text x="105" y="44" text-anchor="middle" font-size="9" fill="#333">• 自适应搜索策略</text>
      <text x="105" y="56" text-anchor="middle" font-size="9" fill="#333">• 持续学习能力</text>
    </g>
    
    <g transform="translate(480, 30)">
      <rect x="0" y="0" width="190" height="55" rx="5" fill="#e8f5e9" stroke="#4caf50"/>
      <text x="95" y="18" text-anchor="middle" font-size="10" font-weight="bold" fill="#2e7d32">
        简化ToT提示
      </text>
      <text x="95" y="32" text-anchor="middle" font-size="9" fill="#333">• 单提示实现</text>
      <text x="95" y="44" text-anchor="middle" font-size="9" fill="#333">• PanelGPT框架</text>
      <text x="95" y="56" text-anchor="middle" font-size="9" fill="#333">• 降低复杂度</text>
    </g>
  </g>
</svg>

思维树的核心机制:
1. **思维生成**:在每个节点生成多个候选思维
2. **状态评估**:对每个思维的价值进行评分
3. **搜索决策**:选择最有前途的分支继续探索
4. **回溯机制**:遇到死胡同时返回之前的状态

## 实际应用

### 基础实现框架

**ToT系统架构**:
```python
class TreeOfThoughts:
    def __init__(self, llm, search_algorithm="bfs", max_depth=5):
        self.llm = llm
        self.search_algorithm = search_algorithm
        self.max_depth = max_depth
        self.memory = []  # 存储搜索历史
        
    def generate_thoughts(self, state, k=4):
        """生成k个候选思维"""
        prompt = f"""
        当前状态:{state}
        请生成{k}个不同的下一步思维:
        """
        thoughts = self.llm.generate_multiple(prompt, n=k)
        return thoughts
    
    def evaluate_thought(self, thought):
        """评估思维的价值"""
        prompt = f"""
        评估以下思维的价值(0-10分):
        {thought}
        考虑:正确性、进展、可行性
        """
        score = self.llm.evaluate(prompt)
        return score
    
    def search(self, problem):
        """执行树搜索"""
        if self.search_algorithm == "bfs":
            return self.breadth_first_search(problem)
        elif self.search_algorithm == "dfs":
            return self.depth_first_search(problem)
        elif self.search_algorithm == "beam":
            return self.beam_search(problem)

具体应用示例

24点游戏求解
def solve_24_game(numbers):
    """使用ToT解决24点游戏"""
    tot = TreeOfThoughts(llm, search_algorithm="beam")
    
    initial_state = {
        "numbers": numbers,
        "operations": [],
        "target": 24
    }
    
    # 定义思维生成规则
    def generate_step(state):
        thoughts = []
        nums = state["numbers"]
        
        # 生成所有可能的运算组合
        for i, j in combinations(range(len(nums)), 2):
            for op in ['+', '-', '*', '/']:
                thought = f"{nums[i]} {op} {nums[j]}"
                thoughts.append(thought)
        
        return thoughts
    
    # 执行搜索
    solution = tot.search_with_custom_generator(
        initial_state, 
        generate_step,
        is_goal=lambda s: len(s["numbers"]) == 1 and s["numbers"][0] == 24
    )
    
    return solution

创意写作应用

def creative_writing_tot(prompt, constraints):
    """使用ToT进行创意写作"""
    
    class StoryNode:
        def __init__(self, text, coherence_score, creativity_score):
            self.text = text
            self.coherence = coherence_score
            self.creativity = creativity_score
            self.children = []
    
    def generate_story_branches(node, num_branches=3):
        """为故事节点生成多个可能的延续"""
        branches = []
        for _ in range(num_branches):
            continuation = llm.generate(
                f"继续这个故事:{node.text}\n"
                f"约束:{constraints}"
            )
            # 评估连贯性和创意性
            scores = evaluate_story_quality(continuation)
            branches.append(StoryNode(
                continuation, 
                scores['coherence'],
                scores['creativity']
            ))
        return branches
    
    # 构建故事树并选择最佳路径
    root = StoryNode(prompt, 1.0, 1.0)
    best_story = beam_search_story(root, beam_width=3, depth=5)
    
    return best_story

2024年创新应用

不确定性思维树(TouT)
class TreeOfUncertainThoughts(TreeOfThoughts):
    """集成不确定性量化的ToT"""
    
    def evaluate_with_uncertainty(self, thought):
        """评估思维并量化不确定性"""
        # 多次采样获得分布
        scores = []
        for _ in range(5):
            score = self.llm.evaluate(thought, temperature=0.7)
            scores.append(score)
        
        mean_score = np.mean(scores)
        uncertainty = np.std(scores)
        
        return {
            'score': mean_score,
            'uncertainty': uncertainty,
            'confidence': 1 / (1 + uncertainty)
        }
    
    def risk_aware_search(self, problem, risk_tolerance=0.5):
        """考虑风险的搜索策略"""
        # 在高风险决策点要求更高确定性
        # 在低风险探索阶段允许更多不确定性
        pass

性能对比

任务GPT-4 + CoTGPT-4 + ToT提升幅度
24点游戏4%74%+1750%
创意写作41分68分+65.9%
迷你填字游戏16%78%+387.5%

相关概念

延伸阅读