Table of Contents

Class SelfConsistencyStrategy<T>

Namespace
AiDotNet.Reasoning.Strategies
Assembly
AiDotNet.dll

Implements Self-Consistency reasoning by sampling multiple reasoning paths and using majority voting.

public class SelfConsistencyStrategy<T> : ReasoningStrategyBase<T>, IReasoningStrategy<T>

Type Parameters

T

The numeric type used for scoring (e.g., double, float).

Inheritance
SelfConsistencyStrategy<T>
Implements
Inherited Members

Remarks

For Beginners: Self-Consistency is like solving a problem multiple times independently and then comparing answers. If you get the same answer most of the time, you can be confident it's correct.

How it works:

  1. Generate multiple independent reasoning chains for the same problem
  2. Each chain uses slightly different wording/approach (controlled by temperature)
  3. Extract the final answer from each chain
  4. Use majority voting to pick the most common answer

Example: Problem: "What is 15% of 240?"

Attempt 1: 15% = 0.15, then 0.15 × 240 = 36 ✓ Attempt 2: 240 ÷ 100 × 15 = 36 ✓ Attempt 3: 240 × 15/100 = 36 ✓ Attempt 4: Convert to fraction: 240 × 3/20 = 36 ✓ Attempt 5: (Error) 240 × 1.5 = 360 ✗

Majority vote: "36" appears 4 times, "360" appears 1 time → Answer: "36"

Why it works:

  • Random errors don't repeat consistently
  • Correct reasoning tends to reach the same answer
  • Filters out hallucinations and calculation mistakes

Research basis: "Self-Consistency Improves Chain of Thought Reasoning in Language Models" (Wang et al., 2022) showed significant improvements over standard CoT, especially on reasoning benchmarks.

When to use:

  • Important decisions where accuracy matters
  • Mathematical or logical reasoning
  • When you can afford multiple LLM calls
  • Problems where errors are common

Example Usage:

var chatModel = new OpenAIChatModel<double>("gpt-4");
var strategy = new SelfConsistencyStrategy<double>(chatModel);

var config = new ReasoningConfig
{
    NumSamples = 10,  // Try 10 different reasoning paths
    Temperature = 0.7  // Moderate diversity in approaches
};

var result = await strategy.ReasonAsync(
    "If a train travels 60 mph for 2.5 hours, how far does it go?",
    config
);

// Result includes all alternative chains for transparency
Console.WriteLine($"Final answer: {result.FinalAnswer}");
Console.WriteLine($"Based on {result.AlternativeChains.Count} samples");

Constructors

SelfConsistencyStrategy(IChatModel<T>, IEnumerable<ITool>?)

Initializes a new instance of the SelfConsistencyStrategy<T> class.

public SelfConsistencyStrategy(IChatModel<T> chatModel, IEnumerable<ITool>? tools = null)

Parameters

chatModel IChatModel<T>

The chat model used for reasoning.

tools IEnumerable<ITool>

Optional tools available during reasoning.

Remarks

For Beginners: Creates a Self-Consistency strategy that uses Chain-of-Thought reasoning multiple times and aggregates the results. You can choose how to aggregate: - Majority voting (default): Most common answer wins - Weighted voting: Higher confidence answers count more

Properties

Description

Gets a description of what this reasoning strategy does and when to use it.

public override string Description { get; }

Property Value

string

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

string

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

query string

The problem or question to reason about.

config ReasoningConfig

Configuration options for the reasoning process.

cancellationToken CancellationToken

Token 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.