
Tree of Thoughts (ToT) is a powerful and flexible algorithm that significantly advances model reasoning by up to 70%. This plug-and-play version allows you to connect your own models and experience superintelligence!
No complex implementations, just pass in one of these prompts to your model. Head over to prompts.txt.
"Three experts with exceptional logical thinking skills are collaboratively answering a question using the tree of thoughts method. Each expert will share their thought process in detail, taking into account the previous thoughts of others and admitting any errors. They will iteratively refine and expand upon each other's ideas, giving credit where it's due. The process continues until a conclusive answer is found. Organize the entire response in a markdown table format. The question is..."
Clone this repository:
git clone https://github.com/kyegomez/tree-of-thoughts
cd tree-of-thoughts
python3 -m pip install -r requirements.txt
cd tree_of_thoughts
Set OpenAI key in an environment file:
.env..env file as OPENAI_API_KEY='SK-YOUR KEY'.Then go to montecarlo_example.py and fill in your API key!
In the examples folder, we have other examples for Hugging Face Transformers + Hugging Face Pipelines.
Alternatively, you can use pip to install Tree of Thoughts:
pip install tree-of-thoughts
Create a Python script (e.g., example.py) and import the necessary classes:
import os
from tree_of_thoughts import OpenAILanguageModel
from tree_of_thoughts import MonteCarloTreeofThoughts
api_model = "gpt-3.5-turbo"
model = OpenAILanguageModel(api_key='api key', api_model=api_model)
# Initialize the MonteCarloTreeofThoughts class with the model
tree_of_thoughts = MonteCarloTreeofThoughts(model)
# To reproduce the same results from the Tree of Thoughts paper, or even better,
# craft a one-shot chain of thought prompt for your task below:
initial_prompt = """
Input: 2 8 8 14
Possible next steps:
2 + 8 = 10 (left: 8 10 14)
8 / 2 = 4 (left: 4 8 14)
14 + 2 = 16 (left: 8 8 16)
2 * 8 = 16 (left: 8 14 16)
8 - 2 = 6 (left: 6 8 14)
14 - 8 = 6 (left: 2 6 8)
14 / 2 = 7 (left: 7 8 8)
14 - 2 = 12 (left: 8 8 12)
Input: use 4 numbers and basic arithmetic operations (+-*/) to obtain 24 in 1 equation
Possible next steps:
"""
num_thoughts = 1
max_steps = 3
max_states = 4
pruning_threshold = 0.5
solution = tree_of_thoughts.solve(
initial_prompt=initial_prompt,
num_thoughts=num_thoughts,
max_steps=max_steps,
max_states=max_states,
pruning_threshold=pruning_threshold,
)
print(f"Solution: {solution}")
Or integrate your own custom language model:
class CustomLanguageModel(AbstractLanguageModel):
def __init__(self, model):
self.model = model
def generate_thoughts(self, state, k):
# implement the thought generation logic using self.model
pass
def evaluate_states(self, states):
# implement state evaluation logic using self.model
pass
Run the example script.
To use Tree of Thoughts with OpenAI's API, create a custom model class that inherits from AbstractLanguageModel and implements the required methods using OpenAI's API. Then, create an instance of the TreeOfThoughts class with the custom model and the desired search algorithm ('BFS' or 'DFS').
To run Hugging Face Transformers with Tree of Thoughts:
git clone https://github.com/kyegomez/tree-of-thoughts
cd tree-of-thoughts
python3 huggingfaceExample.py
from tree_of_thoughts import HuggingLanguageModel
model_name = "gpt2"
model_tokenizer = "your tokenizer"
huggingface_model = HuggingLanguageModel(model_name, model_tokenizer)
class HuggingLanguageModel(AbstractLanguageModel):
def __init__(self, model_name):
self.model = AutoModelForCausalLM.from_pretrained(model_name)
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
def generate_thoughts(self, state, k):
state_text = ' '.join(state)
prompt = f"Given the current state of reasoning: '{state_text}', generate {k} coherent thoughts to achieve the reasoning process:"
inputs = self.tokenizer(prompt, return_tensors="pt")
outputs = self.model.generate(**inputs, num_return_sequences=k)
thoughts = [self.tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
return thoughts
def evaluate_states(self, states, initial_prompt):
state_values = {}
for state in states:
state_text = ' '.join(state)
prompt = f"Given the current state of reasoning: '{state_text}', pessimistically evaluate its value as a float between 0 and 1 based on its potential to achieve {initial_prompt}"
inputs = self.tokenizer(prompt, return_tensors="pt")
outputs = self.model.generate(**inputs, num_return_sequences=1)
value_text = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
try:
value = float(value_text)
except ValueError:
value = 0 # Assign a default value if the conversion fails
state_values[state] = value
return state_values
This algorithm is still in its infancy, but its potential remains unimaginable. Let's advance the reasoning of AI together under this banner.
You can easily share this repository by clicking on the following buttons:
For Instagram, while it doesn't directly support sharing web links, you can share the screenshot of our project and the link in your caption or bio. You can download the project screenshot by clicking the image below:
We greatly appreciate any help in spreading the word about our project. Thank you for your support!
The Tree of Thoughts library supports a variety of search algorithms that can be employed for different problem-solving contexts. Here's a brief overview of each search algorithm along with their primary benefits and use-cases.
BFS explores all the nodes at the present depth before going on to the nodes at the next depth level. It is an excellent choice when the depth of the tree is relatively small, and solutions are spread out evenly.
Benefits: - It guarantees to find the shallowest goal, i.e., the solution with fewer steps. - It is a simple and straightforward algorithm for traversing trees or graphs.
Use-cases: - Ideal for problems where the depth of the tree/graph is not very large. - Useful when the goal is close to the root.
DFS explores as far as possible along each branch before backing up. It is suitable when the tree depth is significant, and solutions are located deep in the tree.
Benefits: - It uses less memory compared to BFS as it needs to store only a single path from the root to a leaf node, along with remaining unexplored sibling nodes for each node on the path. - It can explore deeper solutions that are not accessible with BFS.
Use-cases: - It is often used in simulations due to its more aggressive (deeper) search. - Ideal for searching through a big search space.
Best-First Search uses an evaluation function to decide which adjacent node is most promising and then explores. It is suitable for problems where we have some heuristic information about the distance from the current state to the goal.
Benefits: - It can provide a more efficient solution by using heuristics. - It does not explore unnecessary paths, thus saving resources.
Use-cases: - Suitable for a large dataset where the goal node's location is unknown. - Ideal for problems where some heuristic can guide the search to the goal.
A* Search finds the least-cost path from the given initial node to one goal node (out of one or more possible goals). It uses a best-first search and finds the least-cost path to a goal.
Benefits: - It is complete, optimal, optimally efficient, and uses heuristics to guide itself. - A* balances between BFS and DFS and avoids expanding paths that are already expensive.
Use-cases: - Widely used in pathfinding and graph traversal, the process of plotting an efficiently directed path between multiple points. - Suitable for games, mapping apps, and routing paths for vehicles where we need an optimal solution.
MCTS uses random sampling of the search space and uses the results to guide the search. It is best when the search space is vast and cannot be completely traversed.
Benefits: - It ca
$ claude mcp add tree-of-thoughts \
-- python -m otcore.mcp_server <graph>