Skip to content

Commit

Permalink
feat(JS): allow fetching examples by metadata (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
agola11 authored May 2, 2024
1 parent 15a2a8d commit c7b3595
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2063,12 +2063,14 @@ export class Client {
exampleIds,
asOf,
inlineS3Urls,
metadata,
}: {
datasetId?: string;
datasetName?: string;
exampleIds?: string[];
asOf?: string | Date;
inlineS3Urls?: boolean;
metadata?: KVMap;
} = {}): AsyncIterable<Example> {
let datasetId_;
if (datasetId !== undefined && datasetName !== undefined) {
Expand Down Expand Up @@ -2097,6 +2099,10 @@ export class Client {
params.append("id", id_);
}
}
if (metadata !== undefined) {
const serializedMetadata = JSON.stringify(metadata);
params.append("metadata", serializedMetadata);
}
for await (const examples of this._getPaginated<Example>(
"/examples",
params
Expand Down
38 changes: 38 additions & 0 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,44 @@ test.concurrent(
expect(example3?.outputs?.output).toEqual("hi there 3");
expect(example3?.metadata?.key).toEqual("value 3");

await client.createExample(
{ input: "hello world" },
{ output: "hi there" },
{
datasetId: dataset.id,
metadata: { foo: "bar", baz: "qux" },
}
);
let examplesList3 = await toArray(
client.listExamples({ datasetId: dataset.id, metadata: { foo: "bar" } })
);
expect(examplesList3.length).toEqual(1);
expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

examplesList3 = await toArray(
client.listExamples({ datasetId: dataset.id, metadata: { foo: "qux" } })
);
expect(examplesList3.length).toEqual(0);

examplesList3 = await toArray(
client.listExamples({ datasetId: dataset.id, metadata: { baz: "qux" } })
);
expect(examplesList3.length).toEqual(1);

expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

examplesList3 = await toArray(
client.listExamples({
datasetId: dataset.id,
metadata: { foo: "bar", baz: "qux" },
})
);
expect(examplesList3.length).toEqual(1);
expect(examplesList3[0].metadata?.foo).toEqual("bar");
expect(examplesList3[0].metadata?.baz).toEqual("qux");

await client.deleteDataset({ datasetId: dataset.id });
},
180_000
Expand Down

0 comments on commit c7b3595

Please sign in to comment.