Skip to content

Commit

Permalink
Added support for returning files from custom columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Dec 30, 2024
1 parent 7734024 commit 6972314
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
42 changes: 42 additions & 0 deletions packages/evalite-tests/tests/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,45 @@ it("Should let users add files to data().input and data().expected", async () =>
],
});
});

it("Should let users add files to experimental_customColumns", async () => {
using fixture = loadFixture("files");

const captured = captureStdout();

await runVitest({
cwd: fixture.dir,
path: undefined,
testOutputWritable: captured.writable,
mode: "run-once-and-exit",
});

const dir = path.join(fixture.dir, FILES_LOCATION);

const files = await readdir(dir);

expect(files).toHaveLength(1);

const filePath = path.join(dir, files[0]!);

const file = await readFile(filePath);

expect(file).toBeTruthy();

const db = createDatabase(fixture.dbLocation);

const evals = await getEvalsAsRecord(db);

expect(evals.FilesWithColumns![0]).toMatchObject({
results: [
{
rendered_columns: [
{
label: "Column",
value: EvaliteFile.fromPath(filePath),
},
],
},
],
});
});
27 changes: 27 additions & 0 deletions packages/evalite-tests/tests/fixtures/files/files-4.eval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { evalite } from "evalite";
import { reportTrace } from "evalite/traces";
import { readFileSync } from "node:fs";
import path from "node:path";

evalite("FilesWithColumns", {
data: () => {
return [
{
input: "abc",
expected: "abcdef",
},
];
},
task: async (input) => {
return input + "def";
},
scorers: [],
experimental_customColumns: () => {
return [
{
label: "Column",
value: readFileSync(path.join(import.meta.dirname, "test.png")),
},
];
},
});
36 changes: 28 additions & 8 deletions packages/evalite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ export const evalite = <TInput, TExpected = TInput>(
experimental_customColumns: opts.experimental_customColumns,
});

const [outputWithFiles, tracesWithFiles] = await Promise.all([
createEvaliteFileIfNeeded({
rootDir,
input: output,
}),
handleFilesInTraces(rootDir, traces),
]);
const [outputWithFiles, tracesWithFiles, renderedColumns] =
await Promise.all([
createEvaliteFileIfNeeded({
rootDir,
input: output,
}),
handleFilesInTraces(rootDir, traces),
handleFilesInColumns(rootDir, experimental_columns),
]);

task.meta.evalite = {
result: {
Expand All @@ -164,7 +166,7 @@ export const evalite = <TInput, TExpected = TInput>(
scores,
traces: tracesWithFiles,
status: "success",
renderedColumns: experimental_columns,
renderedColumns,
},
duration: Math.round(performance.now() - start),
};
Expand Down Expand Up @@ -230,6 +232,24 @@ export const createScorer = <TInput, TExpected = TInput>(opts: {

export * from "./evalite-file.js";

const handleFilesInColumns = async (
rootDir: string,
columns: Evalite.RenderedColumn[]
) => {
return await Promise.all(
columns.map(async (column) => {
const file = await createEvaliteFileIfNeeded({
rootDir,
input: column.value,
});
return {
...column,
value: file,
};
})
);
};

const handleFilesInTraces = async (
rootDir: string,
traces: Evalite.Trace[]
Expand Down

0 comments on commit 6972314

Please sign in to comment.