eval

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0, MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package eval grades an AI target against cases with deterministic checks or a judge model. Evals spend tokens, so they live in tests behind the eval build tag and run with `fletch test --evals`:

//go:build eval

func TestRefunds(t *testing.T) {
	eval.Run(t, eval.Model(assistant), eval.Cases{
		{Input: "I want a refund for a 45-day-old order", Expect: eval.Rubric("declines; cites the 30-day policy")},
	}, eval.Judge(judge), eval.Threshold(0.8), eval.Repeat(3))
}

When ReportDirEnv names a directory, Run writes one JSON Report there per call; `fletch test --evals` collects them into .fletch/evals.

Index

Constants

View Source
const DefaultThreshold = 0.8

DefaultThreshold is the mean score a case needs when Threshold is not set.

View Source
const ReportDirEnv = "FLETCH_EVAL_REPORT_DIR"

ReportDirEnv names the environment variable that holds the directory Run writes its reports to.

Variables

View Source
var ErrNoJudge = errors.New("eval: a Rubric needs eval.Judge(model)")

ErrNoJudge is returned for a Rubric case when no Judge is set.

Functions

This section is empty.

Types

type Attempt

type Attempt struct {
	Output string `json:"output"`
	Score
	Error string `json:"error,omitempty"`
}

Attempt is one answer to a case and its grade.

type Case

type Case struct {
	// Name names the case in reports; default "case_<n>".
	Name   string
	Input  string
	Expect Expectation
}

Case is one input and what its answer must meet.

type CaseResult

type CaseResult struct {
	Name      string    `json:"name"`
	Input     string    `json:"input"`
	Score     float64   `json:"score"`
	Threshold float64   `json:"threshold"`
	Pass      bool      `json:"pass"`
	Attempts  []Attempt `json:"attempts"`
}

CaseResult is how one case did.

type Cases

type Cases []Case

Cases is a list of Case.

type CheckFunc

type CheckFunc func(output string) Score

CheckFunc is an Expectation written as a function of the answer.

func (CheckFunc) Grade

func (f CheckFunc) Grade(_ context.Context, _ aikit.Model, _, output string) (Score, error)

Grade calls f.

type Expectation

type Expectation interface {
	Grade(ctx context.Context, judge aikit.Model, input, output string) (Score, error)
}

Expectation grades one answer. judge is the Judge model, or nil.

func Contains

func Contains(subs ...string) Expectation

Contains scores the share of subs that the answer contains, ignoring case.

func Rubric

func Rubric(criteria string) Expectation

Rubric is graded by the Judge model against criteria written in plain language.

type Func

type Func func(ctx context.Context, input string) (string, error)

Func adapts a function to a Target.

func (Func) Answer

func (f Func) Answer(ctx context.Context, input string) (string, error)

Answer calls f.

type Option

type Option func(*config)

Option configures Evaluate and Run.

func Judge

func Judge(m aikit.Model) Option

Judge sets the model that grades Rubric cases.

func Repeat

func Repeat(n int) Option

Repeat asks each case n times and averages the scores, since answers vary between runs. Default 1.

func Threshold

func Threshold(v float64) Option

Threshold sets the mean score a case needs to pass. Default DefaultThreshold.

type Report

type Report struct {
	// Test is the test's name and Dir the package directory; Run sets them.
	Test  string       `json:"test,omitempty"`
	Dir   string       `json:"dir,omitempty"`
	Cases []CaseResult `json:"cases"`
}

Report is the result of one Evaluate or Run.

func Evaluate

func Evaluate(ctx context.Context, target Target, cases Cases, opts ...Option) (*Report, error)

Evaluate asks target each case and grades the answers. An answer that fails, or a grade that errors, scores 0 for that attempt. Only an expectation that cannot be graded at all, such as a Rubric with no Judge, returns an error.

func Run

func Run(t *testing.T, target Target, cases Cases, opts ...Option) *Report

Run is Evaluate as a test: each case is a subtest that fails below the threshold. With ReportDirEnv set it also writes the report there.

func (*Report) Passed

func (r *Report) Passed() bool

Passed reports whether every case passed.

type Score

type Score struct {
	Value  float64 `json:"score"`
	Reason string  `json:"reason,omitempty"`
}

Score is a grade from 0 (fails) to 1 (meets the expectation fully).

type Target

type Target interface {
	Answer(ctx context.Context, input string) (string, error)
}

Target is what an eval asks: an agent, a model, or any function from input to answer.

func Model

func Model(m aikit.Model, opts ...aikit.Option) Target

Model returns a Target that sends the input to m as a user message.

Jump to

Keyboard shortcuts

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