eval

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2025 License: Apache-2.0 Imports: 20 Imported by: 2 modules, 6 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 is used to run evals in Braintrust.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

type Case[I, R any] struct {
	// Input is the input to the task function.
	Input I

	// Expected is the expected output (for scoring).
	// Optional.
	Expected R

	// Tags are labels to attach to this case.
	// Optional.
	Tags []string

	// Metadata is additional metadata for this case.
	// Optional.
	Metadata map[string]interface{}

	// These fields are only set if the Case is part of a Dataset.
	// They link the eval result back to the source dataset row.
	ID      string // Dataset record ID
	XactID  string // Transaction ID
	Created string // Creation timestamp
}

Case represents a single test case in an evaluation.

type Dataset

type Dataset[I, R any] interface {
	// Next returns the next case, or io.EOF if there are no more cases.
	Next() (Case[I, R], error)

	// ID returns the dataset ID if backed by a Braintrust dataset.
	// Returns empty string for literal in-memory cases.
	ID() string

	// Version returns the dataset version if applicable.
	// Returns empty string for literal cases or unversioned datasets.
	Version() string
}

Dataset is an iterator interface for evaluation datasets. It is commonly an in-memory slice of cases, but can also be a dataset lazily loaded from the Braintrust API.

func NewDataset

func NewDataset[I, R any](cases []Case[I, R]) Dataset[I, R]

NewDataset creates a Dataset iterator from a slice of cases.

type DatasetAPI

type DatasetAPI[I, R any] struct {
	// contains filtered or unexported fields
}

DatasetAPI provides methods for loading datasets with automatic type conversion so they can be easily used in evals.

func (*DatasetAPI[I, R]) Get

func (d *DatasetAPI[I, R]) Get(ctx context.Context, id string) (Dataset[I, R], error)

Get loads a dataset by ID and returns a Dataset iterator.

func (*DatasetAPI[I, R]) Query

func (d *DatasetAPI[I, R]) Query(ctx context.Context, opts DatasetQueryOpts) (Dataset[I, R], error)

Query loads a dataset with advanced query options.

type DatasetQueryOpts

type DatasetQueryOpts struct {
	// Name is the dataset name (requires project context)
	Name string

	// ID is the dataset ID
	ID string

	// Version specifies a specific dataset version
	Version string

	// Limit specifies the maximum number of records to return (0 = unlimited)
	Limit int
}

DatasetQueryOpts contains options for querying datasets.

type Evaluator

type Evaluator[I, R any] struct {
	// contains filtered or unexported fields
}

Evaluator provides a reusable way to run multiple evaluations with the same input and output types. This is useful when you need to run several evaluations in sequence with the same type signature, or use hosted prompts, scorers and datasets with automatic type conversion.

func NewEvaluator

func NewEvaluator[I, R any](session *auth.Session, cfg *config.Config, tp *trace.TracerProvider) *Evaluator[I, R]

NewEvaluator creates a new evaluator with explicit dependencies. The type parameters I (input) and R (result/output) must be specified explicitly. Most users should use braintrust.NewEvaluator(client).

func (*Evaluator[I, R]) Datasets

func (e *Evaluator[I, R]) Datasets() *DatasetAPI[I, R]

Datasets returns a DatasetAPI for loading datasets with this evaluator's type parameters.

func (*Evaluator[I, R]) Run

func (e *Evaluator[I, R]) Run(ctx context.Context, opts Opts[I, R]) (*Result, error)

Run executes an evaluation using this evaluator's dependencies.

func (*Evaluator[I, R]) Scorers

func (e *Evaluator[I, R]) Scorers() *ScorerAPI[I, R]

Scorers returns a ScorerAPI for loading scorers with this evaluator's type parameters.

func (*Evaluator[I, R]) Tasks

func (e *Evaluator[I, R]) Tasks() *TaskAPI[I, R]

Tasks returns a TaskAPI for loading tasks/prompts with this evaluator's type parameters.

type Metadata

type Metadata map[string]any

Metadata is a map of strings to a JSON-encodable value.

type Opts

type Opts[I, R any] struct {
	// Experiment is the name of the experiment to create or use.
	// Required.
	Experiment string

	// ProjectName is the name of the project to create the experiment in.
	// Optional. If not specified, uses the default project from config.
	ProjectName string

	// Dataset is an iterator over the test cases to evaluate.
	// Can be literal in-memory cases or API-backed dataset.
	// Required.
	Dataset Dataset[I, R]

	// Task is the function to evaluate for each case.
	// It receives the input and should return the output.
	// Required.
	Task TaskFunc[I, R]

	// Scorers are the scoring functions to apply to each case result.
	// Optional. If empty, no scoring is performed.
	Scorers []Scorer[I, R]

	// Tags are labels to attach to the experiment.
	// Optional.
	Tags []string

	// Metadata is additional metadata to attach to the experiment.
	// Optional.
	Metadata map[string]interface{}

	// Update controls whether to update an existing experiment or fail if it exists.
	// Optional. Defaults to false.
	Update bool

	// Parallelism controls the number of goroutines to use for parallel execution.
	// Optional. Defaults to 1 (sequential execution).
	// Set to a value > 1 to enable parallel case evaluation.
	Parallelism int

	// Quiet controls whether to suppress printing the result summary.
	// Optional. Defaults to false (summary is printed).
	// Set to true to suppress output.
	Quiet bool
}

Opts defines the options for running an evaluation. I is the input type and R is the result/output type.

type Result

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

Result contains the results of an evaluation.

func Run

func Run[I, R any](ctx context.Context, opts Opts[I, R], cfg *config.Config, session *auth.Session, tp *trace.TracerProvider) (*Result, error)

Run executes an evaluation using client resources (config, session, tracerProvider). This is the main entry point for eval execution.

func (*Result) Error

func (r *Result) Error() error

Error returns an errors that were encountered while running the eval.

func (*Result) ID

func (r *Result) ID() string

ID returns the experiment ID.

func (*Result) Name

func (r *Result) Name() string

Name returns the experiment name.

func (r *Result) Permalink() (string, error)

Permalink returns link to this eval in the Braintrust UI.

func (*Result) String

func (r *Result) String() string

String returns a string representaton of the result for printing on the console.

The format it prints will change and shouldn't be relied on for programmatic use.

type Score

type Score struct {
	// Name is the name of the score (e.g., "accuracy", "exact_match").
	Name string

	// Score is the numeric score value.
	Score float64

	// Metadata is optional additional metadata for this score.
	Metadata map[string]interface{}
}

Score represents a single score result.

type ScoreFunc

type ScoreFunc[I, R any] func(ctx context.Context, result TaskResult[I, R]) (Scores, error)

ScoreFunc is a function that evaluates a task result and returns a list of Scores.

type Scorer

type Scorer[I, R any] interface {
	// Name returns the name of this scorer.
	Name() string
	// Run evaluates the task result.
	// It returns one or more Score results.
	Run(context.Context, TaskResult[I, R]) (Scores, error)
}

Scorer is an interface for scoring the output of a task.

func NewScorer

func NewScorer[I, R any](name string, scoreFunc ScoreFunc[I, R]) Scorer[I, R]

NewScorer creates a new scorer with the given name and score function.

type ScorerAPI

type ScorerAPI[I, R any] struct {
	// contains filtered or unexported fields
}

ScorerAPI provides methods for loading scorers for evaluation.

func (*ScorerAPI[I, R]) Get

func (s *ScorerAPI[I, R]) Get(ctx context.Context, slug string) (Scorer[I, R], error)

Get loads a scorer by slug and returns a Scorer.

func (*ScorerAPI[I, R]) Query

func (s *ScorerAPI[I, R]) Query(ctx context.Context, opts ScorerQueryOpts) ([]ScorerInfo, error)

Query searches for scorers matching the given options.

type ScorerInfo

type ScorerInfo struct {
	ID      string
	Name    string
	Project string
}

ScorerInfo contains metadata about a scorer.

type ScorerQueryOpts

type ScorerQueryOpts struct {
	// Project filters scorers by project
	Project string

	// Name filters by specific name
	Name string
}

ScorerQueryOpts contains options for querying scorers.

type Scores

type Scores = []Score

Scores is a collection of Score results returned by scorers.

func S

func S(score float64) Scores

S is a helper function to concisely return a single score from scorers. Scores created with S will default to the name of the scorer that creates them. S(0.5) is equivalent to Scores{{Score: 0.5}}.

type TaskAPI

type TaskAPI[I, R any] struct {
	// contains filtered or unexported fields
}

TaskAPI provides methods for loading tasks/prompts for evaluation.

func (*TaskAPI[I, R]) Get

func (t *TaskAPI[I, R]) Get(ctx context.Context, slug string) (TaskFunc[I, R], error)

Get loads a task/prompt by slug and returns a TaskFunc.

type TaskFunc

type TaskFunc[I, R any] func(ctx context.Context, input I, hooks *TaskHooks) (TaskOutput[R], error)

TaskFunc is the signature for evaluation task functions. It receives the input, hooks for accessing eval context, and returns a TaskOutput.

func T

func T[I, R any](fn func(ctx context.Context, input I) (R, error)) TaskFunc[I, R]

T is a simple adapter that converts a basic task function into a TaskFunc. This is useful if your task is only concerned with inputs and outputs. Example:

task := eval.T(func(ctx context.Context, input string) (string, error) {
	return input, nil
})

eval.Run(ctx, eval.Opts[string, string]{Task: task, Dataset: cases}, cfg, session, tp)

type TaskHooks

type TaskHooks struct {
	// The eval and task spans are included, if you want to add custom attributes or events.
	TaskSpan oteltrace.Span
	EvalSpan oteltrace.Span

	// Readonly fields. These aren't necessarily recommended to be included in the task function,
	// but are available for advanced use cases.
	Expected any // type-assert
	Metadata Metadata
	Tags     []string
}

TaskHooks provides access to evaluation context within a task. All fields are read-only except for span modification.

type TaskOutput

type TaskOutput[R any] struct {
	Value R
}

TaskOutput wraps the output value from a task.

type TaskResult

type TaskResult[I, R any] struct {
	Input    I        // The case input
	Expected R        // What we expected
	Output   R        // What the task actually returned
	Metadata Metadata // Case metadata
}

TaskResult represents the complete result of executing a task on a case. This is passed to scorers for evaluation.

Jump to

Keyboard shortcuts

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