Skip to content

Commit

Permalink
Add back support for image parts in messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
shaper committed Nov 22, 2024
1 parent 2978847 commit 21e1cc0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'dotenv/config';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';
import fs from 'node:fs';

async function main() {
const togetherai = createOpenAICompatible({
apiKeyEnvVarName: 'TOGETHER_AI_API_KEY',
baseURL: 'https://api.together.xyz/v1',
name: 'togetherai',
});
// const model = togetherai.chatModel('google/gemma-2b-it');
const model = togetherai.chatModel('mistralai/Mixtral-8x7B-Instruct-v0.1');
const result = await generateText({
model,
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'Describe the image in detail.' },
{ type: 'image', image: fs.readFileSync('./data/comic-cat.png') },
],
},
],
});

console.log(result.text);
}

main().catch(console.error);
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,62 @@ describe('user messages', () => {

expect(result).toEqual([{ role: 'user', content: 'Hello' }]);
});

it('should convert messages with image parts', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'user',
content: [
{ type: 'text', text: 'Hello' },
{
type: 'image',
image: new Uint8Array([0, 1, 2, 3]),
mimeType: 'image/png',
},
],
},
]);

expect(result).toEqual([
{
role: 'user',
content: [
{ type: 'text', text: 'Hello' },
{
type: 'image_url',
image_url: { url: 'data:image/png;base64,AAECAw==' },
},
],
},
]);
});

it('should handle URL-based images', async () => {
const result = convertToOpenAICompatibleChatMessages([
{
role: 'user',
content: [
{
type: 'image',
image: new URL('https://example.com/image.jpg'),
mimeType: 'image/jpeg',
},
],
},
]);

expect(result).toEqual([
{
role: 'user',
content: [
{
type: 'image_url',
image_url: { url: 'https://example.com/image.jpg' },
},
],
},
]);
});
});

describe('tool calls', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
LanguageModelV1Prompt,
UnsupportedFunctionalityError,
} from '@ai-sdk/provider';
import { convertUint8ArrayToBase64 } from '@ai-sdk/provider-utils';
import { OpenAICompatibleChatPrompt } from './openai-compatible-api-types';

export function convertToOpenAICompatibleChatMessages(
Expand Down Expand Up @@ -30,10 +31,17 @@ export function convertToOpenAICompatibleChatMessages(
return { type: 'text', text: part.text };
}
case 'image': {
// TODO(shaper): Add back the below.
throw new UnsupportedFunctionalityError({
functionality: 'Image content parts in user messages',
});
return {
type: 'image_url',
image_url: {
url:
part.image instanceof URL
? part.image.toString()
: `data:${
part.mimeType ?? 'image/jpeg'
};base64,${convertUint8ArrayToBase64(part.image)}`,
},
};
}
case 'file': {
throw new UnsupportedFunctionalityError({
Expand Down

0 comments on commit 21e1cc0

Please sign in to comment.