CodeReasoner<T>

Models & Types · AiDotNet.Reasoning.DomainSpecific

Specialized reasoner for code-related problems including generation, debugging, and explanation.

Implements: IDomainReasoner<T>

For Beginners

CodeReasoner is like a programming tutor that thinks through code problems step-by-step, writes code, and can even test if it works.

What it does:

  • Breaks down programming problems into steps
  • Generates code with explanations
  • Can debug and fix errors
  • Explains existing code
  • Uses Tree-of-Thoughts for complex algorithms

Example workflow: Problem: “Write a function to check if a number is prime”

Step 1: “Define the problem: Check divisibility from 2 to sqrt(n)” Step 2: “Handle edge cases: numbers <= 1 are not prime” Step 3: “Implement the algorithm”

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True

Step 4: “Test with examples: is_prime(7) = True, is_prime(4) = False”

Used for benchmarks:

  • HumanEval (Python code generation)
  • MBPP (Python programming problems)
  • Code debugging tasks
  • Code explanation tasks

Constructors

Constructor Summary
CodeReasoner(IChatClient<T> chatModel, IEnumerable<IAgentTool> tools) Initializes a new instance of the CodeReasoner class.

Methods

Method Summary
Task<ReasoningResult<T>> DebugCodeAsync(string code, string error, ReasoningConfig config, CancellationToken cancellationToken) Debugs code by analyzing errors and suggesting fixes.
Task<ReasoningResult<T>> ExplainCodeAsync(string code, ReasoningConfig config, CancellationToken cancellationToken) Explains how existing code works.
Task<ReasoningResult<T>> GenerateCodeAsync(string specification, string language, ReasoningConfig config, CancellationToken cancellationToken) Generates code with step-by-step explanation.
Task<ReasoningResult<T>> SolveAsync(string problem, ReasoningConfig config, bool useTreeSearch, CancellationToken cancellationToken) Solves a code-related problem using reasoning.
string ExtractCode(string text) Extracts code blocks from markdown-formatted text.