通过树形搜索结构探索多个推理分支并支持回溯的高级推理框架
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)
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
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