Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS updates to attachment type #1325

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 106 additions & 131 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { raiseForStatus } from "./utils/error.js";
import { _getFetchImplementation } from "./singletons/fetch.js";

import { stringify as stringifyForTracing } from "./utils/fast-safe-stringify/index.js";
import { v4 as uuid4 } from "uuid";

export interface ClientConfig {
apiUrl?: string;
Expand Down Expand Up @@ -1148,12 +1149,20 @@ export class Client implements LangSmithTracingClientInterface {
);
continue;
}
accumulatedParts.push({
name: `attachment.${payload.id}.${name}`,
payload: new Blob([content], {
type: `${contentType}; length=${content.byteLength}`,
}),
});
// eslint-disable-next-line no-instanceof/no-instanceof
if (content instanceof Blob) {
Copy link
Collaborator

@jacoblee93 jacoblee93 Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait sorry why do we need this vs. just accepting UInt8Array again?

I think the content typing doesn't include Blob here?

I know I said earlier we shouldn't do this - but I wonder if there's a way we can accept a Node Buffer here. Will talk to you in a sec.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also - can this piggyback off the new method you created below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because attachment could be a Blob now (from you: https://langchain.slack.com/archives/D076HL2E77B/p1733946087301879)

accumulatedParts.push({
name: `attachment.${payload.id}.${name}`,
payload: content,
});
} else {
accumulatedParts.push({
name: `attachment.${payload.id}.${name}`,
payload: new Blob([content], {
type: `${contentType}; length=${content.byteLength}`,
}),
});
}
}
}
}
Expand Down Expand Up @@ -3918,77 +3927,7 @@ export class Client implements LangSmithTracingClientInterface {
"Your LangSmith version does not allow using the multipart examples endpoint, please update to the latest version."
);
}
const formData = new FormData();

for (const example of updates) {
const exampleId = example.id;

// Prepare the main example body
const exampleBody = {
...(example.metadata && { metadata: example.metadata }),
...(example.split && { split: example.split }),
};

// Add main example data
const stringifiedExample = stringifyForTracing(exampleBody);
const exampleBlob = new Blob([stringifiedExample], {
type: "application/json",
});
formData.append(exampleId, exampleBlob);

// Add inputs
if (example.inputs) {
const stringifiedInputs = stringifyForTracing(example.inputs);
const inputsBlob = new Blob([stringifiedInputs], {
type: "application/json",
});
formData.append(`${exampleId}.inputs`, inputsBlob);
}

// Add outputs if present
if (example.outputs) {
const stringifiedOutputs = stringifyForTracing(example.outputs);
const outputsBlob = new Blob([stringifiedOutputs], {
type: "application/json",
});
formData.append(`${exampleId}.outputs`, outputsBlob);
}

// Add attachments if present
if (example.attachments) {
for (const [name, attachment] of Object.entries(example.attachments)) {
let mimeType: string;
let data: AttachmentData;

if (Array.isArray(attachment)) {
[mimeType, data] = attachment;
} else {
mimeType = attachment.mimeType;
data = attachment.data;
}
const attachmentBlob = new Blob([data], {
type: `${mimeType}; length=${data.byteLength}`,
});
formData.append(`${exampleId}.attachment.${name}`, attachmentBlob);
}
}

if (example.attachments_operations) {
const stringifiedAttachmentsOperations = stringifyForTracing(
example.attachments_operations
);
const attachmentsOperationsBlob = new Blob(
[stringifiedAttachmentsOperations],
{
type: "application/json",
}
);
formData.append(
`${exampleId}.attachments_operations`,
attachmentsOperationsBlob
);
}
}
const formData = _prepareMultiPartData(updates);

const response = await this.caller.call(
_getFetchImplementation(),
Expand Down Expand Up @@ -4017,60 +3956,7 @@ export class Client implements LangSmithTracingClientInterface {
"Your LangSmith version does not allow using the multipart examples endpoint, please update to the latest version."
);
}
const formData = new FormData();

for (const example of uploads) {
const exampleId = (example.id ?? uuid.v4()).toString();

// Prepare the main example body
const exampleBody = {
created_at: example.created_at,
...(example.metadata && { metadata: example.metadata }),
...(example.split && { split: example.split }),
};

// Add main example data
const stringifiedExample = stringifyForTracing(exampleBody);
const exampleBlob = new Blob([stringifiedExample], {
type: "application/json",
});
formData.append(exampleId, exampleBlob);

// Add inputs
const stringifiedInputs = stringifyForTracing(example.inputs);
const inputsBlob = new Blob([stringifiedInputs], {
type: "application/json",
});
formData.append(`${exampleId}.inputs`, inputsBlob);

// Add outputs if present
if (example.outputs) {
const stringifiedOutputs = stringifyForTracing(example.outputs);
const outputsBlob = new Blob([stringifiedOutputs], {
type: "application/json",
});
formData.append(`${exampleId}.outputs`, outputsBlob);
}

// Add attachments if present
if (example.attachments) {
for (const [name, attachment] of Object.entries(example.attachments)) {
let mimeType: string;
let data: AttachmentData;

if (Array.isArray(attachment)) {
[mimeType, data] = attachment;
} else {
mimeType = attachment.mimeType;
data = attachment.data;
}
const attachmentBlob = new Blob([data], {
type: `${mimeType}; length=${data.byteLength}`,
});
formData.append(`${exampleId}.attachment.${name}`, attachmentBlob);
}
}
}
const formData = _prepareMultiPartData(uploads);

const response = await this.caller.call(
_getFetchImplementation(),
Expand Down Expand Up @@ -4393,3 +4279,92 @@ export interface LangSmithTracingClientInterface {

updateRun: (runId: string, run: RunUpdate) => Promise<void>;
}

function isExampleUpdateWithAttachments(
obj: ExampleUpdateWithAttachments | ExampleUploadWithAttachments
): obj is ExampleUpdateWithAttachments {
return (
(obj as ExampleUpdateWithAttachments).attachments_operations !== undefined
);
}

function _prepareMultiPartData(
examples: ExampleUpdateWithAttachments[] | ExampleUploadWithAttachments[]
): FormData {
const formData = new FormData();

for (const example of examples) {
const exampleId = example.id ?? uuid4();

// Prepare the main example body
const exampleBody = {
...(example.metadata && { metadata: example.metadata }),
...(example.split && { split: example.split }),
};

// Add main example data
const stringifiedExample = stringifyForTracing(exampleBody);
const exampleBlob = new Blob([stringifiedExample], {
type: "application/json",
});
formData.append(exampleId, exampleBlob);

// Add inputs
if (example.inputs) {
const stringifiedInputs = stringifyForTracing(example.inputs);
const inputsBlob = new Blob([stringifiedInputs], {
type: "application/json",
});
formData.append(`${exampleId}.inputs`, inputsBlob);
}

// Add outputs if present
if (example.outputs) {
const stringifiedOutputs = stringifyForTracing(example.outputs);
const outputsBlob = new Blob([stringifiedOutputs], {
type: "application/json",
});
formData.append(`${exampleId}.outputs`, outputsBlob);
}

// Add attachments if present
if (example.attachments) {
for (const [name, [mimeType, data]] of Object.entries(
example.attachments
)) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (data instanceof Blob) {
formData.append(`${exampleId}.attachment.${name}`, data);
} else {
formData.append(
`${exampleId}.attachment.${name}`,
new Blob([data], {
type: `${mimeType}; length=${data.byteLength}`,
})
);
}
}
}

if (
isExampleUpdateWithAttachments(example) &&
example.attachments_operations
) {
const stringifiedAttachmentsOperations = stringifyForTracing(
example.attachments_operations
);
const attachmentsOperationsBlob = new Blob(
[stringifiedAttachmentsOperations],
{
type: "application/json",
}
);
formData.append(
`${exampleId}.attachments_operations`,
attachmentsOperationsBlob
);
}
}

return formData;
}
3 changes: 2 additions & 1 deletion js/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export interface AttachmentInfo {
presigned_url: string;
}

export type AttachmentData = Uint8Array | ArrayBuffer;

export type AttachmentData = ArrayBuffer | Uint8Array | Blob;

export type AttachmentDescription = {
mimeType: string;
Expand Down
9 changes: 8 additions & 1 deletion js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,12 @@ test("upload examples multipart", async () => {
inputs: { text: "foo bar" },
outputs: { response: "baz" },
attachments: {
my_file: ["image/png", fs.readFileSync(pathname)],
my_file: [
"image/png",
new Blob([fs.readFileSync(pathname)], {
type: `image/png; length=${fs.readFileSync(pathname).byteLength}`,
}),
],
},
};

Expand All @@ -1303,12 +1308,14 @@ test("upload examples multipart", async () => {

const createdExample1 = await client.readExample(exampleId);
expect(createdExample1.inputs["text"]).toBe("hello world");
expect(createdExample1.attachments?.["test_file"]).toBeDefined();

const createdExample2 = await client.readExample(
createdExamples.example_ids.find((id) => id !== exampleId)!
);
expect(createdExample2.inputs["text"]).toBe("foo bar");
expect(createdExample2.outputs?.["response"]).toBe("baz");
expect(createdExample2.attachments?.["my_file"]).toBeDefined();

// Test examples were sent to correct dataset
const allExamplesInDataset = [];
Expand Down
9 changes: 9 additions & 0 deletions js/src/tests/traceable.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ test.concurrent(
const testAttachment2 = new Uint8Array([5, 6, 7, 8]);
const testAttachment3 = new ArrayBuffer(4);
new Uint8Array(testAttachment3).set([13, 14, 15, 16]);
const testAttachment4Content = new Blob(["Hello world!"]);
const testAttachment4 = new Blob([testAttachment4Content], {
type: `text/plain; length=${testAttachment4Content.size}`,
});

const traceableWithAttachmentsAndInputs = traceable(
(
Expand All @@ -696,6 +700,7 @@ test.concurrent(
{
test1bin: ["application/octet-stream", testAttachment1],
test2bin: ["application/octet-stream", testAttachment2],
test3bin: ["application/octet-stream", testAttachment4],
inputbin: ["application/octet-stream", attachment],
input2bin: [
"application/octet-stream",
Expand Down Expand Up @@ -749,6 +754,10 @@ test.concurrent(
"application/octet-stream",
testAttachment2,
]);
expect(runCreate?.attachments?.["test3bin"]).toEqual([
"application/octet-stream",
testAttachment4,
]);
expect(runCreate?.attachments?.["inputbin"]).toEqual([
"application/octet-stream",
new Uint8Array([9, 10, 11, 12]),
Expand Down
Loading