Class ReasoningStrategyBase<T>
Abstract base class for reasoning strategies that solve problems through structured thinking. Provides common functionality for all reasoning approaches.
public abstract class ReasoningStrategyBase<T> : IReasoningStrategy<T>
Type Parameters
TThe numeric type used for calculations and scoring (e.g., double, float).
- Inheritance
-
ReasoningStrategyBase<T>
- Implements
- Derived
- Inherited Members
Remarks
For Beginners: This base class is like a template for creating different types of reasoning strategies. Just like AgentBase provides common functionality for all agents, ReasoningStrategyBase provides the shared foundation that all reasoning strategies need: - Managing the language model (the "brain") - Tracking tools that can be used - Recording the reasoning process - Handling configuration and timing
Specific reasoning strategies (Chain-of-Thought, Tree-of-Thoughts, etc.) inherit from this class and implement their unique reasoning logic while getting all the common features for free.
This follows the Template Method design pattern, where the base class defines the structure and derived classes fill in the specific details.
Example Usage:
// Inherit from this base class to create a custom strategy
public class MyCustomStrategy<T> : ReasoningStrategyBase<T>
{
public MyCustomStrategy(IChatModel<T> chatModel) : base(chatModel) { }
protected override async Task<ReasoningResult<T>> ReasonCoreAsync(
string query, ReasoningConfig config, CancellationToken cancellationToken)
{
// Implement your custom reasoning logic here
AppendTrace("Starting my custom reasoning approach...");
// ... your implementation ...
return result;
}
}
Constructors
ReasoningStrategyBase(IChatModel<T>, IEnumerable<ITool>?)
Initializes a new instance of the ReasoningStrategyBase<T> class.
protected ReasoningStrategyBase(IChatModel<T> chatModel, IEnumerable<ITool>? tools = null)
Parameters
chatModelIChatModel<T>The chat model used for reasoning and generating thoughts.
toolsIEnumerable<ITool>Optional tools that can be used during reasoning (calculators, search, etc.).
Remarks
For Beginners: This constructor sets up the basic infrastructure every reasoning strategy needs: - A language model (the AI that does the thinking) - Optional tools (like calculators, code interpreters, search engines) - A trace/log to record the reasoning process
Think of it like setting up a workstation: you need a brain (the model), some tools (calculator, reference books), and a notebook (the trace) to write down your thinking.
Exceptions
- ArgumentNullException
Thrown when
chatModelis null.
Properties
ChatModel
Gets the chat model used for reasoning.
protected IChatModel<T> ChatModel { get; }
Property Value
- IChatModel<T>
Remarks
For Beginners: This is the AI language model that generates thoughts and reasoning steps. It's protected so derived classes can use it, but not publicly accessible from outside.
Description
Gets a description of what this reasoning strategy does and when to use it.
public abstract 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."
ReasoningTrace
Gets the current reasoning trace.
protected string ReasoningTrace { get; }
Property Value
Remarks
For Beginners: This is a running log of the reasoning process - like a notebook where all thoughts, actions, and observations are recorded. Useful for debugging and understanding how the strategy arrived at its answer.
StrategyName
Gets the name of this reasoning strategy.
public abstract 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.
Tools
Gets the read-only list of tools available to this strategy.
protected IReadOnlyList<ITool> Tools { get; }
Property Value
Remarks
For Beginners: These are external tools (like calculators, search engines, code interpreters) that the reasoning strategy can use to help solve problems. Protected so derived classes can access them.
Methods
AppendTrace(string)
Appends a message to the reasoning trace.
protected void AppendTrace(string message)
Parameters
messagestringThe message to append.
Remarks
For Beginners: This is like writing in your reasoning notebook. Call this method whenever you want to record what's happening during reasoning. For example: - "Generating reasoning steps..." - "Evaluating thought quality: 0.85" - "Verification passed for step 3"
All these traces get included in the final result for transparency and debugging.
ClearTrace()
Clears the reasoning trace, starting fresh.
protected void ClearTrace()
Remarks
For Beginners: This erases the reasoning notebook, starting with a clean page. Called at the start of each new reasoning task to avoid mixing up traces from different problems.
ExecuteTool(string, string)
Executes a tool with the given input.
protected string ExecuteTool(string toolName, string input)
Parameters
Returns
- string
The tool's output, or an error message if execution failed.
Remarks
For Beginners: This runs a tool and returns its result. For example: - ExecuteTool("Calculator", "15 * 240") → "3600" - ExecuteTool("WebSearch", "capital of France") → "Paris"
If the tool doesn't exist or fails to execute, returns an error message instead of throwing. This keeps the reasoning process running even if one tool fails.
FindTool(string)
Finds a tool by its name.
protected ITool? FindTool(string toolName)
Parameters
toolNamestringThe name of the tool to find.
Returns
- ITool
The tool with the specified name, or null if not found.
Remarks
For Beginners: This searches through the available tools for one with a specific name. The search is case-insensitive, so "Calculator", "calculator", and "CALCULATOR" all match.
Returns null if the tool doesn't exist, so always check before using!
GetToolDescriptions()
Gets a formatted description of all available tools.
protected string GetToolDescriptions()
Returns
- string
A string describing all tools and their capabilities.
Remarks
For Beginners: Creates a text description of all available tools that can be included in prompts to the language model. This tells the AI what tools it can use.
Example output: "Available tools:
- Calculator: Performs mathematical calculations
- WebSearch: Searches the internet for information"
ReasonAsync(string, ReasoningConfig?, CancellationToken)
Applies the reasoning strategy to solve a problem or answer a query.
public Task<ReasoningResult<T>> ReasonAsync(string query, ReasoningConfig? config = null, CancellationToken cancellationToken = default)
Parameters
querystringThe problem or question to reason about.
configReasoningConfigConfiguration options for the reasoning process (optional).
cancellationTokenCancellationTokenToken to cancel the operation (optional).
Returns
- Task<ReasoningResult<T>>
A reasoning result containing the answer, reasoning chain, and metadata.
Remarks
For Beginners: This method is where the actual "thinking" happens. You provide a question or problem, and the strategy applies its specific reasoning approach to solve it.
The config parameter lets you customize how the strategy works (like setting how many steps to take, or how deep to explore). The cancellationToken allows you to stop the reasoning if it's taking too long.
The result includes not just the final answer, but also the complete reasoning process, so you can see how the AI arrived at its conclusion.
ReasonCoreAsync(string, ReasoningConfig, CancellationToken)
Core reasoning logic to be implemented by derived strategies.
protected abstract 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.
ValidateConfig(ReasoningConfig)
Validates that a reasoning configuration is valid.
protected virtual void ValidateConfig(ReasoningConfig config)
Parameters
configReasoningConfigThe configuration to validate.
Remarks
For Beginners: This checks that all configuration settings make sense. For example: - MaxSteps must be positive - Scores must be between 0 and 1 - Beam width must be positive
Call this in derived classes to validate config before using it.
Exceptions
- ArgumentException
Thrown when configuration is invalid.