Deliberate Problem Solving with Large Language Models - Implementation of Tree of Thoughts paper
Project description
Tree of Thoughts
Deliberate Problem Solving with Large Language Models
Architecture • Installation • Quick Start • API Reference
A Python implementation of Tree of Thoughts (ToT), a framework that frames problem solving as a search over a tree of thoughts.
Architecture
Tree of Thoughts Architecture
│
├── ToTAgent (Main Agent)
│ ├── Orchestrates the search process
│ ├── Uses thought generators and evaluators
│ └── Applies search strategies
│
├── ThoughtTree (Tree Structure)
│ ├── ThoughtNode - Individual reasoning steps
│ ├── Path tracking from root to leaf
│ └── Pruning and visualization
│
├── SearchStrategy (Traversal Methods)
│ ├── BFSStrategy - Breadth-First Search
│ ├── DFSStrategy - Depth-First Search
│ ├── BestFirstStrategy - Value-based prioritization
│ └── BeamSearchStrategy - Memory-efficient search
│
└── Types (Data Structures)
├── Problem - Task to solve
├── Thought - Single reasoning step
├── Solution - Final answer with reasoning
└── SearchResult - Search statistics
Core Workflow
┌─────────────────────────────────────────────────────────────────┐
│ Tree of Thoughts Process │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Problem ──▶ Generate Thoughts ──▶ Evaluate ──▶ Select Best │
│ │ │ │ │ │
│ │ └────────────────┴──────────────┘ │
│ │ │ │
│ │ ▼ │
│ │ Check Solution? ──▶ Yes ──▶ Return Answer │
│ │ │ │
│ │ No │
│ │ │ │
│ └───────────────────────┴──▶ Expand Tree ──▶ Repeat │
│ │
└─────────────────────────────────────────────────────────────────┘
Installation
pip install tree-of-thoughts
Quick Start
from tot import ToTAgent, Problem, BestFirstStrategy
# Define thought generator (typically uses an LLM)
def generate_thoughts(state: str, n: int) -> list:
# Generate n possible next thoughts
# response = llm.generate(f"Generate {n} next thoughts: {state}")
return ["Thought 1", "Thought 2", "Thought 3"][:n]
# Define thought evaluator
def evaluate_thought(state: str, thought: str) -> float:
# Return value between 0-1 (higher is better)
# response = llm.generate(f"Rate this thought: {thought}")
return 0.8
# Define solution checker
def check_solution(state: str, thought: str) -> tuple:
# Return (is_solution, answer)
# Check if we've reached a valid solution
return (False, "") # or (True, "final answer")
# Create and run agent
agent = ToTAgent(
generate_thoughts,
evaluate_thought,
check_solution,
strategy=BestFirstStrategy(),
max_depth=5,
)
result = agent.solve(Problem(question="Solve this problem"))
print(f"Success: {result.success}")
print(f"Answer: {result.solution.answer if result.solution else 'Not found'}")
Search Strategies
| Strategy | Description | Best For |
|---|---|---|
| BFS | Explores all nodes at depth before going deeper | Shortest solution path |
| DFS | Explores as deep as possible before backtracking | Quick any solution |
| Best-First | Prioritizes highest-value nodes | Quality solutions |
| Beam Search | Keeps only top-k candidates per level | Memory efficiency |
CLI Usage
# Run with different strategies
tot run "What is 15 * 17?" --strategy best
tot run "Solve this puzzle" --strategy bfs
# Interactive mode
tot interactive --strategy dfs
# Export trajectory
tot run "Question" --export result.json
API Reference
ToTAgent
| Parameter | Description |
|---|---|
generate_thoughts |
Function to generate next thoughts |
evaluate_thought |
Function to score thoughts (0-1) |
check_solution |
Function to check if solved |
strategy |
Search strategy (default: BestFirst) |
max_depth |
Maximum tree depth |
max_nodes |
Maximum nodes to explore |
branching_factor |
Thoughts per node |
Academic Reference
Implementation of the Tree of Thoughts paper:
Tree of Thoughts: Deliberate Problem Solving with Large Language Models
Shunyu Yao, Dian Yu, Jeffrey Zhao, Izhak Shafran, Thomas L. Griffiths, Yuan Cao, Karthik R. Narasimhan
NeurIPS 2023
@inproceedings{yao2023tree,
title={Tree of Thoughts: Deliberate Problem Solving with Large Language Models},
author={Yao, Shunyu and Yu, Dian and Zhao, Jeffrey and Shafran, Izhak and Griffiths, Thomas L and Cao, Yuan and Narasimhan, Karthik R},
booktitle={Advances in Neural Information Processing Systems (NeurIPS)},
year={2023}
}
License
MIT License
Made with ❤️ by AI Agent Research Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tree_of_thoughts_ai-0.1.0.tar.gz.
File metadata
- Download URL: tree_of_thoughts_ai-0.1.0.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9ccee6ad9771f29524fd9c36d0546d0cfc156da405c127c4d5efede25ef6822
|
|
| MD5 |
ca7ddb232ded3c2f5175fbf44d6598ae
|
|
| BLAKE2b-256 |
7a3d2f40388bd9ed88f1d2768483c65761ca7270b28d47bcdb483708430ea028
|
File details
Details for the file tree_of_thoughts_ai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tree_of_thoughts_ai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97bd7978ba5aaa065f2fdf19be50b043eec0a116a42ab35f776ead5422c54e58
|
|
| MD5 |
8795e3149247ec3912635f66c3d09860
|
|
| BLAKE2b-256 |
6d025f045d28237c4f3fc114ec995228ff0fe60d8a8fd8dfd03b325a7fb4abef
|