Table of Contents

Method BuildAndValidate

Namespace
LMKit.Agents
Assembly
LM-Kit.NET.dll

BuildAndValidate(out IReadOnlyList<string>)

Builds the agent and validates its configuration.

public Agent BuildAndValidate(out IReadOnlyList<string> errors)

Parameters

errors IReadOnlyList<string>

When this method returns, contains validation errors if any were found.

Returns

Agent

A new Agent if validation passes; otherwise, null.

Examples

Building with validation:

using LMKit.Model;
using LMKit.Agents;

using var model = new LM("path/to/model.gguf");

var agent = new AgentBuilder()
    .WithModel(model)
    .WithPersona("Assistant")
    .WithTools(t => t.Register(new MyTool()))
    .BuildAndValidate(out var errors);

if (agent != null)
{
    Console.WriteLine("Agent is ready!");
    var result = agent.Run("Test the agent.");
    Console.WriteLine(result.Content);
}
else
{
    Console.WriteLine("Validation failed:");
    foreach (var error in errors)
    {
        Console.WriteLine($"  - {error}");
    }
}

Handling missing model error:

using LMKit.Agents;

// Forgot to set the model
var agent = new AgentBuilder()
    .WithPersona("Assistant")
    .BuildAndValidate(out var errors);

// agent will be null, errors will contain "Agent requires a language model."
if (agent == null)
{
    Console.WriteLine($"Cannot create agent: {string.Join(", ", errors)}");
}

Remarks

This method combines Build() and Validate(out IReadOnlyList<string>) in a single operation. Use it when you want to ensure the agent is correctly configured before use.