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

feat(attachments): create context-aware helper for attachments #44

Merged
merged 6 commits into from
Jul 25, 2024

Conversation

Dam-Buty
Copy link
Contributor

@Dam-Buty Dam-Buty commented Jul 17, 2024

Non-Breaking

This creates a new context-aware helper that abstracts the logic around creating attachments, as well as providing the following enhancements :

  • Make threadId optional (it is already optional on the API)
  • Fix typing to enforce either content or path at compile time
  • Accept a variety of formats as input content (ReadableStream, ReadStream, Buffer, File, Blob & ArrayBuffer)
  • As a treat i squashed the chainlit-logo to hell so it is <3KB ⚡

Old syntax

// Upload the file
const { objectKey } = await literalClient.api.uploadFile({
  threadId: thread.id,
  content: fileStream,
  mime
});

// Create the Attachment
const attachment = new Attachment({ name: 'Attachment', objectKey, mime });

// Attach it to the step
await thread
  .step({
    name: 'Assistant',
    type: 'assistant_message',
    output: { content: 'Here is the generated image!' },
    attachments: [attachment]
  })
  .send();

New syntax

await literalClient
  .step({
    name: 'Assistant',
    type: 'assistant_message',
    output: { content: 'Here is the generated image!' }
  })
  .wrap(async () => {
    // This will upload the file, create the attachment
    // and automatically attach it to the wrapped step
    await client.api.createAttachment({
      content: fileStream,
      mime,
      name: 'Attachment'
    });
  });

Copy link

linear bot commented Jul 17, 2024

ENG-1590 TS SDK : inconsistencies on uploadFile

The uploadFile method on the TS SDK has the following signature :

{
    content?: Maybe<any>;
    path?: Maybe<string>;
    id?: Maybe<string>;
    threadId: string;
    mime?: Maybe<string>;
}
  • threadId is mandatory but it is optional on the API, both in the upload/file endpoint & in the Attachment type
  • content on the other hand should be mandatory
  • content is typed any but it will definitely break if you don't provide it with a Readable

For the content part, it is a bit impractical when the file comes from a web form. In this case it is presented to you as a File , and it took me a hot minute to figure out that i had to transform it like that :

const nodeStream = Readable.fromWeb(
  formAudio.stream() as ReadableStream<any>
);
// nodeStream can now be used in uploadFile

In a similar situation, the openai SDK provides a toFile helper which takes as input a wide variety of File, Blob, ArrayBuffer, ReadStream and more, and converts it to something you can upload.

As a general rule we want to change the API for attachments, instead of first uploading the file then creating the Attachment instance we should have client.api.createAttachment which uploads the file & returns an Attachment instance. uploadFile stays as is in case some use cases need it.

  • Make threadId optional
  • Fix typing to have either content or path
  • Accept ReadableStream, ReadStream, Buffer, File, Blob & ArrayBuffer as input content
  • abstract upload + Attachment instantiation with a helper method createAttachment

@desaxce desaxce self-requested a review July 22, 2024 10:06
Copy link
Collaborator

@desaxce desaxce left a comment

Choose a reason for hiding this comment

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

Looks great to me!

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's still a png, what was the trick to reduce size? # of RGB channels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes ! i used one of those online squashers, and reduced the color depth (to <10 colors iirc)

@@ -596,19 +620,25 @@ export class API {
* @returns An object containing the `objectKey` of the uploaded file and the signed `url`, or `null` values if the upload fails.
* @throws {Error} Throws an error if neither `content` nor `path` is provided, or if the server response is invalid.
*/

async uploadFile(params: UploadFileParamsWithContent): Promise<{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't know we could declare prototypes for doc/autocompletion purposes.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Just realized you mentioned "Fix typing to enforce either content or path at compile time" -> very clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes overloading function types is a great way to handle this kind of either/or parameters. It's a bit verbose but the end result is satisfying from the POV of the dev using the function.

src/api.ts Show resolved Hide resolved
@@ -0,0 +1,96 @@
import 'dotenv/config';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you so much for splitting tests in their own file!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you're welcome, i'm unable to focus on a 700 line test file anyway 😅

const arrayBuffer = buffer.buffer;
const blob = new Blob([buffer]);
// We wrap the blob in a blob and simulate the structure of a File
const file = new Blob([blob], { type: 'image/jpeg' });
Copy link
Collaborator

Choose a reason for hiding this comment

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

png maybe?

src/api.ts Outdated
try {
threadFromStore = this.client.getCurrentThread();
} catch (error) {
// Ignore error thrown if getCurrentThread is called outside of a context
Copy link
Collaborator

Choose a reason for hiding this comment

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

When I debug, I expect caught errors to appear somewhere in the logs, it helps me navigate through the code and quickly identify the location of a bug. I think it's only me in the team though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, there is genuinely nothing to log. You may or may not be inside of a context, both of which are valid states. But you're right, that's a code smell so i added _currentThread and _currentStep methods which do not throw outside of context. It is much cleaner.

I couldn't make them private (as they are consumed from outside of the LiteralClient class) but they are undocumented.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, it's like using try/catch as an if condition.

@Dam-Buty Dam-Buty requested a review from desaxce July 25, 2024 08:52
Copy link
Collaborator

@desaxce desaxce left a comment

Choose a reason for hiding this comment

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

I don't think the openai e2e test should be impacted by this change, there are no attachments in those tests.

@Dam-Buty
Copy link
Contributor Author

I don't think the openai e2e test should be impacted by this change, there are no attachments in those tests.

yes, confirmed, they do work in local

@Dam-Buty Dam-Buty merged commit aaaa54c into main Jul 25, 2024
1 of 2 checks passed
@Dam-Buty Dam-Buty deleted the damien/eng-1590-ts-sdk-inconsistencies-on-uploadfile branch August 1, 2024 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants