Table of Contents

Class AiModelResult<T, TInput, TOutput>

Namespace
AiDotNet.Models.Results
Assembly
AiDotNet.dll

Partial class containing Test-Time Augmentation (TTA) prediction methods.

[Serializable]
public class AiModelResult<T, TInput, TOutput> : IFullModel<T, TInput, TOutput>, IModel<TInput, TOutput, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, TInput, TOutput>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, TInput, TOutput>>, IGradientComputable<T, TInput, TOutput>, IJitCompilable<T>

Type Parameters

T

The numeric type used for calculations, typically float or double.

TInput
TOutput
Inheritance
AiModelResult<T, TInput, TOutput>
Implements
IFullModel<T, TInput, TOutput>
IModel<TInput, TOutput, ModelMetadata<T>>
IParameterizable<T, TInput, TOutput>
ICloneable<IFullModel<T, TInput, TOutput>>
IGradientComputable<T, TInput, TOutput>
Inherited Members
Extension Methods

Remarks

This class encapsulates a trained predictive model along with all the information needed to use it for making predictions on new data. It includes the model itself, the results of the optimization process that created the model, normalization information for preprocessing input data and postprocessing predictions, and metadata about the model. The class also provides methods for serializing and deserializing the model, allowing it to be saved to and loaded from files.

For Beginners: This class represents a complete, ready-to-use predictive model.

When working with machine learning models:

  • You need to store not just the model itself, but also how to prepare data for it
  • You want to keep track of how the model was created and how well it performs
  • You need to be able to save the model and load it later

This class handles all of that by storing:

  • The actual model that makes predictions
  • Information about how the model was optimized
  • How to normalize/scale input data before making predictions
  • Metadata about the model (like feature names, creation date, etc.)

It also provides methods to:

  • Make predictions on new data
  • Save the model to a file
  • Load a model from a file

This makes it easy to train a model once and then use it many times in different applications.

Properties

AutoMLSummary

Gets the AutoML summary for this model, if AutoML was used during building.

public AutoMLRunSummary? AutoMLSummary { get; }

Property Value

AutoMLRunSummary

Remarks

This property contains a redacted summary of the AutoML search (trial outcomes and scores), and intentionally excludes hyperparameter values and other proprietary details.

For Beginners: If you enabled AutoML, this tells you how the automatic search went.

BenchmarkReport

Gets the most recent benchmark report produced for this model, if available.

public BenchmarkReport? BenchmarkReport { get; }

Property Value

BenchmarkReport

Remarks

This is populated when benchmarking is enabled through the facade (for example, AiModelBuilder.ConfigureBenchmarking(...)) or when EvaluateBenchmarksAsync(BenchmarkingOptions?, CancellationToken) is called.

For Beginners: This is the "report card" from running standardized benchmark suites.

CrossValidationResult

Gets or sets the results from cross-validation.

public CrossValidationResult<T, TInput, TOutput>? CrossValidationResult { get; }

Property Value

CrossValidationResult<T, TInput, TOutput>

Cross-validation results containing fold-by-fold performance metrics and aggregated statistics, or null if cross-validation was not performed.

Remarks

For Beginners: If cross-validation was configured during model building, this contains detailed information about how the model performed across different subsets of the training data. This helps you understand: - How consistently the model performs across different data splits - Whether the model is overfitting or underfitting - The typical performance you can expect on new, unseen data

The results include:

  • Performance metrics for each fold (R², RMSE, MAE, etc.)
  • Aggregated statistics across all folds (mean, standard deviation)
  • Feature importance scores averaged across folds
  • Timing information for training and evaluation

If this is null, cross-validation was not performed, and you should rely on the OptimizationResult for performance metrics instead.

Example usage:

if (result.CrossValidationResult != null)
{
    var avgR2 = result.CrossValidationResult.R2Stats.Mean;
    var r2StdDev = result.CrossValidationResult.R2Stats.StandardDeviation;
    Console.WriteLine($"R² = {avgR2} ± {r2StdDev}");
}

DefaultLossFunction

Gets the default loss function used by this model for gradient computation.

[JsonIgnore]
public ILossFunction<T> DefaultLossFunction { get; }

Property Value

ILossFunction<T>

Exceptions

InvalidOperationException

If Model is not initialized.

HasFewShotExampleSelector

Checks whether a few-shot example selector is configured and available for use.

public bool HasFewShotExampleSelector { get; }

Property Value

bool

True if a few-shot example selector is configured; otherwise, false.

HasPromptAnalyzer

Checks whether a prompt analyzer is configured and available for use.

public bool HasPromptAnalyzer { get; }

Property Value

bool

True if a prompt analyzer is configured; otherwise, false.

HasPromptChain

Checks whether a prompt chain is configured and available for use.

public bool HasPromptChain { get; }

Property Value

bool

True if a prompt chain is configured; otherwise, false.

HasPromptCompressor

Checks whether a prompt compressor is configured and available for use.

public bool HasPromptCompressor { get; }

Property Value

bool

True if a prompt compressor is configured; otherwise, false.

HasPromptOptimizer

Checks whether a prompt optimizer is configured and available for use.

public bool HasPromptOptimizer { get; }

Property Value

bool

True if a prompt optimizer is configured; otherwise, false.

HasPromptTemplate

Checks whether a prompt template is configured and available for use.

public bool HasPromptTemplate { get; }

Property Value

bool

True if a prompt template is configured; otherwise, false.

Remarks

For Beginners: Use this to check if you can call FormatPrompt().

Example:

if (modelResult.HasPromptTemplate)
{
    var prompt = modelResult.FormatPrompt(variables);
}
else
{
    // Use a default prompt or throw an error
}

HasTokenizer

Gets whether a tokenizer is configured for this model.

public bool HasTokenizer { get; }

Property Value

bool

ParameterCount

Gets the number of parameters in the underlying model.

public int ParameterCount { get; }

Property Value

int

ProfilingReport

Gets the profiling report captured during training and/or inference, if profiling was enabled.

public ProfileReport? ProfilingReport { get; }

Property Value

ProfileReport

Remarks

For Beginners: This helps you see where time was spent during model build and prediction.

SupportsJitCompilation

Gets whether the underlying model currently supports JIT compilation.

public bool SupportsJitCompilation { get; }

Property Value

bool

Returns true if the wrapped model implements IJitCompilable and supports JIT, false otherwise.

Remarks

This property delegates to the wrapped model's SupportsJitCompilation property if the model implements IJitCompilable. If the model does not implement this interface or does not support JIT compilation, this returns false.

For Beginners: Whether you can use JIT compilation depends on the type of model you trained.

Models that support JIT compilation (SupportsJitCompilation = true):

  • Linear regression models
  • Polynomial regression models
  • Ridge/Lasso regression models
  • Models using differentiable operations

Models that do NOT support JIT (SupportsJitCompilation = false):

  • Decision trees
  • Random forests
  • Gradient boosted trees
  • Models using discrete logic

If your model supports JIT:

  • Predictions will be 5-10x faster
  • The computation graph is compiled to optimized native code
  • You get this speedup automatically when calling Predict()

If your model doesn't support JIT:

  • Predictions still work normally
  • No JIT acceleration, but still optimized for the model type

Exceptions

InvalidOperationException

Thrown when Model is null.

Methods

Adapt(TInput, TOutput, int, double)

Quickly adapts the model to a new task using a few examples (few-shot learning).

public void Adapt(TInput supportX, TOutput supportY, int steps = -1, double learningRate = -1)

Parameters

supportX TInput

Input features for adaptation (typically 1-50 examples per class).

supportY TOutput

Target outputs for adaptation.

steps int

Number of adaptation steps (default 10 for meta-learning, 50 for supervised).

learningRate double

Learning rate for adaptation (default 0.01). Set to -1 to use config defaults.

Remarks

This method performs lightweight adaptation to quickly adjust the model to a new task or domain. It is designed for scenarios where you have limited labeled data and need fast adaptation.

For Beginners: Adapt() is like giving your model a quick "refresher course" on a new task.

When to use Adapt():

  • You have 1-50 examples of a new task
  • You need quick adjustment (seconds to minutes)
  • You want to preserve the model's general capabilities

How it works:

  • Meta-learning models: Uses the meta-learner's fast adaptation (inner loop)
  • Supervised models: Performs quick fine-tuning with small learning rate
  • With LoRA: Only adapts small adapter layers (very efficient!)
  • Without LoRA: Updates all parameters (slower but more flexible)

Examples:

  • Adapt a general image classifier to recognize a new category (5-10 images)
  • Adjust a language model to a specific writing style (10-20 examples)
  • Fine-tune a recommendation model to a new user (5-15 interactions)

Performance:

  • Meta-learning + LoRA: Fastest (milliseconds to seconds)
  • Meta-learning alone: Fast (seconds)
  • Supervised + LoRA: Moderate (seconds to minutes)
  • Supervised alone: Slower (minutes)

Exceptions

InvalidOperationException

Thrown when Model is not initialized.

ArgumentNullException

Thrown when supportX or supportY is null.

AnalyzePrompt(string)

Analyzes a prompt and returns detailed metrics about its structure and characteristics.

public PromptMetrics AnalyzePrompt(string prompt)

Parameters

prompt string

The prompt text to analyze.

Returns

PromptMetrics

A PromptMetrics object containing token count, cost estimate, complexity score, and detected patterns.

Remarks

This method uses the configured IPromptAnalyzer to examine a prompt and extract metrics such as token count, estimated cost, complexity score, variable count, example count, and detected prompt patterns.

For Beginners: Prompt analysis helps you understand and optimize your prompts.

When you analyze a prompt, you learn:

  • Token count: How many "words" the AI sees (affects cost and limits)
  • Estimated cost: How much this prompt will cost in API fees
  • Complexity score: How complicated the prompt is (0 = simple, 1 = complex)
  • Variable count: How many {placeholders} need to be filled
  • Detected patterns: What type of prompt this is (question, instruction, etc.)

Example:

string prompt = "You are a helpful assistant. Translate {text} from English to Spanish.";
var metrics = modelResult.AnalyzePrompt(prompt);

Console.WriteLine($"Tokens: {metrics.TokenCount}");           // e.g., 15
Console.WriteLine($"Estimated cost: ${metrics.EstimatedCost}"); // e.g., $0.0001
Console.WriteLine($"Complexity: {metrics.ComplexityScore}");   // e.g., 0.3
Console.WriteLine($"Variables: {metrics.VariableCount}");      // e.g., 1
Console.WriteLine($"Patterns: {string.Join(", ", metrics.DetectedPatterns)}");
// e.g., "translation, instruction"

Use this to:

  • Estimate costs before sending to AI
  • Identify overly complex prompts that might confuse the model
  • Validate that all required variables are present

Exceptions

InvalidOperationException

Thrown when no prompt analyzer is configured.

ApplyGradients(Vector<T>, T)

Applies pre-computed gradients to update the model parameters.

public void ApplyGradients(Vector<T> gradients, T learningRate)

Parameters

gradients Vector<T>

The gradient vector to apply.

learningRate T

The learning rate for the update.

Remarks

This method delegates to the underlying model's ApplyGradients implementation. Updates parameters using: θ = θ - learningRate * gradients

For Beginners: After computing gradients, this method actually updates the model's parameters by moving them in the direction that reduces error. It delegates to the wrapped model.

Exceptions

InvalidOperationException

If Model is not initialized.

BeginInferenceSession()

Begins an inference session for stateful inference features (e.g., KV-cache).

public AiModelResult<T, TInput, TOutput>.InferenceSession BeginInferenceSession()

Returns

AiModelResult<T, TInput, TOutput>.InferenceSession

Remarks

Sessions are intended for serving-style workloads where you run many sequential inference steps. A session can create multiple independent sequences, each maintaining its own state (like KV-cache).

For Beginners: Use a session when you are doing "token-by-token" inference.

Clone()

Creates a shallow copy of this AiModelResult.

public IFullModel<T, TInput, TOutput> Clone()

Returns

IFullModel<T, TInput, TOutput>

A new AiModelResult instance that is a shallow copy of this one.

Remarks

This method delegates to WithParameters to ensure consistency in how OptimizationResult is handled. The cloned instance will have a new model with the same parameters and updated OptimizationResult metadata.

CompressPrompt(string, CompressionOptions?)

Compresses a prompt to reduce its token count while preserving essential meaning.

public CompressionResult CompressPrompt(string prompt, CompressionOptions? options = null)

Parameters

prompt string

The prompt text to compress.

options CompressionOptions

Optional compression options to control the compression behavior.

Returns

CompressionResult

A CompressionResult containing the compressed prompt and compression metrics.

Remarks

This method uses the configured IPromptCompressor to reduce the token count of a prompt while attempting to preserve its essential meaning and effectiveness. Compression can help reduce API costs and fit longer content into limited context windows.

For Beginners: Prompt compression makes your prompts shorter to save money and fit limits.

AI APIs charge per token, and models have maximum context sizes. Compression helps by:

  • Removing redundant words and phrases
  • Shortening verbose explanations
  • Eliminating unnecessary whitespace
  • Using more concise phrasing

Example:

string longPrompt = @"
    Please take the following text and translate it from the English language
    to the Spanish language. Make sure to preserve the original meaning and
    tone of the text as much as possible. Here is the text: {text}";

var result = modelResult.CompressPrompt(longPrompt);

Console.WriteLine($"Original tokens: {result.OriginalTokenCount}");    // e.g., 50
Console.WriteLine($"Compressed tokens: {result.CompressedTokenCount}"); // e.g., 20
Console.WriteLine($"Saved: {result.TokensSaved} tokens ({result.CompressionRatio:P0})");
Console.WriteLine($"Compressed: {result.CompressedPrompt}");
// e.g., "Translate English to Spanish, preserving meaning and tone: {text}"

Use compression options to control behavior:

var options = new CompressionOptions
{
    TargetReduction = 0.3,      // Try to reduce by 30%
    PreserveVariables = true,   // Don't remove {placeholders}
    PreserveCodeBlocks = true   // Don't modify code examples
};
var result = modelResult.CompressPrompt(longPrompt, options);

Exceptions

InvalidOperationException

Thrown when no prompt compressor is configured.

ComputeGradients(TInput, TOutput, ILossFunction<T>?)

Computes gradients of the loss function with respect to model parameters WITHOUT updating parameters.

public Vector<T> ComputeGradients(TInput input, TOutput target, ILossFunction<T>? lossFunction = null)

Parameters

input TInput

The input data (will be normalized automatically).

target TOutput

The target/expected output (will be normalized automatically).

lossFunction ILossFunction<T>

The loss function to use. If null, uses the model's default loss function.

Returns

Vector<T>

A vector containing gradients with respect to all model parameters.

Remarks

This method normalizes input and target before computing gradients, maintaining consistency with the Predict method. Gradients are computed on normalized data and returned as-is (gradients are with respect to parameters, not outputs, so no denormalization is needed).

For Beginners: This calculates which direction to adjust the model's parameters to reduce error, without actually changing them. Input and target are automatically normalized before gradient computation, just like Predict normalizes input automatically.

Exceptions

InvalidOperationException

If Model or Normalizer is not initialized.

CreateDeploymentRuntime(string, string, string)

Creates a deployment runtime for production features like versioning, A/B testing, caching, and telemetry.

public DeploymentRuntime<T> CreateDeploymentRuntime(string modelPath, string modelName, string version)

Parameters

modelPath string

The path to the exported ONNX model file.

modelName string

The name of the model (e.g., "HousePricePredictor").

version string

The version identifier (e.g., "1.0.0").

Returns

DeploymentRuntime<T>

A deployment runtime instance.

Remarks

For Beginners: The deployment runtime provides production features: - **Model Versioning**: Manage multiple model versions and roll back if needed - **A/B Testing**: Split traffic between different model versions - **Telemetry**: Track latency, throughput, errors, and metrics - **Caching**: Keep frequently-used models in memory for faster inference

Before using this, you must first export your model to ONNX format.

Example:

// Export model to ONNX
model.ExportToOnnx("model.onnx");

// Create runtime with deployed model
var runtime = model.CreateDeploymentRuntime("model.onnx", "MyModel", "1.0.0");

// Use runtime for inference with production features
var prediction = await runtime.InferAsync("MyModel", "1.0.0", inputData);
var stats = runtime.GetModelStatistics("MyModel");

DeepCopy()

Creates a copy of this AiModelResult with deep-copied core model components.

public IFullModel<T, TInput, TOutput> DeepCopy()

Returns

IFullModel<T, TInput, TOutput>

A new AiModelResult instance.

Remarks

Deep-copied components (independent copies, mutations don't affect original):

  • Model - The underlying predictive model
  • OptimizationResult - Training results and metrics
  • NormalizationInfo - Data normalization parameters

Shallow-copied components (shared references, mutations affect both copies):

  • BiasDetector, FairnessEvaluator - Ethical AI components
  • RagRetriever, RagReranker, RagGenerator, QueryProcessors - RAG components
  • KnowledgeGraph, GraphStore, HybridGraphRetriever - Graph RAG components
  • MetaLearner, MetaTrainingResult - Meta-learning components
  • PromptTemplate, PromptChain, PromptOptimizer - Prompt engineering components
  • FewShotExampleSelector, PromptAnalyzer, PromptCompressor - Prompt engineering components
  • Tokenizer, TokenizationConfig - Tokenization components
  • AgentConfig, AgentRecommendation, ReasoningConfig - Agent/reasoning config
  • DeploymentConfiguration, InferenceOptimizationConfig - Deployment config
  • LoRAConfiguration, CrossValidationResult - Training config

The shallow-copied components are typically stateless configuration objects or services that can be safely shared. If you need independent copies of these components, you should create new instances manually before calling DeepCopy.

For Beginners: This creates a new model that can be modified independently from the original for its core prediction behavior (model weights, normalization). However, configuration objects like prompt templates are shared between the original and the copy - if you modify them, both copies will see the change.

DeepReasonAsync(string, CancellationToken)

Performs deep, thorough reasoning on a complex problem using extensive exploration and verification.

public Task<ReasoningResult<T>> DeepReasonAsync(string problem, CancellationToken cancellationToken = default)

Parameters

problem string

The complex problem to analyze.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ReasoningResult<T>>

A comprehensive reasoning result with extensive exploration.

Remarks

For Beginners: Use this for complex problems that need careful analysis. It explores multiple approaches, verifies reasoning, and provides high-confidence answers.

What it does:

  • Uses Tree-of-Thoughts to explore multiple solution paths
  • Applies verification at each step
  • Uses self-refinement to improve answers
  • Takes longer but produces more reliable results

When to use:

  • Complex multi-step problems
  • Important decisions requiring high confidence
  • Problems with multiple valid approaches
  • When you need to understand all possibilities

Example:

var result = await modelResult.DeepReasonAsync(
    "Design an algorithm to find the shortest path in a weighted graph"
);
// Result includes multiple explored approaches and verification

Exceptions

InvalidOperationException

Thrown when agent configuration is not set up.

Deserialize(byte[])

Deserializes a model from a byte array.

public void Deserialize(byte[] data)

Parameters

data byte[]

A byte array containing the serialized model.

Remarks

This method reconstructs a AiModelResult object from a serialized byte array. It reads the serialized data of each component (model, optimization results, normalization information, and metadata) and deserializes them using the appropriate methods. The model is deserialized using its model-specific deserialization method, while the other components are deserialized from JSON.

For Beginners: This method loads a model from a previously serialized byte array.

The Deserialize method:

  • Takes a byte array containing a serialized model
  • Extracts each component (model, optimization results, etc.)
  • Uses the appropriate deserialization method for each component
  • Reconstructs the complete AiModelResult object

This approach ensures:

  • Each model type is deserialized correctly using its own specific logic
  • All model parameters and settings are properly restored
  • The complete prediction pipeline (normalization, prediction, denormalization) is reconstructed

This method will throw an exception if the deserialization process fails for any component.

Exceptions

InvalidOperationException

Thrown when deserialization fails.

Detokenize(List<int>, bool)

Decodes token IDs back into text.

public string Detokenize(List<int> tokenIds, bool skipSpecialTokens = true)

Parameters

tokenIds List<int>

The token IDs to decode.

skipSpecialTokens bool

Whether to skip special tokens in the output.

Returns

string

The decoded text.

Exceptions

InvalidOperationException

Thrown when no tokenizer is configured.

EvaluateBenchmarkAsync<TScore>(IBenchmark<TScore>, int?, CancellationToken)

Evaluates a reasoning benchmark using the configured facade (prompt chain or agent reasoning).

public Task<BenchmarkResult<TScore>> EvaluateBenchmarkAsync<TScore>(IBenchmark<TScore> benchmark, int? sampleSize = null, CancellationToken cancellationToken = default)

Parameters

benchmark IBenchmark<TScore>

The benchmark to evaluate.

sampleSize int?

Optional number of problems to evaluate (null for all).

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<BenchmarkResult<TScore>>

The benchmark evaluation result.

Type Parameters

TScore

The numeric score type used by the benchmark (for example, double).

Remarks

This method hides the benchmark wiring so users don't have to manually provide a Func<string, Task<string>>. The default evaluation path is:

EvaluateBenchmarksAsync(BenchmarkingOptions?, CancellationToken)

Runs benchmark suites against this model using the unified benchmark runner.

public Task<BenchmarkReport> EvaluateBenchmarksAsync(BenchmarkingOptions? options = null, CancellationToken cancellationToken = default)

Parameters

options BenchmarkingOptions

Benchmarking options (suites, sample size, failure policy).

cancellationToken CancellationToken

Cancellation token.

Returns

Task<BenchmarkReport>

A structured benchmark report.

Remarks

This method is facade-first: users select benchmark suites via enums and receive a structured report. It avoids requiring users to manually wire up benchmark implementations.

EvaluateHumanEvalPassAtKAsync(int, int?, CancellationToken)

Evaluates the model on HumanEval using the configured dataset path (via env var) and returns a benchmark report.

public Task<BenchmarkResult<T>> EvaluateHumanEvalPassAtKAsync(int passK = 1, int? sampleSize = null, CancellationToken cancellationToken = default)

Parameters

passK int
sampleSize int?
cancellationToken CancellationToken

Returns

Task<BenchmarkResult<T>>

Remarks

This currently performs a single generation per prompt (pass@1 behavior). The passK parameter is accepted to enable forward-compatible support for multi-sample evaluation.

EvaluateProgramIoAsync(ProgramEvaluateIoRequest, CancellationToken)

Evaluates a program against input/output test cases via AiDotNet.Serving.

public Task<ProgramEvaluateIoResponse> EvaluateProgramIoAsync(ProgramEvaluateIoRequest request, CancellationToken cancellationToken = default)

Parameters

request ProgramEvaluateIoRequest
cancellationToken CancellationToken

Returns

Task<ProgramEvaluateIoResponse>

EvaluateRobustness(TInput[], TOutput[], IAdversarialAttack<T, TInput, TOutput>)

Evaluates the model's robustness against a specific adversarial attack.

public RobustnessStats<T> EvaluateRobustness(TInput[] testInputs, TOutput[] testLabels, IAdversarialAttack<T, TInput, TOutput> attack)

Parameters

testInputs TInput[]

The test inputs to evaluate on.

testLabels TOutput[]

The true labels for the test inputs.

attack IAdversarialAttack<T, TInput, TOutput>

The adversarial attack to evaluate against.

Returns

RobustnessStats<T>

Robustness metrics including clean accuracy, adversarial accuracy, and attack success rate.

Remarks

For Beginners: This method tests how well your model resists a specific type of adversarial attack. It: 1. Makes predictions on clean (unmodified) inputs 2. Generates adversarial examples using the specified attack 3. Makes predictions on the adversarial examples 4. Computes metrics comparing clean vs adversarial performance

EvaluateRobustness(TInput[], TOutput[], double)

Evaluates the model's robustness using the default attack configuration.

public RobustnessStats<T> EvaluateRobustness(TInput[] testInputs, TOutput[] testLabels, double epsilon = 0.03)

Parameters

testInputs TInput[]

The test inputs to evaluate on.

testLabels TOutput[]

The true labels for the test inputs.

epsilon double

The maximum perturbation size (default: 0.03).

Returns

RobustnessStats<T>

Robustness metrics.

Remarks

For Beginners: This is a convenience method that uses PGD (Projected Gradient Descent) attack with default settings. PGD is one of the strongest gradient-based attacks and is commonly used as a benchmark for adversarial robustness.

EvaluateRobustnessWithAutoAttack(TInput[], TOutput[], double)

Evaluates the model's robustness using AutoAttack (ensemble of diverse attacks).

public RobustnessStats<T> EvaluateRobustnessWithAutoAttack(TInput[] testInputs, TOutput[] testLabels, double epsilon = 0.03)

Parameters

testInputs TInput[]

The test inputs to evaluate on.

testLabels TOutput[]

The true labels for the test inputs.

epsilon double

The maximum perturbation size (default: 0.03).

Returns

RobustnessStats<T>

Robustness metrics.

Remarks

For Beginners: AutoAttack runs multiple different attack methods and picks the best result. This provides a more reliable robustness evaluation because if your model can resist AutoAttack, it's genuinely robust against a wide variety of attacks.

ExecuteCodeTask(CodeTaskRequestBase)

Executes a structured code task using the configured program synthesis model.

public CodeTaskResultBase ExecuteCodeTask(CodeTaskRequestBase request)

Parameters

request CodeTaskRequestBase

Returns

CodeTaskResultBase

ExecuteCodeTaskAsync(CodeTaskRequestBase, CancellationToken)

Executes a structured code task, optionally delegating to AiDotNet.Serving when configured and preferred.

public Task<CodeTaskResultBase> ExecuteCodeTaskAsync(CodeTaskRequestBase request, CancellationToken cancellationToken = default)

Parameters

request CodeTaskRequestBase
cancellationToken CancellationToken

Returns

Task<CodeTaskResultBase>

ExecuteProgramAsync(ProgramExecuteRequest, CancellationToken)

Executes a sandboxed program via AiDotNet.Serving.

public Task<ProgramExecuteResponse> ExecuteProgramAsync(ProgramExecuteRequest request, CancellationToken cancellationToken = default)

Parameters

request ProgramExecuteRequest
cancellationToken CancellationToken

Returns

Task<ProgramExecuteResponse>

ExecuteSqlAsync(SqlExecuteRequest, CancellationToken)

Executes SQL via AiDotNet.Serving.

public Task<SqlExecuteResponse> ExecuteSqlAsync(SqlExecuteRequest request, CancellationToken cancellationToken = default)

Parameters

request SqlExecuteRequest
cancellationToken CancellationToken

Returns

Task<SqlExecuteResponse>

ExportComputationGraph(List<ComputationNode<T>>)

Exports the underlying model's computation graph for JIT compilation.

public ComputationNode<T> ExportComputationGraph(List<ComputationNode<T>> inputNodes)

Parameters

inputNodes List<ComputationNode<T>>

List to populate with input computation nodes.

Returns

ComputationNode<T>

The output computation node representing the model's prediction.

Remarks

This method delegates to the wrapped model's ExportComputationGraph method if the model implements IJitCompilable and supports JIT compilation. If the model does not implement this interface or does not support JIT, this throws NotSupportedException.

For Beginners: This method creates a "recipe" of your model's calculations for JIT compilation.

If your model supports JIT (SupportsJitCompilation = true):

  • This method creates a computation graph from your model
  • The graph represents all the mathematical operations your model performs
  • The JIT compiler uses this to create fast optimized code

If your model doesn't support JIT (SupportsJitCompilation = false):

  • This method will throw an exception
  • Check SupportsJitCompilation before calling this
  • Decision trees, random forests, etc. cannot export computation graphs

You typically don't call this method directly. It's used internally by:

  • AiModelBuilder when building models with JIT enabled
  • The prediction pipeline to compile models for faster inference

Example of what happens inside:

  • Linear model: Creates graph with MatMul(X, Coefficients) + Intercept
  • Neural network: Creates graph with all layers and activations
  • Decision tree: Throws exception - cannot create computation graph

Exceptions

InvalidOperationException

Thrown when Model is null.

NotSupportedException

Thrown when the underlying model does not support JIT compilation.

ExportToCoreML(string)

Exports the model to CoreML format for deployment on Apple devices (iOS, macOS).

public void ExportToCoreML(string outputPath)

Parameters

outputPath string

The file path where the CoreML model will be saved.

Remarks

For Beginners: CoreML is Apple's machine learning framework. Use this when deploying to: - iPhone/iPad apps - macOS applications - Apple Watch apps

CoreML models are optimized for Apple Silicon and Neural Engine, providing excellent performance on Apple devices.

Example:

var model = await new AiModelBuilder<double>()
    .ConfigureExport(new ExportConfig { TargetPlatform = TargetPlatform.CoreML, Quantization = QuantizationMode.Float16 })
    .BuildAsync();
model.ExportToCoreML("model.mlmodel");

ExportToOnnx(string)

Exports the model to ONNX format for cross-platform deployment.

public void ExportToOnnx(string outputPath)

Parameters

outputPath string

The file path where the ONNX model will be saved.

Remarks

For Beginners: ONNX (Open Neural Network Exchange) is a universal format for AI models that works across different frameworks and platforms. Use this for: - Cross-platform deployment (Windows, Linux, macOS) - Cloud deployment - General-purpose production serving

The exported model will use the export configuration specified during model building, or sensible defaults if no configuration was provided.

Example:

var model = await new AiModelBuilder<double>()
    .ConfigureExport(new ExportConfig { TargetPlatform = TargetPlatform.CPU })
    .BuildAsync();
model.ExportToOnnx("model.onnx");

ExportToTFLite(string)

Exports the model to TensorFlow Lite format for mobile and edge deployment.

public void ExportToTFLite(string outputPath)

Parameters

outputPath string

The file path where the TFLite model will be saved.

Remarks

For Beginners: TensorFlow Lite is designed for mobile and edge devices. Use this when deploying to: - Android apps - Raspberry Pi and edge devices - Embedded systems - IoT devices

TFLite models are highly optimized for size and speed on resource-constrained devices.

Example:

var model = await new AiModelBuilder<double>()
    .ConfigureExport(new ExportConfig { TargetPlatform = TargetPlatform.TFLite, Quantization = QuantizationMode.Int8 })
    .BuildAsync();
model.ExportToTFLite("model.tflite");

ExportToTensorRT(string)

Exports the model to TensorRT format for high-performance inference on NVIDIA GPUs.

public void ExportToTensorRT(string outputPath)

Parameters

outputPath string

The file path where the TensorRT model will be saved.

Remarks

For Beginners: TensorRT is NVIDIA's high-performance inference engine. Use this when: - Deploying to servers with NVIDIA GPUs - Maximum inference speed is required - You need GPU-optimized inference

TensorRT provides 2-4x faster inference than ONNX on NVIDIA hardware. Requires NVIDIA GPU to run.

Example:

var model = await new AiModelBuilder<double>()
    .ConfigureExport(new ExportConfig { TargetPlatform = TargetPlatform.TensorRT, Quantization = QuantizationMode.Float16 })
    .BuildAsync();
model.ExportToTensorRT("model.trt");

FindPathInGraph(string, string)

Finds the shortest path between two nodes in the knowledge graph.

public List<string> FindPathInGraph(string startNodeId, string endNodeId)

Parameters

startNodeId string

The ID of the starting node.

endNodeId string

The ID of the target node.

Returns

List<string>

List of node IDs representing the path, or empty list if no path exists.

Remarks

For Beginners: This method finds how two entities are connected.

Example: Finding the path between "Einstein" and "Princeton University" might return: ["einstein", "worked_at_princeton", "princeton_university"]

This is useful for understanding the relationships between concepts.

Exceptions

InvalidOperationException

Thrown when knowledge graph is not configured.

FineTune(TInput, TOutput, int, TInput?, TOutput?, double)

Performs comprehensive fine-tuning on a dataset to optimize for a specific task.

public void FineTune(TInput trainX, TOutput trainY, int epochs = -1, TInput? validationX = default, TOutput? validationY = default, double learningRate = -1)

Parameters

trainX TInput

Training input features (typically 100-10,000+ examples).

trainY TOutput

Training target outputs.

epochs int

Number of training epochs (default 100 for supervised, 50 for meta-learning).

validationX TInput

Optional validation features for monitoring overfitting.

validationY TOutput

Optional validation targets.

learningRate double

Learning rate for fine-tuning (default 0.001). Set to -1 to use config defaults.

Remarks

This method performs extensive fine-tuning to optimize the model for a specific task or domain. It is designed for scenarios where you have substantial labeled data and computational resources.

For Beginners: FineTune() is like giving your model a complete "training course" on a new task.

When to use FineTune():

  • You have 100+ labeled examples
  • You can afford longer training time (minutes to hours)
  • You want to maximize performance on a specific task
  • You're okay with the model specializing (losing some generality)

How it works:

  • Meta-learning models: Extended adaptation with more steps
  • Supervised models: Standard fine-tuning with full optimization
  • With LoRA: Only trains adapter layers (efficient, preserves base model)
  • Without LoRA: Updates all parameters (full fine-tuning)

Difference from Adapt():

  • Adapt(): 1-50 examples, 10-50 steps, preserves generality
  • FineTune(): 100+ examples, 100-1000+ steps, optimizes for specific task

Examples:

  • Fine-tune a general classifier on domain-specific data (1000+ images)
  • Adapt a language model to a specific industry (10,000+ documents)
  • Specialize a recommender for a specific user segment (5,000+ interactions)

Performance:

  • With LoRA: Faster, less memory, preserves base model
  • Without LoRA: Slower, more memory, fully specialized
  • With validation: Better generalization, prevents overfitting

Exceptions

InvalidOperationException

Thrown when Model is not initialized.

ArgumentNullException

Thrown when trainX or trainY is null.

FormatPrompt(Dictionary<string, string>)

Formats a prompt using the configured prompt template.

public string FormatPrompt(Dictionary<string, string> variables)

Parameters

variables Dictionary<string, string>

A dictionary of variable names and their values to substitute into the template.

Returns

string

The formatted prompt string with all variables substituted.

Remarks

This method uses the configured IPromptTemplate to render a prompt with the provided variable values. The template defines the structure of the prompt, and this method fills in the placeholders with actual values.

For Beginners: A prompt template is like a fill-in-the-blank form for AI prompts.

Instead of writing different prompts for each use case, you define a template once with placeholders like {topic} or {language}, then fill them in as needed.

Example:

// Template: "Translate the following text from {source} to {target}: {text}"
var variables = new Dictionary<string, string>
{
    ["source"] = "English",
    ["target"] = "Spanish",
    ["text"] = "Hello, how are you?"
};

string prompt = modelResult.FormatPrompt(variables);
// Result: "Translate the following text from English to Spanish: Hello, how are you?"

Benefits:

  • Consistent prompt structure across your application
  • Easy to modify prompts without changing code
  • Reusable templates for common tasks

Exceptions

InvalidOperationException

Thrown when no prompt template is configured.

GenerateAdversarialExample(TInput, TOutput, double)

Generates an adversarial example for a given input.

public TInput GenerateAdversarialExample(TInput input, TOutput trueLabel, double epsilon = 0.03)

Parameters

input TInput

The clean input to perturb.

trueLabel TOutput

The correct label for the input.

epsilon double

The maximum perturbation size (default: 0.03).

Returns

TInput

The adversarial example.

Remarks

For Beginners: This method creates a slightly modified version of the input that is designed to fool the model. The modification is small enough to be imperceptible to humans but can cause the model to make incorrect predictions.

GenerateAnswer(string, int?, int?, Dictionary<string, object>?)

Generates a grounded answer using the configured RAG pipeline during inference.

public GroundedAnswer<T> GenerateAnswer(string query, int? topK = null, int? topKAfterRerank = null, Dictionary<string, object>? metadataFilters = null)

Parameters

query string

The question to answer.

topK int?

Number of documents to retrieve (optional).

topKAfterRerank int?

Number of documents after reranking (optional).

metadataFilters Dictionary<string, object>

Optional filters for document selection.

Returns

GroundedAnswer<T>

A grounded answer with source citations.

Remarks

For Beginners: Use this during inference to get AI-generated answers backed by your documents. The system will search your document collection, find the most relevant sources, and generate an answer with citations.

RAG must be configured via AiModelBuilder.ConfigureRetrievalAugmentedGeneration() before building the model.

Exceptions

InvalidOperationException

Thrown when RAG components are not configured.

GenerateCode(CodeGenerationRequest)

public CodeGenerationResult GenerateCode(CodeGenerationRequest request)

Parameters

request CodeGenerationRequest

Returns

CodeGenerationResult

GetActiveFeatureIndices()

Gets the indices of features that are actively used by the underlying model.

public IEnumerable<int> GetActiveFeatureIndices()

Returns

IEnumerable<int>

An enumerable of active feature indices.

Exceptions

InvalidOperationException

Thrown when the Model is not initialized.

GetCheckpointManager()

Gets the checkpoint manager for model persistence operations.

public ICheckpointManager<T, TInput, TOutput>? GetCheckpointManager()

Returns

ICheckpointManager<T, TInput, TOutput>

The checkpoint manager, or null if not configured.

Remarks

For Beginners: Use this to save model states or load previous checkpoints.

Example:

var manager = result.GetCheckpointManager();
if (manager != null)
{
    manager.SaveCheckpoint("after_finetuning", model, metrics);
}

GetExperimentInfo()

Gets experiment tracking information as a structured object.

public ExperimentInfo<T>? GetExperimentInfo()

Returns

ExperimentInfo<T>

An ExperimentInfo object containing experiment tracking data, or null if experiment tracking was not used.

Remarks

For Beginners: This provides type-safe access to experiment tracking data.

Example:

var expInfo = result.GetExperimentInfo();
if (expInfo != null)
{
    Console.WriteLine($"Experiment: {expInfo.ExperimentId}");
    Console.WriteLine($"Run: {expInfo.RunId}");

    // Log additional metrics post-training
    if (expInfo.ExperimentRun != null)
    {
        expInfo.ExperimentRun.LogMetric("production_accuracy", 0.92);
    }
}

GetExperimentRun()

Gets the experiment run associated with this model, if experiment tracking was configured.

public IExperimentRun<T>? GetExperimentRun()

Returns

IExperimentRun<T>

The experiment run, or null if experiment tracking was not used.

Remarks

For Beginners: This provides access to the training run for post-training logging.

Example:

var run = result.GetExperimentRun();
if (run != null)
{
    run.LogMetric("production_accuracy", 0.92);
    run.AddNote("Deployed to production");
}

GetExperimentTracker()

Gets the experiment tracker used during training, if configured.

public IExperimentTracker<T>? GetExperimentTracker()

Returns

IExperimentTracker<T>

The experiment tracker, or null if not configured.

Remarks

For Beginners: Use this to compare training runs or start new experiments.

Example:

var tracker = result.GetExperimentTracker();
if (tracker != null)
{
    var allRuns = tracker.ListRuns(experimentId);
}

GetFeatureImportance()

Gets the feature importance scores from the underlying model.

public Dictionary<string, T> GetFeatureImportance()

Returns

Dictionary<string, T>

A dictionary mapping feature names to importance scores.

Exceptions

InvalidOperationException

Thrown when the Model is not initialized.

GetFederatedLearningMetadata()

Gets federated learning training metadata if this model was produced via federated learning.

public FederatedLearningMetadata? GetFederatedLearningMetadata()

Returns

FederatedLearningMetadata

The federated learning metadata, or null if not available.

GetHyperparameterOptimizationResult()

Gets the hyperparameter optimization result, if optimization was used.

public HyperparameterOptimizationResult<T>? GetHyperparameterOptimizationResult()

Returns

HyperparameterOptimizationResult<T>

The optimization result containing all trials, or null if optimization was not used.

Remarks

For Beginners: Use this to analyze which hyperparameters worked best.

Example:

var hpoResult = result.GetHyperparameterOptimizationResult();
if (hpoResult != null)
{
    Console.WriteLine($"Best params: {hpoResult.BestParameters}");
}

GetHyperparameters()

Gets the hyperparameters used for training.

public Dictionary<string, object>? GetHyperparameters()

Returns

Dictionary<string, object>

A dictionary of hyperparameter names to values, or null if not tracked.

GetModelMetadata()

Gets the metadata associated with the model.

public ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData<T> object containing descriptive information about the model.

Remarks

This method returns the metadata associated with the model, which is stored in the ModelMetaData property. It is implemented to satisfy the IPredictiveModel interface, which requires a method to retrieve model metadata. The metadata includes information such as the names of the input features, the name of the target variable, the date and time the model was created, the type of model, and any additional descriptive information.

For Beginners: This method returns descriptive information about the model.

The GetModelMetadata method:

  • Returns the metadata stored in the ModelMetaData property
  • Is required by the IPredictiveModel interface
  • Provides access to information about what the model does and how it works

This method is useful when:

  • You want to display information about the model
  • You need to check what features the model expects
  • You're working with multiple models and need to identify them

For example, you might call this method to get the list of feature names so you can ensure your input data has the correct columns.

GetModelRegistry()

Gets the model registry for version and lifecycle management.

public IModelRegistry<T, TInput, TOutput>? GetModelRegistry()

Returns

IModelRegistry<T, TInput, TOutput>

The model registry, or null if not configured.

Remarks

For Beginners: Use this to manage model versions and stage transitions.

Example:

var registry = result.GetModelRegistry();
if (registry != null)
{
    registry.TransitionModelStage("my-model", 1, ModelStage.Production);
}

GetModelRegistryInfo()

Gets model registry information as a structured object.

public ModelRegistryInfo<T, TInput, TOutput>? GetModelRegistryInfo()

Returns

ModelRegistryInfo<T, TInput, TOutput>

A ModelRegistryInfo object containing registry data, or null if model registry was not used.

Remarks

For Beginners: This provides type-safe access to model versioning and registry data.

Example:

var registryInfo = result.GetModelRegistryInfo();
if (registryInfo != null)
{
    Console.WriteLine($"Model: {registryInfo.RegisteredName} v{registryInfo.Version}");

    // Promote to production
    if (registryInfo.Registry != null)
    {
        registryInfo.Registry.TransitionModelStage(
            registryInfo.RegisteredName,
            registryInfo.Version ?? 1,
            ModelStage.Production);
    }
}

GetNodeRelationships(string, EdgeDirection)

Gets all edges (relationships) connected to a node in the knowledge graph.

public IEnumerable<GraphEdge<T>> GetNodeRelationships(string nodeId, EdgeDirection direction = EdgeDirection.Both)

Parameters

nodeId string

The ID of the node to query.

direction EdgeDirection

The direction of edges to retrieve.

Returns

IEnumerable<GraphEdge<T>>

Collection of edges connected to the node.

Remarks

For Beginners: This method finds all relationships connected to an entity.

Example: Getting edges for "Einstein" might return:

  • Outgoing: STUDIED→Physics, WORKED_AT→Princeton, BORN_IN→Germany
  • Incoming: INFLUENCED_BY→Newton

Exceptions

InvalidOperationException

Thrown when knowledge graph is not configured.

ArgumentException

Thrown when nodeId is null or empty.

GetParameters()

Gets the parameters of the underlying model.

public Vector<T> GetParameters()

Returns

Vector<T>

A vector containing the model parameters.

Exceptions

InvalidOperationException

Thrown when the Model is not initialized.

GetTrainingInfrastructureMetadata()

Gets training infrastructure metadata as a dictionary.

public Dictionary<string, object?> GetTrainingInfrastructureMetadata()

Returns

Dictionary<string, object>

A dictionary containing all training infrastructure metadata.

Remarks

For Beginners: This provides a convenient way to access all training metadata at once.

Includes:

  • ExperimentRunId, ExperimentId - Experiment tracking IDs
  • ModelVersion, RegisteredModelName - Model registry info
  • CheckpointPath - Where the model was checkpointed
  • DataVersionHash - Training data version
  • HyperparameterTrialId - Which optimization trial produced this model

Example:

var metadata = result.GetTrainingInfrastructureMetadata();
Console.WriteLine($"Run ID: {metadata["ExperimentRunId"]}");
Console.WriteLine($"Model Version: {metadata["ModelVersion"]}");

GetTrainingMetricsHistory()

Gets the training metrics history.

public Dictionary<string, List<double>>? GetTrainingMetricsHistory()

Returns

Dictionary<string, List<double>>

A dictionary mapping metric names to their values over time, or null if not tracked.

GetTrainingMonitor()

Gets the training monitor for accessing training diagnostics.

public ITrainingMonitor<T>? GetTrainingMonitor()

Returns

ITrainingMonitor<T>

The training monitor, or null if not configured.

Remarks

For Beginners: Use this to analyze training history and diagnostics.

Example:

var monitor = result.GetTrainingMonitor();
if (monitor != null)
{
    var history = monitor.GetMetricsHistory();
}

HybridRetrieve(Vector<T>, int, int, int)

Retrieves results using hybrid vector + graph search for enhanced context retrieval.

public List<RetrievalResult<T>> HybridRetrieve(Vector<T> queryEmbedding, int topK = 5, int expansionDepth = 1, int maxResults = 10)

Parameters

queryEmbedding Vector<T>

The query embedding vector.

topK int

Number of initial candidates from vector search.

expansionDepth int

How many hops to traverse in the graph (0 = no expansion).

maxResults int

Maximum total results to return.

Returns

List<RetrievalResult<T>>

List of retrieval results with scores and source information.

Remarks

For Beginners: This method combines the best of both worlds: 1. First, it finds similar documents using vector similarity (like traditional RAG) 2. Then, it expands the context by traversing the knowledge graph to find related entities

For example, searching for "photosynthesis" might:

  • Find documents about photosynthesis via vector search
  • Then traverse the graph to also include chlorophyll, plants, carbon dioxide

This provides richer, more complete context than vector search alone.

Exceptions

InvalidOperationException

Thrown when hybrid retriever is not configured.

IsFeatureUsed(int)

Checks if a specific feature is used by the underlying model.

public bool IsFeatureUsed(int featureIndex)

Parameters

featureIndex int

The index of the feature to check.

Returns

bool

True if the feature is used, false otherwise.

Exceptions

InvalidOperationException

Thrown when the Model is not initialized.

LoadFromFile(string)

Loads the model from a file.

public void LoadFromFile(string filePath)

Parameters

filePath string

The path to the file containing the saved model.

Remarks

This method loads a serialized model from a file at the specified path. It reads the byte array from the file and then deserializes it using the Deserialize method. This method provides a convenient way to load a previously saved model.

For Beginners: This method loads a model from a file on disk.

The LoadFromFile method:

  • Takes a file path where the model is stored
  • Reads the byte array from the file
  • Deserializes the byte array to restore the model

This method is useful when:

  • You want to load a previously trained and saved model
  • You need to use a model that was shared with you
  • You want to deploy a pre-trained model in a production environment

For example, to load a model, you might use: myModel.LoadFromFile("C:\\Models\\house_price_predictor.model");

Note: This method is distinct from the static LoadModel overload which requires a model factory.

LoadModel(string, Func<ModelMetadata<T>, IFullModel<T, TInput, TOutput>>)

public static AiModelResult<T, TInput, TOutput> LoadModel(string filePath, Func<ModelMetadata<T>, IFullModel<T, TInput, TOutput>> modelFactory)

Parameters

filePath string
modelFactory Func<ModelMetadata<T>, IFullModel<T, TInput, TOutput>>

Returns

AiModelResult<T, TInput, TOutput>

LoadState(Stream)

Loads the prediction model result's state from a stream.

public virtual void LoadState(Stream stream)

Parameters

stream Stream

The stream to read the model state from.

Remarks

This method deserializes a complete AiModelResult that was previously saved with SaveState, restoring the model, optimization results, normalization information, and all metadata. It uses the existing Deserialize method after reading data from the stream.

For Beginners: This is like loading a saved snapshot of your complete trained model package.

When you call LoadState:

  • The trained model and all its parameters are read from the stream
  • Training results and metrics are restored
  • Normalization settings are reapplied
  • All metadata is recovered

After loading, the model package can:

  • Make predictions using the restored model
  • Access training history and metrics
  • Apply the same normalization as during training
  • Be deployed to production

This is essential for:

  • Resuming interrupted optimization
  • Loading the best model after training
  • Deploying trained models to production
  • Knowledge distillation workflows

Exceptions

ArgumentNullException

Thrown when stream is null.

IOException

Thrown when there's an error reading from the stream.

InvalidOperationException

Thrown when the stream contains invalid or incompatible data.

OptimizePrompt(string, Func<string, T>, int)

Optimizes a prompt to improve its effectiveness using an evaluation function.

public IPromptTemplate OptimizePrompt(string initialPrompt, Func<string, T> evaluationFunction, int maxIterations = 100)

Parameters

initialPrompt string

The initial prompt to optimize.

evaluationFunction Func<string, T>

A function that scores prompt performance (higher scores are better).

maxIterations int

Maximum number of optimization iterations.

Returns

IPromptTemplate

An optimized IPromptTemplate.

Remarks

This method uses the configured IPromptOptimizer to iteratively improve a prompt. The optimizer generates variations, evaluates them using your scoring function, and selects better-performing candidates over multiple iterations.

For Beginners: Prompt optimization automatically improves your prompts through testing.

How it works:

  1. Start with your initial prompt
  2. Generate variations (different wordings, structures)
  3. Test each variation using your evaluation function
  4. Keep the best-performing versions
  5. Repeat until maxIterations is reached

You provide the evaluation function that scores how well a prompt works:

// Evaluation function that tests accuracy
Func<string, double> evaluate = (prompt) =>
{
    double correctCount = 0;
    foreach (var testCase in testSet)
    {
        var result = model.Generate(prompt + testCase.Input);
        if (result == testCase.ExpectedOutput)
            correctCount++;
    }
    return correctCount / testSet.Count; // Returns accuracy 0.0 to 1.0
};

var optimized = modelResult.OptimizePrompt(
    "Classify sentiment:",
    evaluate,
    maxIterations: 50);

// Use the optimized template
string finalPrompt = optimized.Format(new Dictionary<string, string> { ["input"] = text });

Exceptions

InvalidOperationException

Thrown when no prompt optimizer is configured.

OptimizePromptAsync(string, Func<string, Task<T>>, int, CancellationToken)

Optimizes a prompt asynchronously using an async evaluation function.

public Task<IPromptTemplate> OptimizePromptAsync(string initialPrompt, Func<string, Task<T>> evaluationFunction, int maxIterations = 100, CancellationToken cancellationToken = default)

Parameters

initialPrompt string

The initial prompt to optimize.

evaluationFunction Func<string, Task<T>>

An async function that scores prompt performance (higher scores are better).

maxIterations int

Maximum number of optimization iterations.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<IPromptTemplate>

A task that resolves to an optimized IPromptTemplate.

Remarks

Async version of OptimizePrompt for when your evaluation function involves asynchronous operations like API calls or I/O operations.

For Beginners: Use this when your scoring function calls APIs.

Example with async evaluation:

// Async evaluation function that calls an API
Func<string, Task<double>> evaluateAsync = async (prompt) =>
{
    var results = await TestWithApiAsync(prompt);
    return CalculateAccuracy(results);
};

var optimized = await modelResult.OptimizePromptAsync(
    "Classify sentiment:",
    evaluateAsync,
    maxIterations: 50);

Benefits:

  • Doesn't block your program during optimization
  • Can be cancelled if needed
  • Handles async API calls efficiently

Exceptions

InvalidOperationException

Thrown when no prompt optimizer is configured.

Predict(TInput)

Makes predictions using the model on the provided input data.

public TOutput Predict(TInput newData)

Parameters

newData TInput

A matrix of input features, where each row represents an observation and each column represents a feature.

Returns

TOutput

A vector of predicted values, one for each observation in the input matrix.

Remarks

This method makes predictions using the model on the provided input data. It first normalizes the input data using the normalizer from the NormalizationInfo property, then passes the normalized data to the model's Predict method, and finally denormalizes the model's outputs to obtain the final predictions. This process ensures that the input data is preprocessed in the same way as the training data was, and that the predictions are in the same scale as the original target variable.

For Beginners: This method makes predictions on new data using the trained model.

The Predict method:

  • Takes a matrix of input features as its parameter
  • Normalizes the input data to match how the model was trained
  • Passes the normalized data to the model for prediction
  • Denormalizes the results to convert them back to the original scale
  • Returns a vector of predictions, one for each row in the input matrix

This method will throw an exception if:

  • The Model property is null (not initialized)
  • The Normalizer in NormalizationInfo is null (not initialized)

For example, if you have a matrix of house features (square footage, bedrooms, etc.), this method will return a vector of predicted house prices.

Exceptions

InvalidOperationException

Thrown when the Model or Normalizer is not initialized.

PredictWithDefense(TInput)

Makes a prediction with adversarial preprocessing applied if configured.

public TOutput PredictWithDefense(TInput input)

Parameters

input TInput

The input to make a prediction on.

Returns

TOutput

The model's prediction after defensive preprocessing.

Remarks

For Beginners: This method applies defensive preprocessing to the input before making a prediction. Preprocessing can include techniques like: - JPEG compression to remove high-frequency adversarial perturbations - Bit depth reduction to quantize away small perturbations - Denoising to smooth out adversarial noise

PredictWithTestTimeAugmentation(TInput, IAugmentationPolicy<T, TInput>?)

Makes a prediction using Test-Time Augmentation when input/output types match augmentation types.

public TestTimeAugmentationResult<TOutput> PredictWithTestTimeAugmentation(TInput data, IAugmentationPolicy<T, TInput>? augmentationPipeline = null)

Parameters

data TInput

The input data to predict on (must be augmentable).

augmentationPipeline IAugmentationPolicy<T, TInput>

Returns

TestTimeAugmentationResult<TOutput>

A TTA result containing the aggregated prediction and individual predictions.

Remarks

This is a simplified overload for when TInput is directly augmentable (e.g., ImageTensor). For complex type conversions, use the full overload with converter functions.

PredictWithTestTimeAugmentation<TAugData>(TAugData, Func<TAugData, TInput>, Func<TOutput, Vector<T>>, IAugmentationPolicy<T, TAugData>?)

Makes a prediction using Test-Time Augmentation for improved accuracy.

public TestTimeAugmentationResult<TOutput> PredictWithTestTimeAugmentation<TAugData>(TAugData data, Func<TAugData, TInput> convertToModelInput, Func<TOutput, Vector<T>> convertFromModelOutput, IAugmentationPolicy<T, TAugData>? augmentationPipeline = null)

Parameters

data TAugData

The input data to predict on.

convertToModelInput Func<TAugData, TInput>

Function to convert augmented data to model input type.

convertFromModelOutput Func<TOutput, Vector<T>>

Function to convert model output to a format suitable for aggregation.

augmentationPipeline IAugmentationPolicy<T, TAugData>

Returns

TestTimeAugmentationResult<TOutput>

A TTA result containing the aggregated prediction and individual predictions.

Type Parameters

TAugData

The augmentation data type (e.g., ImageTensor<T>).

Remarks

Test-Time Augmentation improves prediction accuracy by: 1. Creating multiple augmented versions of the input 2. Making predictions on each version 3. Aggregating predictions using the configured method (mean, median, vote, etc.)

For Beginners: Instead of making one prediction, TTA makes several predictions on variations of your input (flipped, rotated, etc.) and combines them for a more robust answer. This typically improves accuracy by 1-3% at the cost of slower inference.

Exceptions

InvalidOperationException

Thrown if TTA is not configured or disabled.

PredictWithUncertainty(TInput, int?)

public UncertaintyPredictionResult<T, TOutput> PredictWithUncertainty(TInput newData, int? numSamples = null)

Parameters

newData TInput
numSamples int?

Returns

UncertaintyPredictionResult<T, TOutput>

QueryKnowledgeGraph(string, int)

Queries the knowledge graph to find related nodes by entity name or label.

public IEnumerable<GraphNode<T>> QueryKnowledgeGraph(string query, int topK = 10)

Parameters

query string

The search query (entity name or partial match).

topK int

Maximum number of results to return.

Returns

IEnumerable<GraphNode<T>>

Collection of matching graph nodes.

Remarks

For Beginners: This method searches the knowledge graph for entities matching your query. Unlike vector search which finds similar documents, this finds entities by name or label.

Example:

var nodes = result.QueryKnowledgeGraph("Einstein", topK: 5);
foreach (var node in nodes)
{
    Console.WriteLine($"{node.Label}: {node.Id}");
}

Exceptions

InvalidOperationException

Thrown when Graph RAG is not configured.

QuickReasonAsync(string, CancellationToken)

Quickly solves a problem with minimal reasoning overhead for fast answers.

public Task<string> QuickReasonAsync(string problem, CancellationToken cancellationToken = default)

Parameters

problem string

The problem or question to solve.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<string>

The final answer as a string.

Remarks

For Beginners: Use this for simple problems where you need a fast answer without detailed reasoning steps. It's optimized for speed over thoroughness.

When to use:

  • Simple math problems
  • Quick factual questions
  • When speed matters more than detailed explanation

Example:

string answer = await modelResult.QuickReasonAsync("What is 15% of 240?");
Console.WriteLine(answer);  // "36"

Exceptions

InvalidOperationException

Thrown when agent configuration is not set up.

ReasonAsync(string, ReasoningMode, ReasoningConfig?, CancellationToken)

Solves a problem using advanced reasoning strategies like Chain-of-Thought, Tree-of-Thoughts, or Self-Consistency.

public Task<ReasoningResult<T>> ReasonAsync(string problem, ReasoningMode mode = ReasoningMode.Auto, ReasoningConfig? config = null, CancellationToken cancellationToken = default)

Parameters

problem string

The problem or question to solve.

mode ReasoningMode

The reasoning mode to use (default: Auto selects based on problem complexity).

config ReasoningConfig

Optional reasoning configuration (uses pre-configured settings if null).

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ReasoningResult<T>>

A complete reasoning result with answer, steps, and metrics.

Remarks

For Beginners: This method uses advanced AI reasoning to solve problems step by step, just like how a human would "show their work" on a complex problem.

What it does:

  • Breaks down the problem into logical steps
  • Can explore multiple solution paths (Tree-of-Thoughts)
  • Can verify answers with multiple attempts (Self-Consistency)
  • Returns detailed reasoning chain for transparency

Reasoning modes:

  • Auto: Automatically selects the best strategy
  • ChainOfThought: Step-by-step linear reasoning
  • TreeOfThoughts: Explores multiple paths, backtracks if needed
  • SelfConsistency: Solves multiple times, uses majority voting

Example:

var result = await modelResult.ReasonAsync(
    "If a train travels 60 mph for 2.5 hours, how far does it go?",
    ReasoningMode.ChainOfThought
);
Console.WriteLine(result.FinalAnswer);  // "150 miles"
Console.WriteLine(result.ReasoningChain);  // Shows step-by-step work

Requirements:

  • Agent configuration must be set (ConfigureAgentAssistance during building)
  • API key for LLM provider (OpenAI, Anthropic, etc.)

Exceptions

InvalidOperationException

Thrown when agent configuration is not set up.

ReasonWithConsensusAsync(string, int, CancellationToken)

Solves a problem multiple times using different approaches and returns the consensus answer.

public Task<ReasoningResult<T>> ReasonWithConsensusAsync(string problem, int numAttempts = 5, CancellationToken cancellationToken = default)

Parameters

problem string

The problem to solve.

numAttempts int

Number of independent solving attempts (default: 5).

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<ReasoningResult<T>>

The consensus result with voting statistics.

Remarks

For Beginners: This method solves the same problem multiple times independently and picks the most common answer. It's like asking 5 experts and going with the majority.

How it works:

  • Solves the problem N times independently
  • Each attempt may use slightly different reasoning
  • Uses majority voting to pick the final answer
  • Reports confidence based on agreement level

When to use:

  • Math problems where errors are common
  • When you need high confidence in the answer
  • Problems where reasoning paths can vary

Example:

var result = await modelResult.ReasonWithConsensusAsync(
    "What is the derivative of x^3?",
    numAttempts: 5
);
// If 4 out of 5 attempts say "3x^2", that's the answer
Console.WriteLine($"Consensus: {result.Metrics["consensus_ratio"]:P0}");

Exceptions

InvalidOperationException

Thrown when agent configuration is not set up.

RetrieveDocuments(string, int?, bool, Dictionary<string, object>?)

Retrieves relevant documents without generating an answer during inference.

public IEnumerable<Document<T>> RetrieveDocuments(string query, int? topK = null, bool applyReranking = true, Dictionary<string, object>? metadataFilters = null)

Parameters

query string

The search query.

topK int?

Number of documents to retrieve (optional).

applyReranking bool

Whether to rerank results (default: true).

metadataFilters Dictionary<string, object>

Optional filters for document selection.

Returns

IEnumerable<Document<T>>

Retrieved and optionally reranked documents.

Remarks

For Beginners: Use this during inference to search your document collection without generating an answer. Good for exploring what documents are available or debugging retrieval quality.

RAG must be configured via AiModelBuilder.ConfigureRetrievalAugmentedGeneration() before building the model.

Exceptions

InvalidOperationException

Thrown when RAG components are not configured.

RunChain(string)

Executes a prompt chain synchronously with the given input.

public string RunChain(string input)

Parameters

input string

The initial input to the chain.

Returns

string

The output string from the chain execution.

Remarks

This method executes a multi-step prompt workflow where each step's output becomes the next step's input. Chains enable complex workflows like translation followed by summarization, or analysis followed by formatting.

For Beginners: A prompt chain runs multiple AI steps in sequence.

Instead of doing everything in one big prompt, chains break tasks into steps:

// Chain example: Translate → Summarize → Extract Keywords
// Step 1: Translate document from Spanish to English
// Step 2: Summarize the translated document
// Step 3: Extract key points as bullet points

Each step takes the previous step's output as input.

Benefits of chains:

  • Simpler prompts (each does one thing well)
  • Better quality (specialized prompts perform better)
  • Easier debugging (inspect intermediate results)
  • Flexible workflows (add/remove/modify steps)

Example:

string spanishDocument = "Documento en español...";
string result = modelResult.RunChain(spanishDocument);

Console.WriteLine($"Result: {result}");

Exceptions

InvalidOperationException

Thrown when no prompt chain is configured.

RunChainAsync(string, CancellationToken)

Executes a prompt chain asynchronously with the given input.

public Task<string> RunChainAsync(string input, CancellationToken cancellationToken = default)

Parameters

input string

The initial input to the chain.

cancellationToken CancellationToken

Token to cancel the operation.

Returns

Task<string>

A task that resolves to the output string from the chain execution.

Remarks

Async version of RunChain for non-blocking execution. Essential for chains that make API calls to language models or perform other I/O operations.

For Beginners: Same as RunChain but doesn't block your program.

Use this version when:

  • Running in a web application (keeps server responsive)
  • Processing many documents in parallel
  • Making actual API calls to language models

Example:

string result = await modelResult.RunChainAsync("Input text...");
Console.WriteLine(result);

For parallel processing:

var documents = new[] { "Doc 1", "Doc 2", "Doc 3" };
var tasks = documents.Select(doc => modelResult.RunChainAsync(doc));
var results = await Task.WhenAll(tasks);

Exceptions

InvalidOperationException

Thrown when no prompt chain is configured.

SaveModel(string)

Saves the model to a file.

public void SaveModel(string filePath)

Parameters

filePath string

The path where the model will be saved.

Remarks

This method saves the serialized model to a file at the specified path. It first serializes the model to a byte array using the Serialize method, then writes the byte array to the specified file. If the file already exists, it will be overwritten. This method provides a convenient way to persist the model for later use.

For Beginners: This method saves the model to a file on disk.

The SaveModel method:

  • Takes a file path where the model should be saved
  • Serializes the model to a byte array
  • Writes the byte array to the specified file

This method is useful when:

  • You want to save a trained model for later use
  • You need to share a model with others
  • You want to deploy a model to a production environment

For example, after training a model, you might save it with: myModel.SaveModel("C:\\Models\\house_price_predictor.model");

If the file already exists, it will be overwritten.

SaveState(Stream)

Saves the prediction model result's current state to a stream.

public virtual void SaveState(Stream stream)

Parameters

stream Stream

The stream to write the model state to.

Remarks

This method serializes the entire AiModelResult, including the underlying model, optimization results, normalization information, and metadata. It uses the existing Serialize method and writes the data to the provided stream.

For Beginners: This is like creating a snapshot of your complete trained model package.

When you call SaveState:

  • The trained model and all its parameters are written to the stream
  • Training results and metrics are saved
  • Normalization settings are preserved
  • All metadata is included

This is particularly useful for:

  • Checkpointing during long optimization runs
  • Saving the best model found during training
  • Knowledge distillation workflows
  • Creating model backups before deployment

You can later use LoadState to restore the complete model package.

Exceptions

ArgumentNullException

Thrown when stream is null.

IOException

Thrown when there's an error writing to the stream.

SelectFewShotExamples(string, int)

Selects relevant few-shot examples for a given query or context.

public IReadOnlyList<FewShotExample> SelectFewShotExamples(string query, int maxExamples = 5)

Parameters

query string

The query or context to find relevant examples for.

maxExamples int

The maximum number of examples to return.

Returns

IReadOnlyList<FewShotExample>

A list of FewShotExample objects most relevant to the query.

Remarks

This method uses the configured IFewShotExampleSelector to find the most relevant examples from a pool of available examples. The selection can be based on similarity, diversity, or other strategies depending on the selector implementation.

For Beginners: Few-shot examples teach the AI what you want by showing examples.

Instead of just describing what you want, you show the AI examples:

// Without few-shot: "Translate English to Spanish"
// With few-shot: "Translate English to Spanish. Examples:
//   'Hello' -> 'Hola'
//   'Goodbye' -> 'Adiós'
//   Now translate: 'Good morning'"

The challenge is choosing which examples to include. This method automatically selects the most relevant examples for your specific input.

Example:

// You have a pool of 100 translation examples
// For the input "How are you?", select the 3 most relevant
var examples = modelResult.SelectFewShotExamples("How are you?", maxExamples: 3);

// Build your prompt with the selected examples
var prompt = "Translate English to Spanish. Examples:\n";
foreach (var ex in examples)
{
    prompt += $"  '{ex.Input}' -> '{ex.Output}'\n";
}
prompt += "Now translate: How are you?";

Selection strategies include:

  • Similarity-based: Choose examples most similar to the query
  • Diversity-based: Choose examples covering different cases
  • Hybrid: Balance similarity and diversity

Exceptions

InvalidOperationException

Thrown when no few-shot example selector is configured.

Serialize()

Serializes the model to a byte array.

public byte[] Serialize()

Returns

byte[]

A byte array containing the serialized model.

Remarks

This method serializes the entire AiModelResult object, including the model, optimization results, normalization information, and metadata. The model is serialized using its own Serialize() method, ensuring that model-specific serialization logic is properly applied. The other components are serialized using JSON. This approach ensures that each component of the AiModelResult is serialized in the most appropriate way.

For Beginners: This method converts the model into a format that can be stored or transmitted.

The Serialize method:

  • Uses the model's own serialization method to properly handle model-specific details
  • Serializes other components (optimization results, normalization info, metadata) to JSON
  • Combines everything into a single byte array that can be saved to a file or database

This is important because:

  • Different model types may need to be serialized differently
  • It ensures all the model's internal details are properly preserved
  • It allows for more efficient and robust storage of the complete prediction model package

SetActiveFeatureIndices(IEnumerable<int>)

Setting active feature indices is not supported on AiModelResult.

public void SetActiveFeatureIndices(IEnumerable<int> featureIndices)

Parameters

featureIndices IEnumerable<int>

The feature indices (not used).

Remarks

Changing active features would invalidate the trained model and optimization results. To train a model with different features, use AiModelBuilder with the desired feature configuration.

Exceptions

InvalidOperationException

Always thrown - AiModelResult feature configuration cannot be modified.

SetParameters(Vector<T>)

Setting parameters is not supported on AiModelResult.

public void SetParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The parameter vector (not used).

Remarks

Modifying parameters would invalidate the OptimizationResult which reflects the optimized parameter values. To create a model with different parameters, use AiModelBuilder with custom initial parameters.

Exceptions

InvalidOperationException

Always thrown - AiModelResult parameters cannot be modified.

SummarizeCode(CodeSummarizationRequest)

public CodeSummarizationResult SummarizeCode(CodeSummarizationRequest request)

Parameters

request CodeSummarizationRequest

Returns

CodeSummarizationResult

Tokenize(string)

Tokenizes text using the configured tokenizer.

public TokenizationResult Tokenize(string text)

Parameters

text string

The text to tokenize.

Returns

TokenizationResult

The tokenization result containing token IDs, attention mask, etc.

Remarks

For Beginners: This method converts your text into the format the model needs.

Example:

var result = modelResult.Tokenize("Hello, how are you?");
// result.TokenIds contains [101, 7592, 1010, 2129, 2024, 2017, 1029, 102]
// result.AttentionMask contains [1, 1, 1, 1, 1, 1, 1, 1]

Exceptions

InvalidOperationException

Thrown when no tokenizer is configured.

TokenizeBatch(List<string>)

Tokenizes multiple texts in a batch.

public List<TokenizationResult> TokenizeBatch(List<string> texts)

Parameters

texts List<string>

The texts to tokenize.

Returns

List<TokenizationResult>

A list of tokenization results.

Exceptions

InvalidOperationException

Thrown when no tokenizer is configured.

TokenizeCode(string, ProgramLanguage, EncodingOptions?, CodeTokenizationPipelineOptions?)

Tokenizes code using the canonical code-tokenization pipeline (supports AST extraction when enabled).

public CodeTokenizationResult TokenizeCode(string code, ProgramLanguage language, EncodingOptions? options = null, CodeTokenizationPipelineOptions? pipelineOptions = null)

Parameters

code string
language ProgramLanguage
options EncodingOptions
pipelineOptions CodeTokenizationPipelineOptions

Returns

CodeTokenizationResult

Train(TInput, TOutput)

Training is not supported on AiModelResult. Use AiModelBuilder to create and train new models.

public void Train(TInput input, TOutput expectedOutput)

Parameters

input TInput

Input training data (not used).

expectedOutput TOutput

Expected output values (not used).

Remarks

AiModelResult is a snapshot of a trained model with its optimization results and metadata. Retraining would invalidate the OptimizationResult and metadata. To train a new model or retrain with different data, use AiModelBuilder.Build() instead.

Exceptions

InvalidOperationException

Always thrown - AiModelResult represents an already-trained model and cannot be retrained.

TraverseGraph(string, int)

Traverses the knowledge graph starting from a node using breadth-first search.

public IEnumerable<GraphNode<T>> TraverseGraph(string startNodeId, int maxDepth = 2)

Parameters

startNodeId string

The ID of the starting node.

maxDepth int

Maximum traversal depth.

Returns

IEnumerable<GraphNode<T>>

Collection of nodes reachable from the starting node in BFS order.

Remarks

For Beginners: This method explores the graph starting from a specific entity, discovering all connected entities up to a specified depth.

Example: Starting from "Paris", depth=2 might find:

  • Depth 1: France, Eiffel Tower, Seine River
  • Depth 2: Europe, Iron, Water

This is useful for understanding the context around a specific entity.

Exceptions

InvalidOperationException

Thrown when knowledge graph is not configured.

ValidatePrompt(string, ValidationOptions?)

Validates a prompt and returns any detected issues or warnings.

public IReadOnlyList<PromptIssue> ValidatePrompt(string prompt, ValidationOptions? options = null)

Parameters

prompt string

The prompt text to validate.

options ValidationOptions

Optional validation options to customize the validation behavior.

Returns

IReadOnlyList<PromptIssue>

A list of PromptIssue objects describing any problems found.

Remarks

This method uses the configured IPromptAnalyzer to check a prompt for common issues such as unclosed variables, excessive length, potential injection vulnerabilities, and other problems that could affect prompt quality or safety.

For Beginners: Validation catches problems with your prompts before you send them.

Common issues that validation detects:

  • Unclosed variable placeholders: "{text" instead of "{text}"
  • Prompts that are too long for the model's context window
  • Potential prompt injection attempts in user input
  • Missing required sections or unclear instructions

Example:

string prompt = "Translate {text from English to Spanish."; // Note: unclosed {
var issues = modelResult.ValidatePrompt(prompt);

foreach (var issue in issues)
{
    Console.WriteLine($"[{issue.Severity}] {issue.Message}");
    // Output: "[Error] Unclosed variable placeholder at position 10"
}

if (issues.Any(i => i.Severity == IssueSeverity.Error))
{
    // Don't send the prompt - fix the errors first
}

Exceptions

InvalidOperationException

Thrown when no prompt analyzer is configured.

WithParameters(Vector<T>)

Creates a new instance with the specified parameters.

public IFullModel<T, TInput, TOutput> WithParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The parameter vector to use.

Returns

IFullModel<T, TInput, TOutput>

A new AiModelResult with updated parameters.

Remarks

This method creates a new model with updated parameters. The OptimizationResult is deep-copied and updated to reference the new model. NormalizationInfo is shared (shallow-copied) since normalization parameters don't change when model parameters change.

All configuration components (prompt engineering, RAG, agents, etc.) are shallow-copied, meaning they are shared between the original and new instance. See DeepCopy() for detailed documentation on which components are deep vs shallow copied.

Exceptions

InvalidOperationException

Thrown when the Model is not initialized.