Skip to content

Latest commit

 

History

History
39 lines (31 loc) · 1.1 KB

File metadata and controls

39 lines (31 loc) · 1.1 KB

Custom Rules Guide

Implement IGuardrailRule:

public class ProfanityFilter : IGuardrailRule
{
    private readonly HashSet<string> _blocked;
    public ProfanityFilter(IEnumerable<string> words) => _blocked = new(words, StringComparer.OrdinalIgnoreCase);

    public string Name => "profanity-filter";
    public GuardrailPhase Phase => GuardrailPhase.Both;

    public ValueTask<GuardrailResult> EvaluateAsync(GuardrailContext ctx, CancellationToken ct = default)
    {
        var found = ctx.Text.Split(' ').Any(w => _blocked.Contains(w));
        return ValueTask.FromResult(found
            ? GuardrailResult.Blocked("Prohibited language detected.")
            : GuardrailResult.Passed());
    }
}

Register in a pipeline:

var policy = new GuardrailPolicyBuilder()
    .AddRule(new ProfanityFilter(["word1"]))
    .Build();

Or use a delegate for simple checks:

.AddRule("no-tables", GuardrailPhase.Output,
    (ctx, ct) => ValueTask.FromResult(ctx.Text.Contains("|---|")
        ? GuardrailResult.Blocked("Markdown tables not allowed.")
        : GuardrailResult.Passed()))