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

Fix media file prompting for vertex gemini and google gemini #635

Merged
merged 2 commits into from
Oct 1, 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
32 changes: 20 additions & 12 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,27 @@ export const VertexGoogleChatCompleteConfig: ProviderConfig = {
});

return;
} else if (
url.startsWith('gs://') ||
url.startsWith('https://') ||
url.startsWith('http://')
) {
parts.push({
fileData: {
mimeType: getMimeType(url),
fileUri: url,
},
});
} else {
// NOTE: This block is kept to maintain backward compatibility
// Earlier we were assuming that all images will be base64 with image/jpeg mimeType
parts.push({
inlineData: {
mimeType: 'image/jpeg',
data: c.image_url?.url,
},
});
}

// This part is problematic because URLs are not supported in the current implementation.
// Two problems exist:
// 1. Only Google Cloud Storage URLs are supported.
// 2. MimeType is not supported in OpenAI API, but it is required in Google Vertex AI API.
// Google will return an error here if any other URL is provided.
parts.push({
fileData: {
mimeType: getMimeType(url),
fileUri: url,
},
});
}
});
} else if (typeof message.content === 'string') {
Expand Down
5 changes: 3 additions & 2 deletions src/providers/google-vertex-ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ const fileExtensionMimeTypeMap = {
flv: 'video/flv',
};

export const getMimeType = (url: string) => {
export const getMimeType = (url: string): string | undefined => {
const urlParts = url.split('.');
const extension = urlParts[
urlParts.length - 1
] as keyof typeof fileExtensionMimeTypeMap;
return fileExtensionMimeTypeMap[extension] || 'image/jpeg';
const mimeType = fileExtensionMimeTypeMap[extension];
return mimeType;
};

export const GoogleErrorResponseTransform: (
Expand Down
6 changes: 5 additions & 1 deletion src/providers/google/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ export const GoogleChatCompleteConfig: ProviderConfig = {
data: base64Image,
},
});
} else if (url.startsWith('gs://')) {
} else if (
url.startsWith('gs://') ||
url.startsWith('https://') ||
url.startsWith('http://')
) {
parts.push({
fileData: {
mimeType: getMimeType(url),
Expand Down
Loading