How to talk to LLM: ToT-method

Mikolaj Maslanka
5 min readJun 12, 2023

--

Photo by Glenn Carstens-Peters on Unsplash

Tree of Thoughts — and LLM models

Summary of the paper along with some key practical notes.

The researchers have developed a new framework for language model inference called “Tree of Thoughts” (ToT). This framework allows language models to explore coherent units of text, which they refer to as “thoughts”. These “thoughts” serve as intermediate steps towards problem-solving.

Key Practical Notes

  1. Deliberate Decision Making: ToT enables language models to perform deliberate decision-making. This is achieved by considering multiple reasoning paths and self-evaluating choices to decide the next action.
  2. Looking Ahead and Backtracking: The framework also allows for looking ahead or backtracking when necessary. This is to make global choices, which is a significant advancement in the field of language models.
  3. Enhanced Problem-Solving Abilities: The researchers found that ToT significantly enhances language models’ problem-solving abilities. This was tested on three novel tasks that required non-trivial planning or search.

How to implement ToT in practice?

The paper does not provide a step-by-step guide on implementing the Tree of Thoughts (ToT) framework, but it does give a high-level overview of how it works. Here are some key points:

  1. Deliberate Decision Making: The ToT framework allows language models to perform deliberate decision-making by considering multiple reasoning paths and self-evaluating choices to decide the next action. This is achieved by exploring coherent units of text, referred to as “thoughts”, which serve as intermediate steps towards problem-solving.
  2. Looking Ahead and Backtracking: The framework also allows for looking ahead or backtracking when necessary. This is to make global choices, which is a significant advancement in the field of language models.
  3. Task Setup: In the paper, the researchers set up tasks and used the ToT framework to solve them. For example, in the creative writing task, the input was 4 random sentences and the output should be a coherent passage with 4 paragraphs that end in the 4 input sentences respectively. The language model first generates k = 5 plans and votes for the best one, then similarly generates k = 5 passages based on the best plan then votes for the best one.
  4. Code Repository: The researchers have provided a code repository with all prompts used in their experiments. You can access it [here](https://github.com/ysymyth/tree-of-thought-llm).
  5. Evaluation: The performance of the ToT framework is evaluated based on the success rate of the tasks. For instance, in the Game of 24, while GPT-4 with chain-of-thought prompting only solved 4% of tasks, the ToT method achieved a success rate of 74%.

To implement the ToT framework, you would need to have a good understanding of language models and how they work. You would also need to be able to set up tasks and evaluate the performance of the model. The code repository provided by the researchers would be a good starting point.

A practical algorithm based on the proposed author's ToT approach

Here’s a simplified version of the ToT approach:

  1. Input: Start with an input problem that you want the language model to solve.
  2. Generate Thoughts: Generate multiple “thoughts” or plans for how to solve the problem. In the paper, the authors generate 5 different plans. Each plan represents a different reasoning path that the model could take to solve the problem.
  3. Evaluate Thoughts: Evaluate each of the generated thoughts. This could be done by having the model predict the success of each thought or by using some other evaluation metric.
  4. Select Best Thought: Select the thought that has the highest evaluation score. This becomes the model’s chosen plan for solving the problem.
  5. Execute Thought: Execute the chosen thought. This could involve generating text, performing calculations, or any other actions that are part of the thought.
  6. Evaluate Outcome: Evaluate the outcome of executing the thought. If the problem has been successfully solved, then the process ends. If not, go back to step 2 and generate new thoughts based on the current state of the problem.
  7. Backtracking: If necessary, the model can backtrack, which means it can go back to a previous state and try a different thought.

Here’s a pseudo-code representation of the above steps:

def tree_of_thoughts(problem):
while not problem.is_solved():
thoughts = generate_thoughts(problem)
evaluations = evaluate_thoughts(thoughts)
best_thought = select_best_thought(thoughts, evaluations)
outcome = execute_thought(best_thought)
if not successful(outcome):
backtrack()
return problem.solution()

In this pseudo-code:

  • generate_thoughts(problem) represents a function that generates possible solutions or plans for the given problem.
  • evaluate_thoughts(thoughts) represents a function that evaluates the generated thoughts based on some criteria.
  • select_best_thought(thoughts, evaluations) represents a function that selects the best thought based on the evaluations.
  • execute_thought(best_thought) represents a function that executes the selected thought.
  • successful(outcome) represents a function that checks if the outcome of executing the thought has solved the problem.
  • backtrack() represents a function that allows the model to go back to a previous state and try a different thought.

Applying the Tree of Thoughts

Applying the Tree of Thoughts (ToT) approach to your prompts would involve a bit of a shift in how you interact with this AI model. Instead of asking a single question or giving a single prompt, you would need to guide the model through a series of thoughts or steps to reach the desired outcome. Here’s a general way you could do it:

  1. Define the Problem: Clearly define the problem or question you want the model to solve or answer. This could be anything from asking for a summary of a complex topic to generating a creative story.
  2. Generate Thoughts: Instead of asking the question directly, start by asking the model to generate multiple possible approaches to the problem. For example, if you want a summary of a complex topic, you could ask the model to list different ways it could approach summarizing the topic.
  3. Evaluate Thoughts: Once the model has generated multiple thoughts, ask it to evaluate each one. This could involve asking the model to predict the success of each approach or asking it to explain the pros and cons of each one.
  4. Select Best Thought: Based on the evaluations, select the thought that seems most promising and ask the model to execute it. This could involve asking the model to summarize the topic using the selected approach.
  5. Evaluate Outcome: Evaluate the outcome. If the model’s response is satisfactory, then the process ends. If not, go back to step 2 and generate new thoughts based on the current state of the problem.
  6. Backtracking: If necessary, you can ask the model to backtrack, which means going back to a previous state and trying a different approach.

Example

Here’s an example of how you could apply this approach to asking for a summary of a complex topic:

  1. Define the Problem: “I want a summary of quantum physics.”
  2. Generate Thoughts: “Can you list different ways you could summarize the topic of quantum physics?”
  3. Evaluate Thoughts: “What are the pros and cons of each approach?”
  4. Select Best Thought: “Let’s go with the approach of summarizing the key principles first. Can you summarize the key principles of quantum physics?”
  5. Evaluate Outcome: If the summary is satisfactory, then the process ends. If not, go back to step 2 and generate new thoughts.
  6. Backtracking: “Let’s try a different approach. Can you summarize quantum physics by explaining its historical development?”

Meta information about the paper

Link to paper:

https://arxiv.org/abs/2305.10601

Title of the paper:

“Tree of Thoughts: A New Inference Framework for Modern Language Models”

Authors:

My links

--

--

Mikolaj Maslanka

Software engineer & data architect adept in AWS, and agile methods. Innovates from concept to solution, passionate about AI and tech trends.