buffs

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package buffs provides the buff interface for prompt transformations.

Buffs transform attempts by modifying prompts. They can add prefixes, encode text, translate languages, or apply other mutations to test LLM robustness against prompt variations.

Index

Constants

This section is empty.

Variables

View Source
var Registry = registry.New[Buff]("buffs")

Registry is the global buff registry.

Functions

func DefaultBuff

func DefaultBuff(ctx context.Context, attempts []*attempt.Attempt, t Transformer) ([]*attempt.Attempt, error)

DefaultBuff provides the standard Buff() loop: iterate over input attempts, check for context cancellation between each, collect all Transform() results.

Most buff implementations have identical Buff() methods that follow this exact pattern. Using DefaultBuff eliminates that boilerplate.

Usage in a buff implementation:

func (b *MyBuff) Buff(ctx context.Context, attempts []*attempt.Attempt) ([]*attempt.Attempt, error) {
    return buffs.DefaultBuff(ctx, attempts, b)
}

func Get

func Get(name string) (func(registry.Config) (Buff, error), bool)

Get retrieves a buff factory by name.

func List

func List() []string

List returns all registered buff names.

func NewBuffedProber

func NewBuffedProber(inner types.Prober, chain *BuffChain) types.Prober

NewBuffedProber wraps a prober with buff transformations. If the chain is nil or empty, it returns the inner prober directly (zero overhead). The generator is passed to Probe() at call time, not at construction.

func Register

func Register(name string, factory func(registry.Config) (Buff, error))

Register adds a buff factory to the global registry.

Types

type Buff

type Buff interface {
	// Buff transforms a slice of attempts, returning modified versions.
	Buff(ctx context.Context, attempts []*attempt.Attempt) ([]*attempt.Attempt, error)
	// Transform yields transformed attempts from a single input.
	// Uses iter.Seq for lazy generation (Go 1.23+).
	Transform(a *attempt.Attempt) iter.Seq[*attempt.Attempt]
	// Name returns the buff's fully qualified name.
	Name() string
	// Description returns a human-readable description.
	Description() string
}

Buff transforms attempts by modifying their prompts.

func Create

func Create(name string, cfg registry.Config) (Buff, error)

Create instantiates a buff by name.

type BuffChain

type BuffChain struct {
	// contains filtered or unexported fields
}

BuffChain composes multiple buffs into a sequential pipeline.

func NewBuffChain

func NewBuffChain(buffs ...Buff) *BuffChain

NewBuffChain creates a chain from the given buffs.

func (*BuffChain) Apply

func (c *BuffChain) Apply(ctx context.Context, attempts []*attempt.Attempt) ([]*attempt.Attempt, error)

Apply runs all buffs in sequence on the given attempts.

func (*BuffChain) ApplyPostBuffs

func (c *BuffChain) ApplyPostBuffs(ctx context.Context, a *attempt.Attempt) (*attempt.Attempt, error)

ApplyPostBuffs runs any PostBuff.Untransform hooks on the attempt.

func (*BuffChain) Buffs

func (c *BuffChain) Buffs() []Buff

Buffs returns the underlying buff slice.

func (*BuffChain) HasPostBuffHooks

func (c *BuffChain) HasPostBuffHooks() bool

HasPostBuffHooks returns true if any buff implements PostBuff.

func (*BuffChain) IsEmpty

func (c *BuffChain) IsEmpty() bool

IsEmpty returns true if the chain has no buffs.

func (*BuffChain) Len

func (c *BuffChain) Len() int

Len returns the number of buffs in the chain.

func (*BuffChain) Transform

func (c *BuffChain) Transform(a *attempt.Attempt) iter.Seq[*attempt.Attempt]

Transform applies all buffs lazily using iter.Seq.

type BuffedProber

type BuffedProber struct {
	// contains filtered or unexported fields
}

BuffedProber wraps a Prober and applies buff transformations.

func (*BuffedProber) Description

func (bp *BuffedProber) Description() string

Description returns the probe description (delegated to inner if available).

func (*BuffedProber) GetPrimaryDetector

func (bp *BuffedProber) GetPrimaryDetector() string

GetPrimaryDetector returns the primary detector (delegated to inner if available).

func (*BuffedProber) GetPrompts

func (bp *BuffedProber) GetPrompts() []string

GetPrompts returns the probe prompts (delegated to inner if available).

func (*BuffedProber) Goal

func (bp *BuffedProber) Goal() string

Goal returns the probe goal (delegated to inner if available).

func (*BuffedProber) Name

func (bp *BuffedProber) Name() string

Name returns the probe name (delegated to inner).

func (*BuffedProber) Probe

func (bp *BuffedProber) Probe(ctx context.Context, gen types.Generator) ([]*attempt.Attempt, error)

Probe executes the wrapped probe, applies buffs, re-generates, and returns.

type PostBuff

type PostBuff interface {
	Buff
	// HasPostBuffHook returns true if this buff needs post-generation processing.
	HasPostBuffHook() bool
	// Untransform post-processes an attempt after generation.
	Untransform(ctx context.Context, a *attempt.Attempt) (*attempt.Attempt, error)
}

PostBuff is an optional interface for buffs that need to post-process generator outputs after generation but before detection.

type Transformer

type Transformer interface {
	Transform(a *attempt.Attempt) iter.Seq[*attempt.Attempt]
}

Transformer is any type that can transform a single attempt into a sequence of attempts. All Buff implementations satisfy this interface via their Transform method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL