Skip to content

Commit

Permalink
remove attachment prefix (#1324)
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 authored Dec 11, 2024
1 parent 29c26c5 commit 1d7de25
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 68 deletions.
4 changes: 2 additions & 2 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
32 changes: 13 additions & 19 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1410,24 +1410,24 @@ 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
).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)));
Expand All @@ -1443,14 +1443,11 @@ 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([
"attachment.test_file2",
]);
attachmentData = updatedExample.attachments?.["attachment.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?.["attachment.test_file2"].presigned_url
updatedExample.attachments?.["test_file2"].presigned_url
).then((res) => res.arrayBuffer())) as ArrayBuffer
)
: undefined;
Expand All @@ -1471,14 +1468,11 @@ test("update examples multipart", async () => {
foo: "bar",
dataset_split: ["foo", "bar"],
});
expect(Object.keys(updatedExample.attachments ?? {})).toEqual([
"attachment.test_file",
]);
attachmentData = updatedExample.attachments?.["attachment.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?.["attachment.test_file"].presigned_url
updatedExample.attachments?.["test_file"].presigned_url
).then((res) => res.arrayBuffer())) as ArrayBuffer
)
: undefined;
Expand Down
85 changes: 40 additions & 45 deletions js/src/tests/evaluate_attachments.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ 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
).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)) {
Expand All @@ -57,16 +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"
].presigned_url
const attachmentData: Uint8Array | undefined = attachments?.["image"]
.presigned_url
? new Uint8Array(
(await fetch(attachments?.["attachment.image"].presigned_url).then(
(res) => res.arrayBuffer()
(await fetch(attachments?.["image"].presigned_url).then((res) =>
res.arrayBuffer()
)) as ArrayBuffer
)
: undefined;
Expand Down Expand Up @@ -135,16 +134,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"
].presigned_url
const attachmentData: Uint8Array | undefined = attachments?.["image"]
.presigned_url
? new Uint8Array(
(await fetch(attachments?.["attachment.image"].presigned_url).then(
(res) => res.arrayBuffer()
(await fetch(attachments?.["image"].presigned_url).then((res) =>
res.arrayBuffer()
)) as ArrayBuffer
)
: undefined;
Expand Down Expand Up @@ -212,19 +210,19 @@ 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
).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)) {
Expand All @@ -235,16 +233,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"
].presigned_url
const attachmentData: Uint8Array | undefined = attachments?.["image"]
.presigned_url
? new Uint8Array(
(await fetch(attachments?.["attachment.image"].presigned_url).then(
(res) => res.arrayBuffer()
(await fetch(attachments?.["image"].presigned_url).then((res) =>
res.arrayBuffer()
)) as ArrayBuffer
)
: undefined;
Expand All @@ -259,16 +256,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"
].presigned_url
const attachmentData: Uint8Array | undefined = attachments?.["image"]
.presigned_url
? new Uint8Array(
(await fetch(attachments?.["attachment.image"].presigned_url).then(
(res) => res.arrayBuffer()
(await fetch(attachments?.["image"].presigned_url).then((res) =>
res.arrayBuffer()
)) as ArrayBuffer
)
: undefined;
Expand Down Expand Up @@ -333,19 +329,19 @@ 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
).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)) {
Expand All @@ -359,16 +355,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"
].presigned_url
const attachmentData: Uint8Array | undefined = attachments?.["image"]
.presigned_url
? new Uint8Array(
(await fetch(attachments?.["attachment.image"].presigned_url).then(
(res) => res.arrayBuffer()
(await fetch(attachments?.["image"].presigned_url).then((res) =>
res.arrayBuffer()
)) as ArrayBuffer
)
: undefined;
Expand Down
4 changes: 2 additions & 2 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down

0 comments on commit 1d7de25

Please sign in to comment.