-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
⚡️ perf: Optimize the image upload size for gpt-4-vision
#669
Conversation
@mushan0x0 is attempting to deploy a commit to the LobeHub Team on Vercel. A member of the Team first needs to authorize it. |
Thank you for raising your pull request and contributing to our Community |
此外,实现思路上可以考虑下,是只存压缩后的缩略图,还是 raw 也存,缩略图也存 |
In addition, you can consider the implementation idea, whether to store only compressed thumbnails, or to store raw and thumbnails as well. |
5a7f9d7
to
6865f21
Compare
之前自定义头像有个压缩方法 https://github.com/lobehub/lobe-chat/blob/main/src/utils/imageToBase64.ts |
6865f21
to
691c4ad
Compare
一开始是用的这个,但是里面有个图片居中逻辑,合并的话不如再抽个文件 |
691c4ad
to
d62bafa
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #669 +/- ##
==========================================
- Coverage 87.55% 87.36% -0.19%
==========================================
Files 171 172 +1
Lines 8045 8107 +62
Branches 719 724 +5
==========================================
+ Hits 7044 7083 +39
- Misses 1001 1024 +23 ☔ View full report in Codecov by Sentry. |
9fb1840
to
016acd9
Compare
016acd9
to
c67097b
Compare
gpt-4-vision
gpt-4-vision
c67097b
to
15f7ef3
Compare
15f7ef3
to
a3ec74c
Compare
gpt-4-vision
gpt-4-vision
async uploadFile(file: DB_File) { | ||
// 跳过图片上传测试 |
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.
图片压缩这里测试时只能跳过了,应该很难 mock
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.
可以使用 LobeChat 测试工程师 来帮你写单测:
现在 LobeChat 中很多单测都是它帮忙写的: https://shareg.pt/xHPM9NJ
原图就不用存了,占体积也麻烦,本身 webp 跟原图差别也不大 |
There is no need to save the original image, and it takes up a lot of space. Webp itself is not much different from the original image. |
还有一个思考点,目前压缩图片是通过分辨率判断,是不是通过 blob.size 最大值判断更好,理论上应该是在条件允许的可能下,保留更高清的图片的,只是一个想法,实现上可能会触发多次压缩循环,性能应该不太好 |
There is another point to think about. Currently, compressed images are judged by resolution. Is it better to judge by the maximum value of blob.size? |
我也想试着参与你们,在这个 pr 上我补充了一些测试用例。 大佬们看看可以不 😁 |
I also want to try to participate in you, and I have added some test cases in this PR. Guys, let’s see if you can 😁 |
🔧 chore: Add test cases
@canisminor1990 我感觉不是,应该按照分辨率来。原因是 GPT-4v 模型是按分辨率收钱的: refs: https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding 目前我的实现中是写死了 如果是一张分辨率是 12800x25600 的图片,如果限定尺寸,而不限定分辨率,可能是缩小到了 5120x 12800 ,满足了我们的尺寸要求。但发给 4v 之后容易造成更多的 token 浪费。毕竟有可能使用低分辨率就可以描述清楚了。 |
@canisminor1990 I don’t think so, it should be based on the resolution. The reason is that the GPT-4v model charges based on resolution: At present, the If it is a picture with a resolution of 12800x25600, if the size is limited but not the resolution, it may be reduced to 5120x 12800, which meets our size requirements. However, it is easy to cause more waste of tokens after issuing it to 4v. After all, it is possible to describe clearly using low resolution. |
这样啊 之前没仔细了解过 |
That's it. I haven't understood it carefully before. |
@mushan0x0 merge 下 main ? 有冲突 |
@mushan0x0 merge under main? Conflict |
好的,晚点处理一下 |
Okay, I'll deal with it later. |
413a219
to
be7a692
Compare
❤️ Great PR @mushan0x0 ❤️ The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world. |
🎉 This PR is included in version 0.114.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
💻 变更类型 | Change Type
🔀 变更说明 | Description of Change
设置图片最大宽度或者高度为 2k,再将图片格式转为 webp
Close #668
Close #646
📝 补充信息 | Additional Information
压缩之后
压缩之前