From c7b3595f3739827824da68d40d3369e47db5c46e Mon Sep 17 00:00:00 2001 From: Ankush Gola <9536492+agola11@users.noreply.github.com> Date: Thu, 2 May 2024 09:50:18 -0700 Subject: [PATCH] feat(JS): allow fetching examples by metadata (#646) --- js/src/client.ts | 6 ++++++ js/src/tests/client.int.test.ts | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/js/src/client.ts b/js/src/client.ts index 6017e3c5b..4736740f9 100644 --- a/js/src/client.ts +++ b/js/src/client.ts @@ -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 { let datasetId_; if (datasetId !== undefined && datasetName !== undefined) { @@ -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( "/examples", params diff --git a/js/src/tests/client.int.test.ts b/js/src/tests/client.int.test.ts index 0d99fde23..eaf49b975 100644 --- a/js/src/tests/client.int.test.ts +++ b/js/src/tests/client.int.test.ts @@ -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