scorer

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package scorer defines the interface and built-in implementations for evaluating AI outputs. Scorers range from deterministic string matchers to AI-based judges and persona-aware evaluators.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BehaviorTriggerScorer

type BehaviorTriggerScorer struct {
	// BehaviorName is the name of the behavior being tested.
	BehaviorName string
	// ExpectedActions are actions that should appear in the trace.
	ExpectedActions []string
	// TriggerKeywords are keywords that indicate the trigger was detected.
	TriggerKeywords []string
}

BehaviorTriggerScorer analyzes the agent trace to verify that specific behaviors were triggered in response to conditions.

func NewBehaviorTriggerScorer

func NewBehaviorTriggerScorer(name string, expectedActions, triggerKeywords []string) *BehaviorTriggerScorer

NewBehaviorTriggerScorer creates a behavior trigger scorer.

func (*BehaviorTriggerScorer) Name

func (s *BehaviorTriggerScorer) Name() string

func (*BehaviorTriggerScorer) Score

func (s *BehaviorTriggerScorer) Score(_ context.Context, input *Input) (*Output, error)

type CognitivePhaseScorer

type CognitivePhaseScorer struct {
	// ExpectedPhases is the ordered list of cognitive phases expected.
	ExpectedPhases []string
}

CognitivePhaseScorer analyzes the agent trace for thinking strategy transitions — does the agent progress through expected cognitive phases (e.g. analysis → planning → execution → verification)?

func NewCognitivePhaseScorer

func NewCognitivePhaseScorer(expectedPhases []string) *CognitivePhaseScorer

NewCognitivePhaseScorer creates a cognitive phase scorer.

func (*CognitivePhaseScorer) Name

func (s *CognitivePhaseScorer) Name() string

func (*CognitivePhaseScorer) Score

func (s *CognitivePhaseScorer) Score(_ context.Context, input *Input) (*Output, error)

type CommunicationStyleScorer

type CommunicationStyleScorer struct {
	Client         LLMClient
	Model          string
	ExpectedTone   string
	ExpectedFormat string
}

CommunicationStyleScorer uses an LLM to evaluate tone, formality, and verbosity of the agent output.

func NewCommunicationStyleScorer

func NewCommunicationStyleScorer(client LLMClient, model, tone, format string) *CommunicationStyleScorer

NewCommunicationStyleScorer creates a communication style scorer.

func (*CommunicationStyleScorer) Name

func (s *CommunicationStyleScorer) Name() string

func (*CommunicationStyleScorer) Score

func (s *CommunicationStyleScorer) Score(ctx context.Context, input *Input) (*Output, error)

type ContainsScorer

type ContainsScorer struct {
	Substring       string
	CaseInsensitive bool
}

ContainsScorer scores 1.0 if the actual output contains the expected substring.

func (*ContainsScorer) Name

func (s *ContainsScorer) Name() string

func (*ContainsScorer) Score

func (s *ContainsScorer) Score(_ context.Context, input *Input) (*Output, error)

type CostScorer

type CostScorer struct {
	MaxCost float64
}

CostScorer scores 1.0 if the cost is within the threshold. Cost is read from input.Context["cost"].

func (*CostScorer) Name

func (s *CostScorer) Name() string

func (*CostScorer) Score

func (s *CostScorer) Score(_ context.Context, input *Input) (*Output, error)

type CustomScorer

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

CustomScorer wraps a user-provided function as a Scorer.

func FromFunc

func FromFunc(name string, fn Func) *CustomScorer

FromFunc creates a named scorer from a function.

func (*CustomScorer) Name

func (s *CustomScorer) Name() string

func (*CustomScorer) Score

func (s *CustomScorer) Score(ctx context.Context, input *Input) (*Output, error)

type Descriptor

type Descriptor struct {
	Name        string
	Revision    string // immutable implementation revision; required for frozen evaluations
	Description string
	Dimension   string // the human-like dimension it reports, empty for none
	UsesLLM     bool   // true when scoring makes a model call, which costs money
	// RequiresConfig is true when the scorer cannot be built without
	// config, so a run-level pick with no config would be refused.
	RequiresConfig bool
}

Descriptor says what a registered scorer is, for anyone listing them.

type EmbeddingClient

type EmbeddingClient interface {
	Embed(ctx context.Context, texts []string) ([][]float64, error)
}

EmbeddingClient generates vector embeddings for text.

type ExactScorer

type ExactScorer struct {
	CaseInsensitive bool
}

ExactScorer scores 1.0 if the actual output exactly matches the expected output.

func (*ExactScorer) Name

func (s *ExactScorer) Name() string

func (*ExactScorer) Score

func (s *ExactScorer) Score(_ context.Context, input *Input) (*Output, error)

type Factory

type Factory func(config map[string]any) (Scorer, error)

Factory creates a scorer from a configuration map.

type FactualScorer

type FactualScorer struct {
	Client LLMClient
	Model  string
}

FactualScorer evaluates factual accuracy using an LLM judge.

func NewFactualScorer

func NewFactualScorer(client LLMClient, model string) *FactualScorer

NewFactualScorer creates a factual accuracy scorer.

func (*FactualScorer) Name

func (s *FactualScorer) Name() string

func (*FactualScorer) Score

func (s *FactualScorer) Score(ctx context.Context, input *Input) (*Output, error)

type Func

type Func func(ctx context.Context, input *Input) (*Output, error)

Func is a function that implements the Scorer interface.

type HallucinationScorer

type HallucinationScorer struct {
	Client LLMClient
	Model  string
}

HallucinationScorer detects hallucinated content using an LLM judge.

func NewHallucinationScorer

func NewHallucinationScorer(client LLMClient, model string) *HallucinationScorer

NewHallucinationScorer creates a hallucination detection scorer.

func (*HallucinationScorer) Name

func (s *HallucinationScorer) Name() string

func (*HallucinationScorer) Score

func (s *HallucinationScorer) Score(ctx context.Context, input *Input) (*Output, error)

type Input

type Input struct {
	Input    string            // User input / prompt
	Expected string            // Expected output (optional)
	Actual   string            // Actual output
	Trace    *evalrun.RunTrace // Agent execution trace (optional, for persona scorers)
	Context  map[string]any    // Additional context
}

Input provides all data needed for scoring.

type JSONSchemaScorer

type JSONSchemaScorer struct {
	Schema string
}

JSONSchemaScorer scores 1.0 if the actual output is valid JSON that matches a basic structural expectation. For full JSON Schema validation, provide a schema string.

func (*JSONSchemaScorer) Name

func (s *JSONSchemaScorer) Name() string

func (*JSONSchemaScorer) Score

func (s *JSONSchemaScorer) Score(_ context.Context, input *Input) (*Output, error)

type JSONValidScorer

type JSONValidScorer struct{}

JSONValidScorer scores 1.0 if the actual output is valid JSON.

func (*JSONValidScorer) Name

func (s *JSONValidScorer) Name() string

func (*JSONValidScorer) Score

func (s *JSONValidScorer) Score(_ context.Context, input *Input) (*Output, error)

type LLMClient

type LLMClient interface {
	Complete(ctx context.Context, model, systemPrompt, userInput string, temperature float64) (string, error)
}

LLMClient is the interface for calling a language model for scoring.

type LLMJudgeScorer

type LLMJudgeScorer struct {
	Client      LLMClient
	Model       string
	Criteria    string
	Temperature float64
}

LLMJudgeScorer uses an LLM as a judge to evaluate outputs.

func NewLLMJudgeScorer

func NewLLMJudgeScorer(client LLMClient, model, criteria string) *LLMJudgeScorer

NewLLMJudgeScorer creates a new LLM-as-judge scorer.

func (*LLMJudgeScorer) Name

func (s *LLMJudgeScorer) Name() string

func (*LLMJudgeScorer) Score

func (s *LLMJudgeScorer) Score(ctx context.Context, input *Input) (*Output, error)

type LatencyScorer

type LatencyScorer struct {
	MaxMs int
}

LatencyScorer scores 1.0 if the latency is within the threshold. Latency is read from input.Context["latency_ms"].

func (*LatencyScorer) Name

func (s *LatencyScorer) Name() string

func (*LatencyScorer) Score

func (s *LatencyScorer) Score(_ context.Context, input *Input) (*Output, error)

type LengthScorer

type LengthScorer struct {
	MinTokens int
	MaxTokens int
}

LengthScorer scores based on the token count of the output. Uses a simple word-split approximation for token counting.

func (*LengthScorer) Name

func (s *LengthScorer) Name() string

func (*LengthScorer) Score

func (s *LengthScorer) Score(_ context.Context, input *Input) (*Output, error)

type NotContainsScorer

type NotContainsScorer struct {
	Substring       string
	CaseInsensitive bool
}

NotContainsScorer scores 1.0 if the actual output does NOT contain the substring.

func (*NotContainsScorer) Name

func (s *NotContainsScorer) Name() string

func (*NotContainsScorer) Score

func (s *NotContainsScorer) Score(_ context.Context, input *Input) (*Output, error)

type Output

type Output struct {
	Usage     *evalrun.Usage // Optional judge accounting; nil means unknown, not zero.
	Score     float64        // 0.0 to 1.0
	Passed    bool           // Score >= threshold
	Reason    string         // Human-readable explanation
	Dimension string         // Which evaluation dimension (skill, trait, etc.)
	Details   map[string]any // Scorer-specific details
}

Output captures the result of a scoring evaluation.

type PerceptionFocusScorer

type PerceptionFocusScorer struct {
	// RequiredKeywords are terms the output must reference.
	RequiredKeywords []string
	// IgnoredKeywords are distractors the output should not focus on.
	IgnoredKeywords []string
}

PerceptionFocusScorer evaluates whether the agent attended to the right keywords and patterns in its input, measuring perceptual focus.

func NewPerceptionFocusScorer

func NewPerceptionFocusScorer(required, ignored []string) *PerceptionFocusScorer

NewPerceptionFocusScorer creates a perception focus scorer.

func (*PerceptionFocusScorer) Name

func (s *PerceptionFocusScorer) Name() string

func (*PerceptionFocusScorer) Score

func (s *PerceptionFocusScorer) Score(_ context.Context, input *Input) (*Output, error)

type PersonaCoherenceScorer

type PersonaCoherenceScorer struct {
	Client      LLMClient
	Model       string
	PersonaDesc string
}

PersonaCoherenceScorer performs holistic LLM-based evaluation of whether the agent maintains a consistent identity across all dimensions.

func NewPersonaCoherenceScorer

func NewPersonaCoherenceScorer(client LLMClient, model, personaDesc string) *PersonaCoherenceScorer

NewPersonaCoherenceScorer creates a persona coherence scorer.

func (*PersonaCoherenceScorer) Name

func (s *PersonaCoherenceScorer) Name() string

func (*PersonaCoherenceScorer) Score

func (s *PersonaCoherenceScorer) Score(ctx context.Context, input *Input) (*Output, error)

type RegexScorer

type RegexScorer struct {
	Pattern string
	// contains filtered or unexported fields
}

RegexScorer scores 1.0 if the actual output matches the given regex pattern.

func NewRegexScorer

func NewRegexScorer(pattern string) (*RegexScorer, error)

NewRegexScorer creates a RegexScorer with a pre-compiled pattern.

func (*RegexScorer) Name

func (s *RegexScorer) Name() string

func (*RegexScorer) Score

func (s *RegexScorer) Score(_ context.Context, input *Input) (*Output, error)

type Registry

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

Registry holds named scorer factories and creates scorer instances from config.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a scorer registry with built-in scorers pre-registered.

func (*Registry) Descriptors

func (r *Registry) Descriptors() []Descriptor

Descriptors lists every registered scorer, sorted by name.

func (*Registry) Get

func (r *Registry) Get(name string, config map[string]any) (Scorer, error)

Get creates a scorer instance from a name and config.

func (*Registry) GetVersioned

func (r *Registry) GetVersioned(name, revision string, config map[string]any) (Scorer, error)

GetVersioned resolves exactly the implementation a plan captured.

func (*Registry) Has

func (r *Registry) Has(name string) bool

Has reports whether a scorer is registered under name.

func (*Registry) Register

func (r *Registry) Register(name string, factory Factory)

Register adds a scorer factory by name, with no description.

func (*Registry) RegisterDescribed

func (r *Registry) RegisterDescribed(d Descriptor, factory Factory)

RegisterDescribed adds a scorer factory with a descriptor.

func (*Registry) Revision

func (r *Registry) Revision(name string) (string, error)

Revision returns the registered implementation revision for a frozen plan.

type Scorer

type Scorer interface {
	Name() string
	Score(ctx context.Context, input *Input) (*Output, error)
}

Scorer evaluates an AI output and returns a score between 0.0 and 1.0.

type SemanticScorer

type SemanticScorer struct {
	Client    EmbeddingClient
	Threshold float64
}

SemanticScorer evaluates semantic similarity between expected and actual outputs using embedding cosine similarity.

func NewSemanticScorer

func NewSemanticScorer(client EmbeddingClient, threshold float64) *SemanticScorer

NewSemanticScorer creates a semantic similarity scorer. Threshold is the minimum cosine similarity to pass (default 0.8).

func (*SemanticScorer) Name

func (s *SemanticScorer) Name() string

func (*SemanticScorer) Score

func (s *SemanticScorer) Score(ctx context.Context, input *Input) (*Output, error)

type SkillUsageScorer

type SkillUsageScorer struct {
	ExpectedTools  []string
	ForbiddenTools []string
}

SkillUsageScorer analyzes agent trace tool calls to verify expected tool usage and detect forbidden tool usage.

func NewSkillUsageScorer

func NewSkillUsageScorer(expected, forbidden []string) *SkillUsageScorer

NewSkillUsageScorer creates a skill usage scorer.

func (*SkillUsageScorer) Name

func (s *SkillUsageScorer) Name() string

func (*SkillUsageScorer) Score

func (s *SkillUsageScorer) Score(_ context.Context, input *Input) (*Output, error)

type TraitConsistencyScorer

type TraitConsistencyScorer struct {
	Client    LLMClient
	Model     string
	TraitName string
	TraitDesc string
}

TraitConsistencyScorer uses an LLM judge to assess whether the agent's output is consistent with a specified personality trait.

func NewTraitConsistencyScorer

func NewTraitConsistencyScorer(client LLMClient, model, traitName, traitDesc string) *TraitConsistencyScorer

NewTraitConsistencyScorer creates a trait consistency scorer.

func (*TraitConsistencyScorer) Name

func (s *TraitConsistencyScorer) Name() string

func (*TraitConsistencyScorer) Score

func (s *TraitConsistencyScorer) Score(ctx context.Context, input *Input) (*Output, error)

Jump to

Keyboard shortcuts

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