从线性思维到图结构推理 Chain-of-Thought (CoT) 自 2022 年提出以来,已成为大模型推理的基础范式。但线性思维链的局限在于:真实世界的推理往往不是一条直线,而是包含分支、回溯和交叉的复杂网络。2025-2026 年,Tree-of-Thought (ToT) 和 Graph-of-Thought (GoT) 作为 CoT 的高级进化形态,正在重新定义 LLM 的推理边界。
一、CoT 回顾与局限 1.1 标准 CoT 范式 问题 → [思考步骤1] → [思考步骤2] → [思考步骤3] → 答案 标准 CoT 的 Prompt 模板:
请一步步思考: 1. 首先分析问题中的关键条件... 2. 然后推导中间结论... 3. 最后得出最终答案... 1.2 CoT 的核心局限 局限 描述 影响 单线性 只有一条推理路径 无法处理需要多路径探索的问题 无回溯 一旦走错无法回头 早期错误会传播到最终答案 无比较 无法对比不同推理路径 错过更优解法 固定深度 推理步骤数预设 简单问题过度思考,复杂问题思考不足 二、Tree-of-Thought (ToT) 2.1 核心思想 ToT 将推理过程建模为一棵搜索树,每个节点是一个"思维状态"(thought state),可以生成多个分支并评估:
[初始状态] / \ [思路A] [思路B] / \ / \ [A-1] [A-2] [B-1] [B-2] | | | [答案A] [答案A'] [答案B] 评估:答案B 最优 → 选择路径 B → B-1 2.2 ToT 完整实现 from typing import List, Optional, Callable from dataclasses import dataclass, field import json @dataclass class ThoughtNode: """思维树节点""" state: str # 当前思维状态描述 thought: str # 到达此状态的思考内容 parent: Optional['ThoughtNode'] = None children: List['ThoughtNode'] = field(default_factory=list) value: float = 0.0 # 评估值 0-1 depth: int = 0 visited: bool = False class TreeOfThought: """Tree-of-Thought 推理引擎""" def __init__(self, llm_client, max_depth: int = 5, branching_factor: int = 3, beam_size: int = 2): self.llm = llm_client self.max_depth = max_depth self.branching = branching_factor self.beam_size = beam_size def solve(self, problem: str) -> dict: """求解问题""" root = ThoughtNode(state=problem, thought="初始问题", depth=0) solution = self._search(root) return { 'answer': solution.thought if solution else None, 'path': self._trace_path(solution) if solution else [], 'tree_stats': { 'nodes_generated': self._count_nodes(root), 'max_depth_reached': self._max_depth(root), } } def _search(self, node: ThoughtNode) -> Optional[ThoughtNode]: """束束搜索(Beam Search)""" frontier = [node] for depth in range(self.max_depth): next_frontier = [] for current in frontier: if self._is_solution(current): return current # 生成多个思维分支 thoughts = self._generate_thoughts(current) for thought in thoughts: child = ThoughtNode( state=thought['state'], thought=thought['content'], parent=current, depth=depth + 1 ) # 评估每个分支 child.value = self._evaluate(child) current.children.append(child) next_frontier.append(child) # 保留 top-k 分支 next_frontier.sort(key=lambda n: -n.value) frontier = next_frontier[:self.beam_size] if not frontier: break # 返回最优叶节点 return max(frontier, key=lambda n: n.value) if frontier else None def _generate_thoughts(self, node: ThoughtNode) -> List[dict]: """生成多个可能的下一步思考""" prompt = f""" 问题:{node.state} 当前思考:{node.thought} 当前深度:{node.depth} 请生成 {self.branching} 个不同的下一步思考方向。 每个方向应探索不同的推理路径。 输出JSON格式: [ {{"state": "更新后的问题状态", "content": "具体思考内容"}}, ... ] """ response = self.llm.generate(prompt) return json.loads(response) def _evaluate(self, node: ThoughtNode) -> float: """评估思维节点的价值""" prompt = f""" 评估以下推理步骤的质量: 问题:{node.parent.state if node.parent else node.state} 推理步骤:{node.thought} 请从以下维度评分(0-1): 1. 逻辑正确性 2. 与问题相关性 3. 推进进度(离答案有多近) 返回平均分。 """ response = self.llm.generate(prompt) return float(response.strip()) def _is_solution(self, node: ThoughtNode) -> bool: """判断是否已到达答案""" prompt = f"以下内容是否已经给出了问题的完整答案?回答是或否。\n{node.thought}" return "是" in self.llm.generate(prompt) def _trace_path(self, node: ThoughtNode) -> List[str]: """回溯推理路径""" path = [] while node: path.append({'depth': node.depth, 'thought': node.thought, 'value': node.value}) node = node.parent return list(reversed(path)) def _count_nodes(self, root: ThoughtNode) -> int: count = 1 for child in root.children: count += self._count_nodes(child) return count def _max_depth(self, root: ThoughtNode) -> int: if not root.children: return root.depth return max(self._max_depth(c) for c in root.children) 2.3 ToT 效果对比 在 24 点游戏、创意写作、交叉词谜题等任务上的对比:
...