Table of Contents

Class ReasoningChain<T>

Namespace
AiDotNet.Reasoning.Models
Assembly
AiDotNet.dll

Represents a complete chain of reasoning steps from problem to solution.

public class ReasoningChain<T>

Type Parameters

T

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

Inheritance
ReasoningChain<T>
Inherited Members

Remarks

For Beginners: A reasoning chain is like showing your complete work on a problem, from start to finish. Just like in math class where you write out all your steps:

Problem: "What is 15% of 240?" Step 1: Convert percentage to decimal Step 2: Multiply Step 3: State the answer

The ReasoningChain class keeps track of all these steps together, along with scores that tell you how confident the AI is about each step. It uses a Vector to store scores efficiently, which is important for machine learning operations.

Example Usage:

var chain = new ReasoningChain<double>
{
    Query = "What is 15% of 240?",
    Steps = new List<ReasoningStep<double>>
    {
        new() { StepNumber = 1, Content = "Convert 15% to 0.15", Score = 1.0 },
        new() { StepNumber = 2, Content = "Multiply: 0.15 × 240 = 36", Score = 0.95 },
        new() { StepNumber = 3, Content = "Final answer: 36", Score = 1.0 }
    }
};

// Get all step scores as a vector
var scoreVector = chain.StepScores;
Console.WriteLine($"Average confidence: {scoreVector.Mean()}");

Constructors

ReasoningChain()

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

public ReasoningChain()

Properties

CompletedAt

When this reasoning chain was completed.

public DateTime? CompletedAt { get; set; }

Property Value

DateTime?

Remarks

For Beginners: Records when the AI finished and reached a final answer. The difference between CompletedAt and StartedAt tells you how long the reasoning took.

Duration

Total time spent on this reasoning chain.

public TimeSpan Duration { get; }

Property Value

TimeSpan

Remarks

For Beginners: This calculates how long the reasoning took. If the chain isn't completed yet, it returns the time elapsed so far.

FinalAnswer

The final answer or conclusion from this reasoning chain.

public string FinalAnswer { get; set; }

Property Value

string

Remarks

For Beginners: This is your final answer - the result of all the reasoning steps. In a math problem, this would be the number you circle at the end.

IsFullyVerified

Whether every step in this chain has been verified.

public bool IsFullyVerified { get; }

Property Value

bool

Remarks

For Beginners: This tells you if all steps have been checked and confirmed. It's like having a teacher review every line of your work rather than just the final answer.

Returns true only if ALL steps are verified; if even one step is unverified, returns false.

Metadata

Additional metadata or context for this reasoning chain.

public Dictionary<string, object> Metadata { get; set; }

Property Value

Dictionary<string, object>

Remarks

For Beginners: A flexible storage area for any extra information about this reasoning chain, such as: - Which strategy was used (Chain-of-Thought, Tree-of-Thoughts, etc.) - What tools were used - Domain-specific information - References or citations

OverallScore

Overall confidence score for the entire reasoning chain.

public T OverallScore { get; set; }

Property Value

T

Remarks

For Beginners: This is how confident the AI is about the entire solution, considering all steps together. It's often calculated as the minimum or average of all step scores, because: - A chain is only as strong as its weakest link - Low confidence in any step reduces confidence in the final answer

For example, if steps have scores [0.9, 0.8, 0.95], the overall confidence might be 0.8 (the minimum) or 0.88 (the average).

Query

The original query or problem that this reasoning chain addresses.

public string Query { get; set; }

Property Value

string

Remarks

For Beginners: This is the question or problem you're trying to solve. Everything in the reasoning chain is working toward answering this query.

StartedAt

When this reasoning chain was started.

public DateTime StartedAt { get; set; }

Property Value

DateTime

Remarks

For Beginners: Records when the AI started thinking about this problem.

StepScores

Vector of confidence scores for each step, enabling efficient ML operations.

public Vector<T> StepScores { get; }

Property Value

Vector<T>

Remarks

For Beginners: This property provides all the step scores as a Vector, which is a special data structure optimized for mathematical operations. It's like having all your test scores in a format where you can easily calculate averages, find the lowest score, or perform other statistical analyses.

The Vector is automatically generated from the steps' scores, so you don't need to manually keep it updated.

Why use a Vector instead of a regular list?

  • Vectors support mathematical operations (mean, standard deviation, etc.)
  • They're optimized for machine learning algorithms
  • They integrate with other linear algebra operations in the library

Steps

The ordered list of reasoning steps in this chain.

public List<ReasoningStep<T>> Steps { get; set; }

Property Value

List<ReasoningStep<T>>

Remarks

For Beginners: This is your complete "showing your work" - all the steps from start to finish, in order. Each step builds on the previous ones.

TotalRefinements

Total number of refinements made across all steps in this chain.

public int TotalRefinements { get; }

Property Value

int

Remarks

For Beginners: This counts how many corrections were made in total. A high number might indicate: - The problem was difficult - Initial approaches had errors that needed fixing - The reasoning required multiple iterations to get right

Lower numbers generally indicate cleaner, more straightforward reasoning.

Methods

AddStep(ReasoningStep<T>)

Adds a reasoning step to this chain.

public void AddStep(ReasoningStep<T> step)

Parameters

step ReasoningStep<T>

The step to add.

Remarks

For Beginners: This adds a new step to the end of your reasoning chain. The step number is automatically set to be one more than the current last step.

GetAverageScore()

Gets the average confidence score across all steps.

public T GetAverageScore()

Returns

T

The mean score from all steps, or default if no steps exist.

Remarks

For Beginners: This calculates the average confidence across all steps, giving you a general sense of how reliable the reasoning is overall.

GetMinimumScore()

Gets the minimum confidence score across all steps.

public T GetMinimumScore()

Returns

T

The lowest score from any step, or default if no steps exist.

Remarks

For Beginners: This finds the weakest link in your reasoning chain - the step you're least confident about. This is useful because: - Your overall confidence can't be higher than your least confident step - It helps identify which steps might need more work or verification

ToString()

Returns a formatted string representation of the entire reasoning chain.

public override string ToString()

Returns

string

A multi-line string showing the query, all steps, and final answer.

Remarks

For Beginners: This creates a readable version of the entire reasoning process, useful for displaying to users or logging for debugging.