Tree of Thoughts (ToT)

Open in ColabOpen in GitHub

Overview

Tree of Thoughts (ToT) is a systematic problem-solving framework that enhances language models' reasoning capabilities by exploring and maintaining multiple paths of thought simultaneously.

The core concept of ToT breaks down problem-solving processes into multiple stages, where various "thoughts" are generated and evaluated at each stage, similar to how a chess player thinks several moves ahead.

Each thought represents an independent solution path that the system explores and evaluates in parallel.

In this tutorial, we run a "Make 15" game that combines four numbers to reach 15 using an LLM agent search algorithm with Tree of Thoughts (ToT).

Main steps

  1. Expand: Creative generation phase using LLM to explore all possible equation combinations

  2. Score: Validation phase that evaluates how accurately generated equations reach the target value of 15

  3. Prune: Optimization phase that selects only the best candidates to determine the starting point for the next exploration

Key Components

  • Reverse-Polish Notation(RPN) Calculator for equation evaluation

  • LangChain-based solver

  • State-based graph management

[Note] This code is substantially derived from the LangGraph Tree of Thoughts (ToT) tutorial.

Table of Contents

References


Environment Setup

Setting up your environment is the first step. See the Environment Setup guide for more details.

[Note]

  • The langchain-opentutorial is a package of easy-to-use environment setup guidance, useful functions and utilities for tutorials.

  • Check out the langchain-opentutorial for more details.

You can set API keys in a .env file or set them manually.

[Note] If you’re not using the .env file, no worries! Just enter the keys directly in the cell below, and you’re good to go.

Mathematical Equation Exploration and Evaluation Framework

Define a class that uilizes Pydantic to calculate RPN formulas.

Pydantic is a library that simplifies data validation.

Define a class to organize and store the process of solving mathematical problems, including the generated equations.

This signifies a candidate equation that requires evaluation.

This Defines the fucdamental structures that are responsible for managing and configuring the state of ToT.

LLM Configuration for Game Solution Generation

Construct a solution generation pipeline to automate the "Make 15" game using LangChain and OpenAI.

  • ChatPromptTemplate : Generate system prompts that define the rules and objectives of the game.

Game Solution Explorer

Implement a sophisticated search algorithm to solve the "Make 15" game.

A function that calculates the score of a candidate equation, evaluating how far it has reached the target value of 15.

Core Algorithm Functions.

Calculate the scores for all generated candidate equations and select the candidate equations with the highest scores.

Check if the correct answer has been found or if maximum depth has been reached to determine the next step.

Create Graph

Construct a solution exploration graph for the "Make 15" game using LangGraph.

Visualize the compiled graph structure.

png

Run Graph

Generate the number puzzles that fit a range using the Number Combination Puzzle Generator.

Find the equation to reach the number 15 by combining the numbers in the given puzzle.

Show the final results for the top three scores.

Last updated