trulens.benchmark.golden_set_generator¶
trulens.benchmark.golden_set_generator
¶
Golden set generation from production TruLens records.
The hardest part of aligning LLM judges is building a good golden set. TruLens already stores production records β inputs, outputs and judge scores β in its database, so instead of hand-writing lists of dicts, users can sample real traffic for human annotation.
GoldenSetGenerator
queries a TruSession for records (optionally filtered by app name, date range
or feedback score range), samples them with a configurable strategy (uniform
random, stratified across low/medium/high score buckets, or uncertainty β the
records whose judge scores sit closest to the decision boundary), and exports
the sample in the GroundTruthAgreement golden-set format or to CSV/JSON for
external annotation tools. Once annotated, the golden set can be validated,
loaded back, and persisted via TruSession.add_ground_truth_to_dataset.
Example
from trulens.benchmark.golden_set_generator import GoldenSetGenerator
from trulens.core.session import TruSession
session = TruSession()
generator = GoldenSetGenerator(session, seed=42)
# Sample 50 records, stratified by existing relevance scores.
sample = generator.sample(
n=50,
app_name="my_rag_app",
strategy="stratified",
feedback_name="relevance",
)
# Export for annotation.
sample.to_csv("golden_set_to_annotate.csv")
# After human annotation, validate, load back and persist.
annotated = generator.load_annotations("golden_set_annotated.csv")
generator.save_golden_set("my_golden_set", annotated)
Classes¶
GoldenSetSample
¶
A sample of records ready for human annotation.
Rows carry the golden-set columns (query, expected_response and an
empty expected_score to be filled by an annotator) plus provenance
columns (record_id, app_name, app_version, ts, and the sampled
judge's name and score when a feedback function was used for sampling).
| PARAMETER | DESCRIPTION |
|---|---|
df
|
Sample rows, one per record.
TYPE:
|
Functions¶
to_df
¶
to_list
¶
Return the sample as a list of golden-set dicts.
The result matches the GroundTruthAgreement golden-set format:
[{"query": ..., "expected_response": ..., "expected_score": None}].
GoldenSetGenerator
¶
Samples production records from a TruSession for golden-set curation.
| PARAMETER | DESCRIPTION |
|---|---|
session
|
The
TYPE:
|
seed
|
Optional seed making |
Functions¶
sample
¶
sample(
n: int,
app_name: Optional[str] = None,
app_version: Optional[str] = None,
strategy: str = STRATEGY_RANDOM,
feedback_name: Optional[str] = None,
start_time: Optional[Any] = None,
end_time: Optional[Any] = None,
min_score: Optional[float] = None,
max_score: Optional[float] = None,
decision_boundary: float = 0.5,
max_records: Optional[int] = None,
) -> GoldenSetSample
Sample records for annotation.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Number of records to sample. If fewer records match the filters, all matching records are returned with a warning.
TYPE:
|
app_name
|
Only sample records from this app. |
app_version
|
Only sample records from this app version. |
strategy
|
One of
TYPE:
|
feedback_name
|
Feedback function whose scores drive the
|
start_time
|
Only sample records at or after this timestamp
(anything |
end_time
|
Only sample records at or before this timestamp. |
min_score
|
Only sample records with |
max_score
|
Only sample records with |
decision_boundary
|
Center of the
TYPE:
|
max_records
|
Cap on how many records to fetch from the database before sampling. |
| RETURNS | DESCRIPTION |
|---|---|
GoldenSetSample
|
|
GoldenSetSample
|
with |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
On an unknown strategy, a missing/unknown
|
load_annotations
¶
load_annotations(
source: Union[str, DataFrame, Sequence[Dict]],
require_scores: bool = True,
score_range: Optional[Tuple[float, float]] = (0.0, 1.0),
) -> DataFrame
Load and validate an annotated golden set.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
Path to an annotated CSV or JSON file (as produced by GoldenSetSample.to_csv / GoldenSetSample.to_json), or an equivalent DataFrame / list of dicts. |
require_scores
|
Require every row to carry a numeric
TYPE:
|
score_range
|
Inclusive |
| RETURNS | DESCRIPTION |
|---|---|
DataFrame
|
A DataFrame ready for |
DataFrame
|
|
DataFrame
|
with |
DataFrame
|
into a per-row |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
On missing columns, empty queries, missing scores (when required) or out-of-range scores. |
save_golden_set
¶
save_golden_set(
dataset_name: str,
annotated: Union[str, DataFrame, Sequence[Dict]],
dataset_metadata: Optional[Dict[str, Any]] = None,
) -> int
Persist an annotated golden set as a TruLens dataset.
| PARAMETER | DESCRIPTION |
|---|---|
dataset_name
|
Name of the dataset to create or extend.
TYPE:
|
annotated
|
Annotated golden set β anything load_annotations accepts, or the DataFrame it returned. |
dataset_metadata
|
Optional metadata stored with the dataset. |
| RETURNS | DESCRIPTION |
|---|---|
int
|
The number of ground truth rows written. |