Skip to content

Commit 668fa01

Browse files
committed
Adds examples for Tree of Thought prompting
1 parent 323ecde commit 668fa01

File tree

20 files changed

+2624
-4
lines changed

20 files changed

+2624
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ yarn-error.log*
2929

3030
# local env files
3131
.env*.local
32+
.envrc*
3233

3334
# vercel
3435
.vercel

_internal/hero_image/marketing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ def __():
1313
@app.cell
1414
def __():
1515
# Try to keep the text simple, one or two words.
16-
text = """ComputeText"""
17-
font_name = "IBMPlexMono-Bold" # For code
18-
# font_name = "IBMPlexSans-Medium" # For text
16+
text = """Tree of Thought"""
17+
# font_name = "IBMPlexMono-Bold" # For code
18+
font_name = "IBMPlexSans-Medium" # For text
1919
font_size = 250 # You may need to decrease this
2020
return font_name, font_size, text
2121

@@ -89,7 +89,7 @@ def __(mo):
8989
- high altitude drone footage birds eye view of mountain range with clouds and crashing turbulent ocean waves at sunset
9090
9191
{gen_prompt}
92-
92+
9393
#### Alternate prompts
9494
- highly detailed anime scene miyazaki ghost in the shell bright colors
9595
- highly detailed art deco illustration

poetry.lock

Lines changed: 925 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[tool.poetry]
2+
name = "substrate-examples"
3+
version = "0.1.0"
4+
description = "Various examples of Substrate usage"
5+
authors = ["Substrate <[email protected]>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.10"
10+
11+
[tool.poetry.group.dev.dependencies]
12+
marimo = "^0.8.0"
13+
pillow = "^10.4.0"
14+
numpy = "^2.1.0"
15+
substrate = "^220240617.1.8"
16+
17+
[build-system]
18+
requires = ["poetry-core"]
19+
build-backend = "poetry.core.masonry.api"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Tree of Thought
2+
3+
<details>
4+
<summary>How to run this example</summary>
5+
<br/>
6+
7+
```bash
8+
# Set your API key as an environment variable.
9+
export SUBSTRATE_API_KEY=ENTER_YOUR_KEY
10+
11+
# Run the TypeScript example
12+
13+
# If using tsx:
14+
cd typescript # Navigate to the typescript example
15+
npm install # Install dependencies
16+
npx tsx example.ts # Run the example
17+
18+
# If using Deno:
19+
cd typescript
20+
deno run example.ts
21+
22+
# Run the Python example
23+
24+
# If using Poetry:
25+
cd python # Navigate to the python example
26+
poetry install # Install dependencies and build the example
27+
poetry run main # Run the example
28+
29+
# If using Rye:
30+
# Update pyproject.toml to switch to Rye.
31+
cd python
32+
rye sync
33+
rye run main
34+
```
35+
36+
</details>
37+
38+
![hero](hero.jpg)
39+
40+
## Overview
41+
42+
Tree of Thought (ToT) is an advanced prompting technique for large language models (LLMs) that enhances problem-solving capabilities by simulating a multi-step reasoning process. This approach encourages the model to:
43+
44+
1. Break down complex problems into smaller, manageable sub-problems
45+
2. Generate multiple potential solutions or "thoughts" for each sub-problem
46+
3. Evaluate and prune less promising paths
47+
4. Combine the most promising thoughts to form a coherent solution
48+
49+
By mimicking human-like reasoning, ToT allows LLMs to tackle more challenging tasks, improve accuracy, and provide more transparent decision-making processes. This technique is particularly useful for problems requiring multi-step reasoning, strategic planning, or creative problem-solving.
50+
51+
There are a few novel problems which can be solved using this technique, like solving a Sudoku puzzle, but it seems equally suited to improving LLM responses by providing a structured and transparent reasoning process. Our example asks the model to reason through a game of Hide and Seek, displaying the final reasoning behind where the LLM believes the hider to be.
52+
53+
While putting this example together, we found that the general structure can be remain while changing only the initial framing of the problem, with interesting results coming from simply changing prompt text. Try asking it to refine a short story in a specific style or to play a different kind of game!
54+
55+
## How it works
56+
57+
In our example, the tree is represented by a panel of experts at each step - we prompt the LLM for multiple responses to a given prompt, and then ask it to rank those responses and choose the best one. This can be thought of as a breadth-first approach to tree pruning, since we explore each of the next paths before moving to the next layer. This is in contrast to a depth-first approach, where we explore each path to its end before moving to the next.
58+
59+
The key to this process is the repeated process of generating multiple alternative responses to a prompt followed by synthesis of those responses. This feedback loop allows the LLM to refine its reasoning and improve its output over time.
60+
61+
![diagram](diagram.svg)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
direction: right
2+
classes: {
3+
substrate: {
4+
label: "substrate"
5+
style: {
6+
font: mono
7+
font-color: gray
8+
font-size: 20
9+
stroke: gray
10+
stroke-dash: 1
11+
fill: "transparent"
12+
border-radius: 16
13+
}
14+
}
15+
node: {
16+
style: {
17+
font: mono
18+
font-size: 24
19+
stroke-width: 2
20+
fill: transparent
21+
stroke: gray
22+
border-radius: 16
23+
stroke-dash: 1
24+
3d: true
25+
}
26+
}
27+
edge: {
28+
style: {
29+
stroke: "#000"
30+
stroke-dash: 2
31+
}
32+
}
33+
}
34+
35+
substrate.class: substrate
36+
substrate.a.class: node
37+
substrate.b.class: node
38+
substrate.c.class: node
39+
substrate.d.class: node
40+
substrate.a.label: expert a
41+
substrate.b.label: expert b
42+
substrate.c.label: expert c
43+
substrate.d.label: consensus
44+
substrate.a->substrate.d : Idea 1 { class: edge }
45+
substrate.b->substrate.d : Idea 1 { class: edge }
46+
substrate.c->substrate.d : Idea 1 { class: edge }
47+
substrate.e.class: node
48+
substrate.f.class: node
49+
substrate.g.class: node
50+
substrate.h.class: node
51+
substrate.e.label: expert a
52+
substrate.f.label: expert b
53+
substrate.g.label: expert c
54+
substrate.h.label: consensus
55+
substrate.d->substrate.e : Expert b Idea 1 { class: edge }
56+
substrate.d->substrate.f : Expert b Idea 1 { class: edge }
57+
substrate.d->substrate.g : Expert b Idea 1 { class: edge }
58+
substrate.e->substrate.h : Idea 2 { class: edge }
59+
substrate.f->substrate.h : Idea 2 { class: edge }
60+
substrate.g->substrate.h : Idea 2 { class: edge }
61+
substrate.i.class: node
62+
substrate.j.class: node
63+
substrate.k.class: node
64+
substrate.l.class: node
65+
substrate.i.label: expert a
66+
substrate.j.label: expert b
67+
substrate.k.label: expert c
68+
substrate.l.label: consensus
69+
substrate.h->substrate.i : Expert a Idea 2 { class: edge }
70+
substrate.h->substrate.j : Expert a Idea 2 { class: edge }
71+
substrate.h->substrate.k : Expert a Idea 2 { class: edge }
72+
substrate.i->substrate.l : Idea 3 { class: edge }
73+
substrate.j->substrate.l : Idea 3 { class: edge }
74+
substrate.k->substrate.l : Idea 3 { class: edge }

0 commit comments

Comments
 (0)