From e8189aeeba05a045ae9d297e0f41b0be0330c988 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Wed, 11 Dec 2024 10:25:51 -0800 Subject: [PATCH 1/2] remove attachment prefix --- js/src/client.ts | 4 +- js/src/tests/client.int.test.ts | 22 ++++----- js/src/tests/evaluate_attachments.int.test.ts | 48 +++++++++---------- python/langsmith/client.py | 4 +- 4 files changed, 39 insertions(+), 39 deletions(-) diff --git a/js/src/client.ts b/js/src/client.ts index 23f33d515..b3f0a1e03 100644 --- a/js/src/client.ts +++ b/js/src/client.ts @@ -2735,7 +2735,7 @@ export class Client implements LangSmithTracingClientInterface { // add attachments back to the example example.attachments = Object.entries(attachment_urls).reduce( (acc, [key, value]) => { - acc[key] = { + acc[key.slice("attachment.".length)] = { presigned_url: value.presigned_url, }; return acc; @@ -2832,7 +2832,7 @@ export class Client implements LangSmithTracingClientInterface { if (attachment_urls) { example.attachments = Object.entries(attachment_urls).reduce( (acc, [key, value]) => { - acc[key] = { + acc[key.slice("attachment.".length)] = { presigned_url: value.presigned_url, }; return acc; diff --git a/js/src/tests/client.int.test.ts b/js/src/tests/client.int.test.ts index 18582a01b..7c4f58b9b 100644 --- a/js/src/tests/client.int.test.ts +++ b/js/src/tests/client.int.test.ts @@ -1410,23 +1410,23 @@ test("update examples multipart", async () => { let updatedExample = await client.readExample(exampleId); expect(updatedExample.inputs.text).toEqual("hello world2"); expect(Object.keys(updatedExample.attachments ?? {}).sort()).toEqual( - ["attachment.bar", "attachment.test_file"].sort() + ["bar", "test_file"].sort() ); expect(updatedExample.metadata).toEqual({ bar: "foo" }); let attachmentData: Uint8Array | undefined = updatedExample.attachments?.[ - "attachment.test_file" + "test_file" ].presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file"].presigned_url + updatedExample.attachments?.["test_file"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; expect(attachmentData).toEqual(new Uint8Array(fs.readFileSync(pathname))); - attachmentData = updatedExample.attachments?.["attachment.bar"].presigned_url + attachmentData = updatedExample.attachments?.["bar"].presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.bar"].presigned_url + updatedExample.attachments?.["bar"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -1444,13 +1444,13 @@ test("update examples multipart", async () => { updatedExample = await client.readExample(exampleId); expect(updatedExample.metadata).toEqual({ foo: "bar" }); expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "attachment.test_file2", + "test_file2", ]); - attachmentData = updatedExample.attachments?.["attachment.test_file2"] + attachmentData = updatedExample.attachments?.["test_file2"] .presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file2"].presigned_url + updatedExample.attachments?.["test_file2"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -1472,13 +1472,13 @@ test("update examples multipart", async () => { dataset_split: ["foo", "bar"], }); expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "attachment.test_file", + "test_file", ]); - attachmentData = updatedExample.attachments?.["attachment.test_file"] + attachmentData = updatedExample.attachments?.["test_file"] .presigned_url ? new Uint8Array( (await fetch( - updatedExample.attachments?.["attachment.test_file"].presigned_url + updatedExample.attachments?.["test_file"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; diff --git a/js/src/tests/evaluate_attachments.int.test.ts b/js/src/tests/evaluate_attachments.int.test.ts index 9a78a3f9a..e1c346d91 100644 --- a/js/src/tests/evaluate_attachments.int.test.ts +++ b/js/src/tests/evaluate_attachments.int.test.ts @@ -34,18 +34,18 @@ test("evaluate can handle examples with attachments", async () => { config?: TargetConfigT ) => { // Verify we receive the attachment data - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( (await fetch( - config?.attachments?.["attachment.image"].presigned_url + config?.attachments?.["image"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -57,15 +57,15 @@ test("evaluate can handle examples with attachments", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( + (await fetch(attachments?.["image"].presigned_url).then( (res) => res.arrayBuffer() )) as ArrayBuffer ) @@ -135,15 +135,15 @@ test("evaluate with attachments not in target function", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( + (await fetch(attachments?.["image"].presigned_url).then( (res) => res.arrayBuffer() )) as ArrayBuffer ) @@ -212,18 +212,18 @@ test("multiple evaluators with attachments", async () => { config?: TargetConfigT ) => { // Verify we receive the attachment data - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( (await fetch( - config?.attachments?.["attachment.image"].presigned_url + config?.attachments?.["image"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -235,15 +235,15 @@ test("multiple evaluators with attachments", async () => { const customEvaluatorOne = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( + (await fetch(attachments?.["image"].presigned_url).then( (res) => res.arrayBuffer() )) as ArrayBuffer ) @@ -259,15 +259,15 @@ test("multiple evaluators with attachments", async () => { const customEvaluatorTwo = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( + (await fetch(attachments?.["image"].presigned_url).then( (res) => res.arrayBuffer() )) as ArrayBuffer ) @@ -333,18 +333,18 @@ test("evaluate with attachments runnable target function", async () => { await client.uploadExamplesMultipart(dataset.id, [example]); const myFunction = async (_input: any, config?: any) => { - if (!config?.attachments?.["attachment.image"]) { + if (!config?.attachments?.["image"]) { throw new Error("Image attachment not found"); } const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = config?.attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( (await fetch( - config?.attachments?.["attachment.image"].presigned_url + config?.attachments?.["image"].presigned_url ).then((res) => res.arrayBuffer())) as ArrayBuffer ) : undefined; @@ -359,15 +359,15 @@ test("evaluate with attachments runnable target function", async () => { const customEvaluator = async ({ attachments }: { attachments?: any }) => { expect(attachments).toBeDefined(); - expect(attachments?.["attachment.image"]).toBeDefined(); + expect(attachments?.["image"]).toBeDefined(); const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); const attachmentData: Uint8Array | undefined = attachments?.[ - "attachment.image" + "image" ].presigned_url ? new Uint8Array( - (await fetch(attachments?.["attachment.image"].presigned_url).then( + (await fetch(attachments?.["image"].presigned_url).then( (res) => res.arrayBuffer() )) as ArrayBuffer ) diff --git a/python/langsmith/client.py b/python/langsmith/client.py index a92a89659..f683a36f1 100644 --- a/python/langsmith/client.py +++ b/python/langsmith/client.py @@ -3900,7 +3900,7 @@ def read_example( response = requests.get(value["presigned_url"], stream=True) response.raise_for_status() reader = io.BytesIO(response.content) - attachments[key.split(".")[1]] = { + attachments[key.removeprefix("attachment.")] = { "presigned_url": value["presigned_url"], "reader": reader, } @@ -3986,7 +3986,7 @@ def list_examples( response = requests.get(value["presigned_url"], stream=True) response.raise_for_status() reader = io.BytesIO(response.content) - attachments[key.split(".")[1]] = { + attachments[key.removeprefix("attachment.")] = { "presigned_url": value["presigned_url"], "reader": reader, } From 7b2e73d14bdad5e93c99dec891ded50149a7ed52 Mon Sep 17 00:00:00 2001 From: isaac hershenson Date: Wed, 11 Dec 2024 10:35:21 -0800 Subject: [PATCH 2/2] fmt --- js/src/tests/client.int.test.ts | 20 +++--- js/src/tests/evaluate_attachments.int.test.ts | 63 +++++++++---------- 2 files changed, 36 insertions(+), 47 deletions(-) diff --git a/js/src/tests/client.int.test.ts b/js/src/tests/client.int.test.ts index 7c4f58b9b..de8710e1f 100644 --- a/js/src/tests/client.int.test.ts +++ b/js/src/tests/client.int.test.ts @@ -1425,9 +1425,9 @@ test("update examples multipart", async () => { expect(attachmentData).toEqual(new Uint8Array(fs.readFileSync(pathname))); attachmentData = updatedExample.attachments?.["bar"].presigned_url ? new Uint8Array( - (await fetch( - updatedExample.attachments?.["bar"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(updatedExample.attachments?.["bar"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; expect(attachmentData).toEqual(new Uint8Array(fs.readFileSync(pathname))); @@ -1443,11 +1443,8 @@ test("update examples multipart", async () => { await client.updateExamplesMultipart(dataset.id, [exampleUpdate4]); updatedExample = await client.readExample(exampleId); expect(updatedExample.metadata).toEqual({ foo: "bar" }); - expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "test_file2", - ]); - attachmentData = updatedExample.attachments?.["test_file2"] - .presigned_url + expect(Object.keys(updatedExample.attachments ?? {})).toEqual(["test_file2"]); + attachmentData = updatedExample.attachments?.["test_file2"].presigned_url ? new Uint8Array( (await fetch( updatedExample.attachments?.["test_file2"].presigned_url @@ -1471,11 +1468,8 @@ test("update examples multipart", async () => { foo: "bar", dataset_split: ["foo", "bar"], }); - expect(Object.keys(updatedExample.attachments ?? {})).toEqual([ - "test_file", - ]); - attachmentData = updatedExample.attachments?.["test_file"] - .presigned_url + expect(Object.keys(updatedExample.attachments ?? {})).toEqual(["test_file"]); + attachmentData = updatedExample.attachments?.["test_file"].presigned_url ? new Uint8Array( (await fetch( updatedExample.attachments?.["test_file"].presigned_url diff --git a/js/src/tests/evaluate_attachments.int.test.ts b/js/src/tests/evaluate_attachments.int.test.ts index e1c346d91..0a64a54f9 100644 --- a/js/src/tests/evaluate_attachments.int.test.ts +++ b/js/src/tests/evaluate_attachments.int.test.ts @@ -44,9 +44,9 @@ test("evaluate can handle examples with attachments", async () => { "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -61,12 +61,11 @@ test("evaluate can handle examples with attachments", async () => { const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -139,12 +138,11 @@ test("evaluate with attachments not in target function", async () => { const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -222,9 +220,9 @@ test("multiple evaluators with attachments", async () => { "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -239,12 +237,11 @@ test("multiple evaluators with attachments", async () => { const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -263,12 +260,11 @@ test("multiple evaluators with attachments", async () => { const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined; @@ -343,9 +339,9 @@ test("evaluate with attachments runnable target function", async () => { "image" ].presigned_url ? new Uint8Array( - (await fetch( - config?.attachments?.["image"].presigned_url - ).then((res) => res.arrayBuffer())) as ArrayBuffer + (await fetch(config?.attachments?.["image"].presigned_url).then( + (res) => res.arrayBuffer() + )) as ArrayBuffer ) : undefined; if (!arraysEqual(attachmentData ?? new Uint8Array(), expectedData)) { @@ -363,12 +359,11 @@ test("evaluate with attachments runnable target function", async () => { const expectedData = new Uint8Array( Buffer.from("fake image data for testing") ); - const attachmentData: Uint8Array | undefined = attachments?.[ - "image" - ].presigned_url + const attachmentData: Uint8Array | undefined = attachments?.["image"] + .presigned_url ? new Uint8Array( - (await fetch(attachments?.["image"].presigned_url).then( - (res) => res.arrayBuffer() + (await fetch(attachments?.["image"].presigned_url).then((res) => + res.arrayBuffer() )) as ArrayBuffer ) : undefined;