Documentation
¶
Overview ¶
Package evalgo is a small, native-Go LLM evaluation framework.
Philosophy (vs. Python's DeepEval/RAGAS): extend Go's strengths — concurrency, determinism, zero-cost heuristics, and `go test` — instead of importing a heavy framework. Metrics split into two families:
- Deterministic (code-based): JSON/regex/contains/exact — fast, free, no LLM.
- Semantic (LLM-as-a-judge): rubric, faithfulness, answer relevancy, context precision — capture intent via a Judge.
The core package has zero third-party dependencies (stdlib only). The agent-go judge adapter lives in the sibling package ./llmjudge so library users who only want deterministic metrics pay no dependency cost.
Example ¶
Example shows Eval-Go used as a library: build a Suite of deterministic metrics over an in-memory golden set and inspect the aggregated report. (Semantic metrics additionally take a judge — see package llmjudge.)
package main
import (
"context"
"fmt"
evalgo "github.com/liliang-cn/eval-go"
)
func main() {
suite := evalgo.Suite{
Metrics: []evalgo.Metric{
evalgo.ValidJSON(),
evalgo.JSONHasFields("name", "age"),
},
Samples: []evalgo.Sample{
{Name: "ok", Output: `{"name":"Jane","age":30}`},
{Name: "missing-age", Output: `{"name":"Jane"}`},
},
}
report := suite.Run(context.Background())
for _, ms := range report.Summary() {
fmt.Printf("%s: %d/%d passed\n", ms.Metric, ms.Passed, ms.Total)
}
fmt.Println("failed:", report.Failed())
}
Output: json_has_fields: 1/2 passed valid_json: 2/2 passed failed: true
Index ¶
- func RegisteredMetrics() []string
- func RunT(t *testing.T, s Suite)
- type JudgeFunc
- type Metric
- func AnswerRelevancy(judge JudgeFunc, passThreshold float64) Metric
- func BuildMetrics(names []string, spec MetricSpec) ([]Metric, error)
- func CitationPresent() Metric
- func Contains(substr string) Metric
- func ContextualPrecision(judge JudgeFunc, passThreshold float64) Metric
- func ExactMatch() Metric
- func Faithfulness(judge JudgeFunc) Metric
- func ForbidsRegex(pattern string) Metric
- func JSONHasFields(fields ...string) Metric
- func MatchesRegex(pattern string) Metric
- func NonEmpty() Metric
- func RubricJudge(judge JudgeFunc, passThreshold float64) Metric
- func ValidJSON() Metric
- type MetricFunc
- type MetricSpec
- type MetricSummary
- type Report
- type Result
- type Sample
- type SampleReport
- type Suite
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisteredMetrics ¶
func RegisteredMetrics() []string
RegisteredMetrics returns the metric names known to BuildMetrics, sorted.
func RunT ¶
RunT drives a Suite through Go's native test runner: one subtest per sample, executed with t.Parallel() so hundreds of rows evaluate concurrently. Failed metrics become t.Error, so `go test` exit codes and CI reporting work for free.
func TestRAG(t *testing.T) {
if os.Getenv("RUN_EVALS") != "true" { t.Skip("set RUN_EVALS=true") }
evalgo.RunT(t, suite)
}
Types ¶
type JudgeFunc ¶
JudgeFunc executes one LLM-as-a-judge call: given a prompt, return raw text (expected to contain JSON). Wrap with RateLimit to pace provider QPS. Build one from agent-go via the sibling package ./llmjudge.
func RateLimit ¶
RateLimit wraps a JudgeFunc with a lazy token-bucket limiter so concurrent samples don't trigger 429 Too Many Requests from the judge provider.
rps is tokens refilled per second; burst is the bucket capacity. Production code may prefer golang.org/x/time/rate; this is an equivalent, dependency-free implementation kept in-tree to honor the framework's stdlib-only core.
type Metric ¶
Metric scores a single sample. Deterministic metrics ignore ctx; semantic metrics use it for the judge call (cancellation / deadlines).
func AnswerRelevancy ¶
AnswerRelevancy measures how much of Output actually addresses Input: extract the answer's statements, classify each as relevant to the question. Score = relevant statements / total. Catches rambling / off-topic answers.
func BuildMetrics ¶
func BuildMetrics(names []string, spec MetricSpec) ([]Metric, error)
BuildMetrics resolves metric names into Metrics. It errors on an unknown name, or on a semantic metric requested without a judge — so misconfiguration fails fast instead of silently skipping evaluation.
func CitationPresent ¶
func CitationPresent() Metric
CitationPresent passes when Output contains at least one [SOURCE]-style citation — a deterministic proxy for grounded, attributable RAG answers.
func ContextualPrecision ¶
ContextualPrecision measures retrieval quality: of the Context chunks supplied, how many are actually relevant to the Input. Score = relevant chunks / total. Low precision means the retriever is surfacing noise.
func ExactMatch ¶
func ExactMatch() Metric
ExactMatch passes when trimmed Output equals trimmed Expected.
func Faithfulness ¶
Faithfulness measures how grounded Output is in Context: extract the claims the answer makes, then check each against the retrieval context. Score = non-contradicted claims / total claims. Catches hallucination.
func ForbidsRegex ¶
ForbidsRegex passes when Output does NOT match the pattern — a safety boundary (no secrets, no banned phrases, no leaked PII).
func JSONHasFields ¶
JSONHasFields passes when Output is a JSON object containing all given keys.
func MatchesRegex ¶
MatchesRegex passes when Output matches the pattern (e.g. a required format).
func RubricJudge ¶
RubricJudge is a G-Eval-style metric: the judge scores Output against Sample.Rubric with chain-of-thought, returning {passed, score, reason}. passThreshold is the minimum score (0..1) required to pass.
type MetricFunc ¶
MetricFunc adapts a function into a Metric.
func (MetricFunc) Name ¶
func (m MetricFunc) Name() string
type MetricSpec ¶
type MetricSpec struct {
Judge JudgeFunc // required for semantic metrics; may be nil for deterministic-only
Threshold float64 // pass threshold for relevancy / context_precision / rubric (default 0.5)
}
MetricSpec configures the metrics built by BuildMetrics.
type MetricSummary ¶
type MetricSummary struct {
Metric string `json:"metric"`
PassRate float64 `json:"pass_rate"` // 0..1
MeanScore float64 `json:"mean_score"`
Passed int `json:"passed"`
Total int `json:"total"`
}
MetricSummary aggregates one metric across all samples.
type Report ¶
type Report struct {
Samples []SampleReport `json:"samples"`
}
Report is the full outcome of a Suite run.
func (Report) Failed ¶
Failed reports whether any sample failed any metric — use as the CI exit gate.
func (Report) Summary ¶
func (r Report) Summary() []MetricSummary
Summary computes per-metric aggregates, ordered by metric name.
func (Report) WriteConsole ¶
WriteConsole emits a human-readable report: per-sample metric grid + per-metric aggregates + overall verdict.
type Result ¶
type Result struct {
Metric string `json:"metric"`
Score float64 `json:"score"`
Passed bool `json:"passed"`
Reason string `json:"reason,omitempty"`
Err string `json:"error,omitempty"`
}
Result is the outcome of one metric on one sample. Score is normalized 0..1.
type Sample ¶
type Sample struct {
Name string `json:"name"` // unique-ish label for reports
Input string `json:"input"` // the question / prompt given to the system
Output string `json:"output"` // the actual answer produced by the system under test
Expected string `json:"expected,omitempty"` // optional reference answer (for ExactMatch etc.)
Context []string `json:"context,omitempty"` // retrieved evidence chunks (for RAG metrics)
Rubric string `json:"rubric,omitempty"` // pass/fail criterion for a rubric judge
Meta map[string]string `json:"meta,omitempty"` // free-form labels carried into reports
}
Sample is one row of a golden dataset plus the system's actual output.
type SampleReport ¶
type SampleReport struct {
Sample string `json:"sample"`
Meta map[string]string `json:"meta,omitempty"`
Results []Result `json:"results"`
Passed bool `json:"passed"`
}
SampleReport holds every metric Result for one sample.
type Suite ¶
type Suite struct {
Samples []Sample
Metrics []Metric
Concurrency int // samples evaluated in parallel; default 4
}
Suite is a dataset + the metrics to apply, run concurrently across samples.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
evalgo
command
Command evalgo is a CLI for evaluating LLM / RAG outputs from a golden dataset.
|
Command evalgo is a CLI for evaluating LLM / RAG outputs from a golden dataset. |
|
Package llmjudge adapts an agent-go LLM client into an evalgo.JudgeFunc.
|
Package llmjudge adapts an agent-go LLM client into an evalgo.JudgeFunc. |