Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/main/java/dev/braintrust/api/BraintrustApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,9 @@ public Experiment getOrCreateExperiment(CreateExperimentRequest request) {
request.tags().orElse(List.of()),
request.metadata().orElse(Map.of()),
"notused",
"notused");
"notused",
request.datasetId(),
request.datasetVersion());
experiments.add(newExperiment);
return newExperiment;
}
Expand Down Expand Up @@ -734,7 +736,9 @@ record CreateExperimentRequest(
Optional<String> description,
Optional<String> baseExperimentId,
Optional<List<String>> tags,
Optional<Map<String, Object>> metadata) {
Optional<Map<String, Object>> metadata,
Optional<String> datasetId,
Optional<String> datasetVersion) {

public CreateExperimentRequest(String projectId, String name) {
this(
Expand All @@ -743,6 +747,8 @@ public CreateExperimentRequest(String projectId, String name) {
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty());
}
}
Expand All @@ -755,7 +761,32 @@ record Experiment(
List<String> tags,
Map<String, Object> metadata,
String createdAt,
String updatedAt) {}
String updatedAt,
Optional<String> datasetId,
Optional<String> datasetVersion) {
/** Convenience constructor */
public Experiment(
String id,
String projectId,
String name,
Optional<String> description,
List<String> tags,
Map<String, Object> metadata,
String createdAt,
String updatedAt) {
this(
id,
projectId,
name,
description,
tags,
metadata,
createdAt,
updatedAt,
Optional.empty(),
Optional.empty());
}
}

record CreateDatasetRequest(String projectId, String name, Optional<String> description) {
public CreateDatasetRequest(String projectId, String name) {
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/dev/braintrust/eval/Dataset.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public interface Dataset<INPUT, OUTPUT> {
/** Convenience method to safely iterate all items in a dataset. */
default void forEach(Consumer<DatasetCase<INPUT, OUTPUT>> consumer) {
try (var cursor = openCursor()) {
Optional<DatasetCase<INPUT, OUTPUT>> cursorCase = cursor.next();
while (cursorCase.isPresent()) {
consumer.accept(cursorCase.get());
cursorCase = cursor.next();
}
cursor.forEach(consumer);
}
}

Expand All @@ -47,6 +43,17 @@ interface Cursor<CASE> extends AutoCloseable {

/** close all cursor resources */
void close();

/** version of the dataset this cursor was opened against */
Optional<String> version();

default void forEach(Consumer<CASE> caseConsumer) {
Optional<CASE> c = next();
while (c.isPresent()) {
caseConsumer.accept(c.get());
c = next();
}
}
}

/** Create an in-memory Dataset containing the provided cases. */
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/dev/braintrust/eval/DatasetBrainstoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import dev.braintrust.api.BraintrustApiClient;
import java.util.*;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.jspecify.annotations.NonNull;

/** A dataset loaded externally from Braintrust using paginated API fetches */
public class DatasetBrainstoreImpl<INPUT, OUTPUT> implements Dataset<INPUT, OUTPUT> {
Expand Down Expand Up @@ -39,7 +41,25 @@ public Optional<String> version() {

@Override
public Cursor<DatasetCase<INPUT, OUTPUT>> openCursor() {
return new BrainstoreCursor();
return new BrainstoreCursor(null == pinnedVersion ? fetchMaxVersion() : pinnedVersion);
}

private String fetchMaxVersion() {
var response =
apiClient.btqlQuery(
"SELECT max(_xact_id) as version FROM dataset('%s')".formatted(datasetId));
if (response.data().isEmpty()) {
throw new RuntimeException(
"Failed to fetch max version for dataset: " + datasetId + " (empty response)");
}
var version = response.data().get(0).get("version");
if (version == null) {
throw new RuntimeException(
"Failed to fetch max version for dataset: "
+ datasetId
+ " (null version — dataset may be empty)");
}
return String.valueOf(version);
}

private class BrainstoreCursor implements Cursor<DatasetCase<INPUT, OUTPUT>> {
Expand All @@ -48,13 +68,15 @@ private class BrainstoreCursor implements Cursor<DatasetCase<INPUT, OUTPUT>> {
private @Nullable String cursor;
private boolean exhausted;
private boolean closed;
private final @Nonnull String cursorVersion;

BrainstoreCursor() {
BrainstoreCursor(@NonNull String cursorVersion) {
this.currentBatch = new ArrayList<>();
this.currentIndex = 0;
this.cursor = null;
this.exhausted = false;
this.closed = false;
this.cursorVersion = cursorVersion;
}

@Override
Expand Down Expand Up @@ -111,7 +133,7 @@ public Optional<DatasetCase<INPUT, OUTPUT>> next() {

private void fetchNextBatch() {
var request =
new BraintrustApiClient.DatasetFetchRequest(batchSize, cursor, pinnedVersion);
new BraintrustApiClient.DatasetFetchRequest(batchSize, cursor, cursorVersion);
var response = apiClient.fetchDatasetEvents(datasetId, request);

currentBatch = new ArrayList<>(response.events());
Expand All @@ -129,5 +151,10 @@ public void close() {
closed = true;
currentBatch.clear();
}

@Override
public Optional<String> version() {
return Optional.of(cursorVersion);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/dev/braintrust/eval/DatasetInMemoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public Optional<DatasetCase<INPUT, OUTPUT>> next() {
public void close() {
closed = true;
}

@Override
public Optional<String> version() {
return Optional.empty();
}
};
}
}
32 changes: 22 additions & 10 deletions src/main/java/dev/braintrust/eval/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,28 @@ private Eval(Builder<INPUT, OUTPUT> builder) {

/** Runs the evaluation and returns results. */
public EvalResult run() {
var experiment =
client.getOrCreateExperiment(
new BraintrustApiClient.CreateExperimentRequest(
orgAndProject.project().id(),
experimentName,
Optional.empty(),
Optional.empty(),
tags.isEmpty() ? Optional.empty() : Optional.of(tags),
metadata.isEmpty() ? Optional.empty() : Optional.of(metadata)));
dataset.forEach(datasetCase -> evalOne(experiment.id(), datasetCase));
try (var cursor = dataset.openCursor()) {
Optional<String> datasetVersion = Optional.empty();
Optional<String> datasetId = Optional.empty();
if (dataset instanceof DatasetBrainstoreImpl<INPUT, OUTPUT>) {
// TODO: come up with a means of expressing this naturally in the dataset api
datasetVersion = cursor.version();
datasetId = Optional.of(dataset.id());
}
var experiment =
client.getOrCreateExperiment(
new BraintrustApiClient.CreateExperimentRequest(
orgAndProject.project().id(),
experimentName,
Optional.empty(),
Optional.empty(),
tags.isEmpty() ? Optional.empty() : Optional.of(tags),
metadata.isEmpty() ? Optional.empty() : Optional.of(metadata),
datasetId,
datasetVersion));
cursor.forEach(datasetCase -> evalOne(experiment.id(), datasetCase));
}

var experimentUrl =
"%s/experiments/%s"
.formatted(
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/dev/braintrust/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,48 @@ void evalWithExperimentTagsAndMetadata() {
assertEquals(expectedTags, experiment.tags(), "Experiment should have tags");
assertEquals(expectedMetadata, experiment.metadata(), "Experiment should have metadata");
}

@Test
@SneakyThrows
void evalLinksToRemoteDataset() {
if (TestHarness.getVcrMode() == VCR.VcrMode.REPLAY) {
return;
}

var experimentName = "test-dataset-linking";
Dataset<String, String> dataset = testHarness.braintrust().fetchDataset("food");

var eval =
testHarness
.braintrust()
.<String, String>evalBuilder()
.name(experimentName)
.dataset(dataset)
.taskFunction(String::toUpperCase)
.scorers(
Scorer.of(
"exact",
(expected, result) -> expected.equals(result) ? 1.0 : 0.0))
.build();
eval.run();
testHarness.awaitExportedSpans();

// Verify the experiment is linked to the dataset
var experiments =
testHarness
.braintrust()
.apiClient()
.listExperiments(TestHarness.defaultProjectId());
var experiment =
experiments.stream()
.filter(e -> e.name().equals(experimentName))
.findFirst()
.orElseThrow(() -> new AssertionError("Experiment not found"));

assertTrue(experiment.datasetId().isPresent(), "Experiment should be linked to a dataset");
assertEquals(dataset.id(), experiment.datasetId().get(), "Dataset ID should match");
assertTrue(
experiment.datasetVersion().isPresent(),
"Experiment should have a dataset version");
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
event: message_start
data: {"type":"message_start","message":{"model":"claude-3-5-haiku-20241022","id":"msg_01VCp1RHSod6yjMZgMhvo63L","type":"message","role":"assistant","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":5,"service_tier":"standard","inference_geo":"not_available"}} }
data: {"type":"message_start","message":{"model":"claude-3-5-haiku-20241022","id":"msg_01NhbxNFB9uMvk5gvd7qG2Ve","type":"message","role":"assistant","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":5,"service_tier":"standard","inference_geo":"not_available"}} }

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""} }
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""} }

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"The capital of France is"} }
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"The capital of France is"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Paris."} }
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Paris."} }

event: content_block_stop
data: {"type":"content_block_stop","index":0}
data: {"type":"content_block_stop","index":0 }

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":10} }
data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":10} }

event: message_stop
data: {"type":"message_stop" }
data: {"type":"message_stop" }

Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"model":"claude-3-5-haiku-20241022","id":"msg_01QZ2Kog1CjtsKvGZnAbYYSQ","type":"message","role":"assistant","content":[{"type":"text","text":"The capital of France is Paris."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":10,"service_tier":"standard","inference_geo":"not_available"}}
{"model":"claude-3-5-haiku-20241022","id":"msg_01SdXWAvhUNSc6wMD8ii2Ma7","type":"message","role":"assistant","content":[{"type":"text","text":"The capital of France is Paris."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":19,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":10,"service_tier":"standard","inference_geo":"not_available"}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id" : "71f8c84f-b988-4c20-ab41-8e0f5e20325c",
"id" : "14d7ddaf-c4dc-4e41-ae83-1b9b0e7e222c",
"name" : "v1_messages",
"request" : {
"url" : "/v1/messages",
Expand All @@ -17,35 +17,35 @@
},
"response" : {
"status" : 200,
"bodyFileName" : "v1_messages-71f8c84f-b988-4c20-ab41-8e0f5e20325c.txt",
"bodyFileName" : "v1_messages-14d7ddaf-c4dc-4e41-ae83-1b9b0e7e222c.txt",
"headers" : {
"Date" : "Fri, 06 Feb 2026 01:36:11 GMT",
"Date" : "Wed, 18 Feb 2026 04:40:37 GMT",
"Content-Type" : "text/event-stream; charset=utf-8",
"Cache-Control" : "no-cache",
"anthropic-ratelimit-requests-limit" : "10000",
"anthropic-ratelimit-requests-remaining" : "9999",
"anthropic-ratelimit-requests-reset" : "2026-02-06T01:36:11Z",
"anthropic-ratelimit-requests-reset" : "2026-02-18T04:40:36Z",
"anthropic-ratelimit-input-tokens-limit" : "5000000",
"anthropic-ratelimit-input-tokens-remaining" : "5000000",
"anthropic-ratelimit-input-tokens-reset" : "2026-02-06T01:36:11Z",
"anthropic-ratelimit-input-tokens-reset" : "2026-02-18T04:40:36Z",
"anthropic-ratelimit-output-tokens-limit" : "1000000",
"anthropic-ratelimit-output-tokens-remaining" : "1000000",
"anthropic-ratelimit-output-tokens-reset" : "2026-02-06T01:36:11Z",
"anthropic-ratelimit-output-tokens-reset" : "2026-02-18T04:40:36Z",
"anthropic-ratelimit-tokens-limit" : "6000000",
"anthropic-ratelimit-tokens-remaining" : "6000000",
"anthropic-ratelimit-tokens-reset" : "2026-02-06T01:36:11Z",
"request-id" : "req_011CXqxWWdE6Kb91K1dfu7Zt",
"anthropic-ratelimit-tokens-reset" : "2026-02-18T04:40:36Z",
"request-id" : "req_011CYEvHDdWoj2q5sbR7CzQ9",
"strict-transport-security" : "max-age=31536000; includeSubDomains; preload",
"anthropic-organization-id" : "27796668-7351-40ac-acc4-024aee8995a5",
"Server" : "cloudflare",
"x-envoy-upstream-service-time" : "388",
"cf-cache-status" : "DYNAMIC",
"x-envoy-upstream-service-time" : "414",
"X-Robots-Tag" : "none",
"Content-Security-Policy" : "default-src 'none'; frame-ancestors 'none'",
"CF-RAY" : "9c96ea441d1eba4e-SEA"
"cf-cache-status" : "DYNAMIC",
"CF-RAY" : "9cfad8ee5a4ab9f4-SEA"
}
},
"uuid" : "71f8c84f-b988-4c20-ab41-8e0f5e20325c",
"uuid" : "14d7ddaf-c4dc-4e41-ae83-1b9b0e7e222c",
"persistent" : true,
"insertionIndex" : 2
}
Loading