-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add: Resize the image size to meet OpenAI requirements #458
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tested this and it works well! Just a few suggestions I commented.
I was also wondering if we should set a lower maximum size of image upload for better performance, or would that be a negligible difference? |
@Amnish04 Yeah, that's a good way to try. if we set lower value of Maybe we add options in follow up PRs for the user to select uploaded image with different compression rate or wanted size. User can adjust it accordingly by the result. |
Deploying with Cloudflare Pages
|
Sounds good, could you please file a follow up for that. |
@humphd During refactoring the code, I realized can also solve the dynamic import issue 😄 Let me know if other updates needed. Thanks a lot for the feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much better! One small fix.
src/lib/utils.ts
Outdated
|
||
// Make sure image's size is within 20MB and 2048x2048 resolution | ||
// https://platform.openai.com/docs/guides/vision/is-there-a-limit-to-the-size-of-the-image-i-can-upload | ||
export const imageCompressionOptions = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this inline below into your compressImageToBase64
function, since we don't use this externally now.
Explanation
This updates make sure users' input image's size is within OpenAI GPT-4V limit.
The OpenAI requirements for images can be found at Is there a limit to the size of the image I can upload?
And Calculating costs
I found this tool browser-image-compression focus on the image compression in web browser is great to handle it, supports both file size and px constraints.
I set the values in imageCompressionOptions as:
The
useWebWorker
is optional, means the tool will use multi-thread web worker for the compression, fallback to run in main-thread (default:true
). Although it is defaulttrue
, I added it to align with the document's UsageHow to test
Use some large size image in the prompt, I got a 30MB image from here
Before
After
Fixes #395
Fixes #465