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 10 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
223 changes: 106 additions & 117 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
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 @@ -423,7 +424,7 @@
// If there is an item on the queue we were unable to pop,
// just return it as a single batch.
if (popped.length === 0 && this.items.length > 0) {
const item = this.items.shift()!;

Check warning on line 427 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Forbidden non-null assertion
popped.push(item);
poppedSizeBytes += item.size;
this.sizeBytes -= item.size;
Expand Down Expand Up @@ -846,7 +847,7 @@
if (this._serverInfo === undefined) {
try {
this._serverInfo = await this._getServerInfo();
} catch (e) {

Check warning on line 850 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

'e' is defined but never used. Allowed unused args must match /^_/u
console.warn(
`[WARNING]: LangSmith failed to fetch info on supported operations. Falling back to batch operations and default limits.`
);
Expand Down Expand Up @@ -1139,12 +1140,20 @@
);
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 @@ -1564,7 +1573,7 @@
treeFilter?: string;
isRoot?: boolean;
dataSourceType?: string;
}): Promise<any> {

Check warning on line 1576 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
let projectIds_ = projectIds || [];
if (projectNames) {
projectIds_ = [
Expand Down Expand Up @@ -1852,7 +1861,7 @@
`Failed to list shared examples: ${response.status} ${response.statusText}`
);
}
return result.map((example: any) => ({

Check warning on line 1864 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
...example,
_hostUrl: this.getHostUrl(),
}));
Expand Down Expand Up @@ -1989,7 +1998,7 @@
}
// projectId querying
return true;
} catch (e) {

Check warning on line 2001 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

'e' is defined but never used. Allowed unused args must match /^_/u
return false;
}
}
Expand Down Expand Up @@ -3364,7 +3373,7 @@
async _logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 3376 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<[results: EvaluationResult[], feedbacks: Feedback[]]> {
const evalResults: Array<EvaluationResult> =
this._selectEvalResults(evaluatorResponse);
Expand Down Expand Up @@ -3403,7 +3412,7 @@
public async logEvaluationFeedback(
evaluatorResponse: EvaluationResult | EvaluationResults,
run?: Run,
sourceInfo?: { [key: string]: any }

Check warning on line 3415 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
): Promise<EvaluationResult[]> {
const [results] = await this._logEvaluationFeedback(
evaluatorResponse,
Expand Down Expand Up @@ -3853,7 +3862,7 @@

public async createCommit(
promptIdentifier: string,
object: any,

Check warning on line 3865 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
options?: {
parentCommitHash?: string;
}
Expand Down Expand Up @@ -3909,70 +3918,7 @@
"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, [mimeType, data]] of Object.entries(
example.attachments
)) {
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 @@ -4001,53 +3947,7 @@
"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, [mimeType, data]] of Object.entries(
example.attachments
)) {
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 All @@ -4071,7 +3971,7 @@
isPublic?: boolean;
isArchived?: boolean;
}
): Promise<Record<string, any>> {

Check warning on line 3974 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type
if (!(await this.promptExists(promptIdentifier))) {
throw new Error("Prompt does not exist, you must create it first.");
}
Expand All @@ -4082,7 +3982,7 @@
throw await this._ownerConflictError("update a prompt", owner);
}

const payload: Record<string, any> = {};

Check warning on line 3985 in js/src/client.ts

View workflow job for this annotation

GitHub Actions / Check linting

Unexpected any. Specify a different type

if (options?.description !== undefined)
payload.description = options.description;
Expand Down Expand Up @@ -4370,3 +4270,92 @@

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;
}
2 changes: 1 addition & 1 deletion js/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export interface AttachmentInfo {
presigned_url: string;
}

export type AttachmentData = Uint8Array | ArrayBuffer;
export type AttachmentData = ArrayBuffer | Uint8Array | Blob;
export type Attachments = Record<string, [string, AttachmentData]>;

/**
Expand Down
13 changes: 10 additions & 3 deletions js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ test("annotationqueue crud", async () => {
}
});

test("upload examples multipart", async () => {
test.only("upload examples multipart", async () => {
isahers1 marked this conversation as resolved.
Show resolved Hide resolved
const client = new Client();
const datasetName = `__test_upload_examples_multipart${uuidv4().slice(0, 4)}`;

Expand All @@ -1278,15 +1278,20 @@ test("upload examples multipart", async () => {
inputs: { text: "hello world" },
// check that passing no outputs works fine
attachments: {
test_file: ["image/png", fs.readFileSync(pathname)],
test_file: ["image/png", new Uint8Array(fs.readFileSync(pathname))],
},
};

const example2: ExampleUploadWithAttachments = {
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 @@ -1300,12 +1305,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
Loading