|
| 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 | + |
| 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 | + |
0 commit comments