Tree of Thoughts

Research & Resources 💻 Python ⚖️ MIT 🔴 No recent commits

Tree of Thoughts (ToT) is a framework that enhances large language models' problem-solving by exploring multiple reasoning paths in a tree structure, using deliberate search and evaluation. It addresses limitations of simple chain-of-thought prompting for complex tasks. It is intended for developers and researchers working with LLMs on tasks like puzzles, creative writing, and crosswords.

✨ Key features

  • Implements ToT with BFS/DFS search algorithms
  • Supports multiple thought generation and evaluation methods
  • Includes prompts and logs for paper experiments
  • Easy to add new tasks with custom prompts
  • Provides naive IO/CoT sampling baselines

🎯 Use cases

  • Solve the Game of 24 puzzle
  • Creative writing tasks
  • Crossword puzzle solving
  • Research on LLM reasoning and planning

📦 Installation

🧰 Requirements: Requires Python, an OpenAI API key set as environment variable OPENAI_API_KEY, and internet access for API calls.

pip install tree-of-thoughts-llm

Or install from source:

git clone https://github.com/princeton-nlp/tree-of-thought-llm
cd tree-of-thought-llm
pip install -r requirements.txt
pip install -e .  # install `tot` package

🚀 Usage

import argparse
from tot.methods.bfs import solve
from tot.tasks.game24 import Game24Task

args = argparse.Namespace(backend='gpt-4', temperature=0.7, task='game24', naive_run=False, prompt_sample=None, method_generate='propose', method_evaluate='value', method_select='greedy', n_generate_sample=1, n_evaluate_sample=3, n_select_sample=5)

task = Game24Task()
ys, infos = solve(args, task, 900)
print(ys[0])

⚠️ Good to know

The repository is the official implementation for the paper and may have non-deterministic outputs; the reproduced game24 experiment achieved 69% score instead of the original 74% due to randomness.

❓ FAQ

How do I set up the OpenAI API key?

Store your OpenAI API key in the environment variable OPENAI_API_KEY, as described in the README.

What are the main algorithm options?

The code supports ToT with BFS and DFS (for crosswords), as well as naive IO/CoT sampling via the --naive_run flag.

Can I add a new task?

Yes, you need to create a task class in tot/tasks/ and task files in tot/data/, add prompts in tot/prompts/, and register the task in tot/tasks/__init__.py.

What is the difference between 'value' and 'vote' evaluation methods?

'value' evaluates states independently, while 'vote' has the model vote on states together, as used in different tasks like Game of 24 and Creative Writing.

📊 Repository

Forks🍴 627
Open issues🐛 8
Last commit🕒 Jan 16, 2025
Created📅 May 2023
Language💻 Python
License⚖️ MIT

🤖 Overview, features, install steps and FAQ were generated from the project's README on Sep 4, 2026. Always check the original source before running commands.