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 (core): support converting attachments to file parts #3146

Merged
merged 4 commits into from
Sep 27, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/silly-pigs-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

feat (core): support converting attachments to file parts
28 changes: 26 additions & 2 deletions packages/ai/core/prompt/attachments-to-parts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Attachment } from '@ai-sdk/ui-utils';
import { ImagePart, TextPart } from './content-part';
import { FilePart, ImagePart, TextPart } from './content-part';
import {
convertDataContentToUint8Array,
convertUint8ArrayToText,
} from './data-content';

type ContentPart = TextPart | ImagePart;
type ContentPart = TextPart | ImagePart | FilePart;

/**
* Converts a list of attachments to a list of content parts
Expand All @@ -29,6 +29,18 @@ export function attachmentsToParts(attachments: Attachment[]): ContentPart[] {
case 'https:': {
if (attachment.contentType?.startsWith('image/')) {
parts.push({ type: 'image', image: url });
} else {
if (!attachment.contentType) {
throw new Error(
'If the attachment is not an image, it must specify a content type',
);
}

parts.push({
type: 'file',
data: url,
mimeType: attachment.contentType,
});
}
break;
}
Expand Down Expand Up @@ -61,6 +73,18 @@ export function attachmentsToParts(attachments: Attachment[]): ContentPart[] {
convertDataContentToUint8Array(base64Content),
),
});
} else {
if (!attachment.contentType) {
throw new Error(
'If the attachment is not an image or text, it must specify a content type',
);
}

parts.push({
type: 'file',
data: base64Content,
mimeType: attachment.contentType,
});
}

break;
Expand Down
76 changes: 76 additions & 0 deletions packages/ai/core/prompt/convert-to-core-messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,35 @@ describe('user message', () => {
]);
});

it('should handle user message with attachments (file)', () => {
const attachment: Attachment = {
contentType: 'application/pdf',
url: 'https://example.com/document.pdf',
};

const result = convertToCoreMessages([
{
role: 'user',
content: 'Check this document',
experimental_attachments: [attachment],
},
]);

expect(result).toEqual([
{
role: 'user',
content: [
{ type: 'text', text: 'Check this document' },
{
type: 'file',
data: new URL('https://example.com/document.pdf'),
mimeType: 'application/pdf',
},
],
},
]);
});

it('should handle user message with attachment URLs', () => {
const attachment: Attachment = {
contentType: 'image/jpeg',
Expand All @@ -70,6 +99,35 @@ describe('user message', () => {
]);
});

it('should handle user message with attachment URLs (file)', () => {
const attachment: Attachment = {
contentType: 'application/pdf',
url: 'data:application/pdf;base64,dGVzdA==',
};

const result = convertToCoreMessages([
{
role: 'user',
content: 'Check this document',
experimental_attachments: [attachment],
},
]);

expect(result).toEqual([
{
role: 'user',
content: [
{ type: 'text', text: 'Check this document' },
{
type: 'file',
data: 'dGVzdA==',
mimeType: 'application/pdf',
},
],
},
]);
});

it('should throw an error for invalid attachment URLs', () => {
const attachment: Attachment = {
contentType: 'image/jpeg',
Expand All @@ -87,6 +145,24 @@ describe('user message', () => {
}).toThrow('Invalid URL: invalid-url');
});

it('should throw an error for file attachments without contentType', () => {
const attachment: Attachment = {
url: 'data:application/pdf;base64,dGVzdA==',
};

expect(() => {
convertToCoreMessages([
{
role: 'user',
content: 'Check this file',
experimental_attachments: [attachment],
},
]);
}).toThrow(
'If the attachment is not an image or text, it must specify a content type',
);
});
jeremyphilemon marked this conversation as resolved.
Show resolved Hide resolved

it('should throw an error for invalid data URL format', () => {
const attachment: Attachment = {
contentType: 'image/jpeg',
Expand Down
36 changes: 18 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading