-
Notifications
You must be signed in to change notification settings - Fork 4
support Datasets and additional output date in Evals #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package dev.braintrust.eval; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
|
|
||
| /** | ||
| * Datasets define the cases for evals. This interface provides a means of iterating through all | ||
| * cases of a particular dataset. | ||
| * | ||
| * <p>The most common implementations are in-memory datasets, and datasets fetched from the | ||
| * Braintrust API. | ||
| */ | ||
| public interface Dataset<INPUT, OUTPUT> { | ||
| Cursor<DatasetCase<INPUT, OUTPUT>> openCursor(); | ||
|
|
||
| String id(); | ||
|
|
||
| String version(); | ||
|
|
||
| @NotThreadSafe | ||
| interface Cursor<CASE> extends AutoCloseable { | ||
| /** | ||
| * Fetch the next case. Returns empty if there are no more cases to fetch. | ||
| * | ||
| * <p>Implementations may make external requests to fetch data. | ||
| * | ||
| * <p>If this method is invoked after {@link #close()} an IllegalStateException will be | ||
| * thrown | ||
| */ | ||
| Optional<CASE> next(); | ||
|
|
||
| /** close all cursor resources */ | ||
| void close(); | ||
| } | ||
|
|
||
| /** Create an in-memory Dataset containing the provided cases. */ | ||
| @SafeVarargs | ||
| static <INPUT, OUTPUT> Dataset<INPUT, OUTPUT> of(DatasetCase<INPUT, OUTPUT>... cases) { | ||
| return new DatasetInMemoryImpl<>(List.of(cases)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package dev.braintrust.eval; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| /** A single row in a dataset. */ | ||
| public record DatasetCase<INPUT, OUTPUT>( | ||
| INPUT input, | ||
| OUTPUT expected, | ||
| @Nonnull List<String> tags, | ||
| @Nonnull Map<String, Object> metadata) { | ||
| public DatasetCase { | ||
| if (!metadata.isEmpty()) { | ||
| throw new RuntimeException("TODO: metadata support not yet implemented"); | ||
| } | ||
| if (!tags.isEmpty()) { | ||
| throw new RuntimeException("TODO: tags support not yet implemented"); | ||
| } | ||
| } | ||
|
|
||
| public static <INPUT, OUTPUT> DatasetCase<INPUT, OUTPUT> of(INPUT input, OUTPUT expected) { | ||
| return new DatasetCase<>(input, expected, List.of(), Map.of()); | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/main/java/dev/braintrust/eval/DatasetInMemoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package dev.braintrust.eval; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| /** A dataset held entirely in memory */ | ||
| class DatasetInMemoryImpl<INPUT, OUTPUT> implements Dataset<INPUT, OUTPUT> { | ||
| private final List<DatasetCase<INPUT, OUTPUT>> cases; | ||
| private final String id; | ||
|
|
||
| DatasetInMemoryImpl(List<DatasetCase<INPUT, OUTPUT>> cases) { | ||
| this.cases = List.copyOf(cases); | ||
| id = "in-memory-dataset<" + this.cases.hashCode() + ">"; | ||
| } | ||
|
|
||
| @Override | ||
| public String id() { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| public String version() { | ||
| return "0"; | ||
| } | ||
|
|
||
| @Override | ||
| public Cursor<DatasetCase<INPUT, OUTPUT>> openCursor() { | ||
| return new Cursor<>() { | ||
| int nextIndex = 0; | ||
| boolean closed = false; | ||
|
|
||
| @Override | ||
| public Optional<DatasetCase<INPUT, OUTPUT>> next() { | ||
| if (closed) { | ||
| throw new IllegalStateException("this method may not be invoked after close"); | ||
| } else if (nextIndex < cases.size()) { | ||
| return Optional.of(cases.get(nextIndex++)); | ||
| } else { | ||
| return Optional.empty(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| closed = true; | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE:
EvalCaseis still supported for backwards compatibility (I've marked the class as deprecated and encouraged use ofDatasetCaseinstead).