Class ChainOfThoughtHandler
Planning handler that implements Chain-of-Thought (CoT) prompting.
Encourages the model to reason through problems step-by-step before providing a final answer. Improves performance on arithmetic, logic, and multi-step reasoning tasks.
public sealed class ChainOfThoughtHandler : PlanningHandlerBase, IPlanningHandler
- Inheritance
-
ChainOfThoughtHandler
- Implements
- Inherited Members
Examples
Example: Create an agent with Chain-of-Thought planning
using LMKit.Model;
using LMKit.Agents;
using System;
using System.Threading.Tasks;
LM model = LM.LoadFromModelID("llama-3.2-3b");
// Build agent with Chain-of-Thought planning
Agent agent = new AgentBuilder()
.WithModel(model)
.WithPersona("Math Tutor")
.WithInstruction("Solve problems by showing your work step by step.")
.WithPlanning(PlanningStrategy.ChainOfThought)
.Build();
// Execute - model will reason through the problem
using var executor = new AgentExecutor();
var result = await executor.ExecuteAsync(
agent,
"If a train travels 120 miles in 2 hours, what is its average speed?");
Console.WriteLine($"Answer: {result.Content}");
Console.WriteLine($"Reasoning:\n{executor.PlanningContext?.GetReasoningTrace()}");
Example: Complex reasoning with Chain-of-Thought
using LMKit.Model;
using LMKit.Agents;
using System;
using System.Threading.Tasks;
LM model = LM.LoadFromModelID("llama-3.2-3b");
Agent agent = new AgentBuilder()
.WithModel(model)
.WithPersona("Logic Expert")
.WithPlanning(PlanningStrategy.ChainOfThought)
.Build();
using var executor = new AgentExecutor();
var result = await executor.ExecuteAsync(agent, @"
All roses are flowers.
Some flowers fade quickly.
Can we conclude that some roses fade quickly?");
Console.WriteLine(result.Content);
Remarks
How It Works
The handler wraps user input with instructions to "think step by step"
and extracts both the reasoning process and the final answer from the output.
Output Format
The handler looks for a "Final Answer:" marker to separate reasoning from
the answer. If not found, the entire output is treated as the answer.
Constructors
- ChainOfThoughtHandler()
Initializes a new instance of the ChainOfThoughtHandler class.
Fields
- DefaultInstruction
The default instruction added to encourage step-by-step reasoning.
- FinalAnswerMarker
The marker indicating the final answer.
Properties
- Instance
Gets the singleton instance with default settings.
- ReasoningInstruction
Gets or sets the instruction to append for step-by-step reasoning.
- RequireFinalAnswerMarker
Gets or sets a value indicating whether to require a "Final Answer:" marker.
When
true, the handler expects the model to explicitly mark its final answer. Whenfalse, the entire output is used if no marker is found.
- Strategy
Gets the planning strategy.
Methods
- PrepareInput(PlanningContext, string)
Prepares the input by appending the step-by-step reasoning instruction.
- ProcessOutput(PlanningContext, string)
Processes the model output by extracting reasoning and final answer.
- WithInstruction(string)
Creates a handler with custom reasoning instruction.
- WithRequiredMarker()
Creates a handler that requires explicit "Final Answer:" markers.