Tree of Thought Prompting

In the ever-evolving field of artificial intelligence, reasoning and problem-solving capabilities have seen remarkable advancements. One such innovation that stands out is the use of Tree of Thoughts (TOT) in AI reasoning, particularly in the realms of mathematical reasoning and writing

TOT excels in guiding the progression of thoughts, making the problem-solving process more comprehensive by incorporating various factors within a single prompt. This approach not only enhances the depth and accuracy of AI responses but also introduces a structured methodology akin to a panel discussion among experts.

In this article we will cover multiple examples of Tree of Thought prompting with the use of ChatGPT and Python (with the help of the OpenAI API).

If you don’t want to read the article or want to watch a video, there is one embedded into the article.

ChatGPT Example 1 Not Labeled

For the first example, we are going to look at creating a running plan. One of my hobbies is running ultramarathons and there are a lot of different runs/workouts I need to complete each week.Â

Due to this complexity, its a great first example to demo how TOT works.

Prompt

I’m building out a running plan this week for a 100 mile ultra marathon. The Goal is to run around 50/60 miles

Monday
This should be an easy run day

Tuesday
This needs to be a speed or tempo run

Wednesday
This should be an easy run day

Thursday
This needs to be a long run. I can only do 16 miles before work

Friday
Since I ran long yesterday I need to recover with an easy run

Saturday
This needs to be a speed or tempo run

ChatGPT Output

After sending this over to ChatGPT, I get the following response. Since it’s a bit lengthy, I’ve used 3 different images.

As you can see, by using the above prompt, we get a detailed summary of the runs I should do on a daily bases with all the criteria met.

ChatGPT Example 2 Labeled

For the second example, we are going to create a labeled version of TOT. The main prompt is labeled. As is the Branches and Sub-Prompts.

The example is based around the impact of AI on fintechs. Being that I work at a fintech on the risk and underwriting team, this would be interesting to see for idea generation.

Prompt

Main Prompt (Trunk): Explore the Use of AI and how it will impact risk and underwiritng at fintech companies

Â

Branch 1: Altered Documents
Sub-prompts:
Processing Statements
Ids

Â

Branch 2: Websites
Sub-prompts:
Template Websites Built Overnight
Duplicating Real Business Websites

ChatGPT Output

After sending this over to ChatGPT, I get the following response. Since it’s a bit lengthy, I’ve used 3 different images.

Python Example 1

Now we are going to move from using ChatGPT to Python.

First thing you’ll need to do is import in langchain, openai, langchain_openai and langchain_experimental.Â

!pip install langchain
!pip install openai
!pip install langchain_openai
!pip install langchain_experimental

Next bring in your OpenAi API key

import os
os.environ["OPENAI_API_KEY"] = ""

To execute the code, we need to following imports.

from langchain_openai import OpenAI
from langchain_openai import ChatOpenAI
from langchain.prompts.prompt import PromptTemplate
from langchain_experimental.smart_llm import SmartLLMChain

To create a prompt, we need to set up a question and then send it to a prompt template. Let’s solve a trivial math question.

hard_question = "I want you to solve for x. x^2 + 10x -75 = 0"
prompt = PromptTemplate.from_template(hard_question)

Establish a Large Language Model. In this case I’m going to use GPT-4

llm = ChatOpenAI(temperature=0, model_name="gpt-4")

To emulate a tree of thought prompt, we will be using a SmartLLMChain.

chain = SmartLLMChain(llm=llm, prompt=prompt, n_ideas=3, verbose=True)

Finally invoke the chain.

chain.invoke({})

Python Example 2

For this example, we are going to take in inputs as part of our prompt. The goal for this example is to look at utilizing a TOT to have a discussion on a baseball player and if they should make the HOF.

prompt_template = PromptTemplate.from_template(
    "3 people are going to have a discussion on {player} and if he should make the Hall of Fame. They are {person1}, {person2}, {person3}. They provide detailed reasoning"
)

Create a new chain based off the prompt template from above.

chain = SmartLLMChain(llm=llm, prompt=prompt_template, n_ideas=3, verbose=True)

Finally, invoke the chain with the 4 variables. 3 people who have the discussion and the player.

chain.invoke({'person1':"seasoned sportswriter", 'person2':"Hall of Fame Pitcher", 'person3':"Baseball Sabermetrics Voter", 'player': "Felix Hernandez"})

Leave a Reply

Your email address will not be published. Required fields are marked *