Class TreeOfThoughtsStrategy<T>
- Namespace
- AiDotNet.Reasoning.Strategies
- Assembly
- AiDotNet.dll
Implements Tree-of-Thoughts (ToT) reasoning that explores multiple reasoning paths in a tree structure.
public class TreeOfThoughtsStrategy<T> : ReasoningStrategyBase<T>, IReasoningStrategy<T>
Type Parameters
TThe numeric type used for scoring (e.g., double, float).
- Inheritance
-
TreeOfThoughtsStrategy<T>
- Implements
- Inherited Members
Remarks
For Beginners: Tree-of-Thoughts (ToT) is like exploring a maze where you can try multiple paths and backtrack if you hit a dead end. Unlike Chain-of-Thought which follows one linear path, ToT explores a tree of possibilities.
How it works:
Problem: "How can we reduce carbon emissions?"
├─ Renewable Energy (score: 0.9)
│ ├─ Solar panels on buildings (score: 0.85)
│ ├─ Wind farm expansion (score: 0.80)
│ └─ Hydroelectric upgrades (score: 0.75)
│
├─ Transportation (score: 0.85)
│ ├─ Electric vehicles (score: 0.90) ← Best path
│ └─ Public transit (score: 0.82)
│
└─ Industrial (score: 0.75)
├─ Carbon capture (score: 0.70)
└─ Process efficiency (score: 0.65)
The search algorithm explores this tree to find the best reasoning path.
Key features:
- Explores multiple paths: Not limited to one direction
- Can backtrack: If a path looks bad, try another
- Evaluation at each step: Score thoughts as you go
- Configurable search: BFS, Beam Search, or other algorithms
Compared to other strategies:
- Chain-of-Thought: Linear, one path only
- Self-Consistency: Multiple independent paths, no tree structure
- Tree-of-Thoughts: Structured exploration with evaluation and backtracking
Research basis: "Tree of Thoughts: Deliberate Problem Solving with Large Language Models" (Yao et al., 2023) showed significant improvements on planning, math, and creative tasks.
When to use:
- Complex problems with multiple viable approaches
- When you need to explore and compare alternatives
- Planning tasks with branching possibilities
- Creative problem-solving
- Strategic decision-making
Example Usage:
var chatModel = new OpenAIChatModel<double>("gpt-4");
var strategy = new TreeOfThoughtsStrategy<double>(
chatModel,
searchAlgorithm: SearchAlgorithmType.BeamSearch
);
var config = new ReasoningConfig
{
ExplorationDepth = 3, // Explore 3 levels deep
BranchingFactor = 3, // Generate 3 alternatives at each node
BeamWidth = 5 // Keep top 5 paths (for Beam Search)
};
var result = await strategy.ReasonAsync(
"Design a sustainable city transportation system",
config
);
// Result includes the best reasoning path found
Console.WriteLine(result.FinalAnswer);
Console.WriteLine($"Explored {result.Metrics["nodes_explored"]} possibilities");
Constructors
TreeOfThoughtsStrategy(IChatModel<T>, IEnumerable<ITool>?, SearchAlgorithmType)
Initializes a new instance of the TreeOfThoughtsStrategy<T> class.
public TreeOfThoughtsStrategy(IChatModel<T> chatModel, IEnumerable<ITool>? tools = null, SearchAlgorithmType searchAlgorithmType = SearchAlgorithmType.BeamSearch)
Parameters
chatModelIChatModel<T>The chat model used for reasoning.
toolsIEnumerable<ITool>Optional tools available during reasoning.
searchAlgorithmTypeSearchAlgorithmTypeThe search algorithm to use (default: BeamSearch).
Remarks
For Beginners: Creates a Tree-of-Thoughts strategy. You can customize: - The search algorithm (how to explore the tree) - The thought generator (how to create alternatives) - The thought evaluator (how to score thoughts)
If you don't specify custom components, sensible defaults are used.
Properties
Description
Gets a description of what this reasoning strategy does and when to use it.
public override string Description { get; }
Property Value
Remarks
For Beginners: This provides information about what makes this strategy unique and what types of problems it's best suited for. For example, Chain-of-Thought might describe itself as "Best for problems requiring step-by-step logical deduction."
StrategyName
Gets the name of this reasoning strategy.
public override string StrategyName { get; }
Property Value
Remarks
For Beginners: This is a human-readable name that identifies the strategy, like "Chain-of-Thought" or "Tree-of-Thoughts". It's useful for logging, debugging, or displaying to users which reasoning approach was used.
Methods
ReasonCoreAsync(string, ReasoningConfig, CancellationToken)
Core reasoning logic to be implemented by derived strategies.
protected override Task<ReasoningResult<T>> ReasonCoreAsync(string query, ReasoningConfig config, CancellationToken cancellationToken)
Parameters
querystringThe problem or question to reason about.
configReasoningConfigConfiguration options for the reasoning process.
cancellationTokenCancellationTokenToken to cancel the operation.
Returns
- Task<ReasoningResult<T>>
A reasoning result containing the answer and reasoning process.
Remarks
For Beginners: This is where the actual reasoning happens - and it's different for each strategy. Derived classes must implement this method with their specific approach: - ChainOfThoughtStrategy: Linear step-by-step reasoning - TreeOfThoughtsStrategy: Explores multiple paths in a tree - SelfConsistencyStrategy: Multiple attempts with voting
The base class handles setup, timing, and error handling; derived classes focus on implementing their unique reasoning algorithm.