Documentation
¶
Overview ¶
Package eval provides evaluation functionality for the Braintrust SDK. This package is designed to work with the client-based architecture and does not rely on global state.
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. This allows lazy loading of cases without requiring them all in memory. Implementations must return io.EOF when iteration is complete. Can represent literal in-memory cases or API-backed datasets.
func NewDataset ¶
NewDataset creates a Dataset iterator from a slice of cases. This is a convenience function for the common case of having all cases in memory. The resulting dataset has no ID or version (literal in-memory cases).
type DatasetAPI ¶
type DatasetAPI[I, R any] struct { // contains filtered or unexported fields }
DatasetAPI provides methods for loading datasets for evaluation. It wraps api.DatasetsClient to provide typed Case[I, R] conversion and pagination handling.
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 (direct lookup)
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 dataasets with automatirc 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.
type Metadata ¶
Metadata is a map of strings to a JSON-encodable value. It is used to store arbitrary metadata about a case.
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.
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 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(ctx context.Context, result TaskResult[I, R]) (Scores, error) }
Scorer is an interface for scoring the output of a task.
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]) Query ¶
func (s *ScorerAPI[I, R]) Query(ctx context.Context, opts ScorerQueryOpts) ([]ScorerInfo, error)
Query searches for scorers matching the given options.
type ScorerInfo ¶
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 TaskAPI ¶
type TaskAPI[I, R any] struct { // contains filtered or unexported fields }
TaskAPI provides methods for loading tasks/prompts for evaluation.
type TaskFunc ¶
TaskFunc is the signature for evaluation task functions. It receives the input, hooks for accessing eval context, and returns a TaskOutput.
type TaskHooks ¶
type TaskHooks struct {
Expected any // Expected output (type-assert when needed)
Metadata Metadata // Case metadata (read-only)
Tags []string // Case tags (read-only)
TaskSpan oteltrace.Span // Current task execution span (can modify)
EvalSpan oteltrace.Span // Parent case/eval span (can modify)
}
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. Future fields may include metadata, skip flags, etc.
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.