Skip to content

Commit

Permalink
fix the vision model when encountering non-png images (#491)
Browse files Browse the repository at this point in the history
Signed-off-by: InternetBugs <[email protected]>
  • Loading branch information
InternetBugs authored Mar 20, 2024
1 parent 6efd7f7 commit 2345249
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dotenv": "^16.0.3",
"express": "^4.18.3",
"express-rate-limit": "^6.7.0",
"file-type": "^19.0.0",
"gpt-token": "^0.0.5",
"https-proxy-agent": "^5.0.1",
"isomorphic-fetch": "^3.0.0",
Expand Down
57 changes: 57 additions & 0 deletions service/pnpm-lock.yaml

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

8 changes: 6 additions & 2 deletions service/src/utils/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'node:fs/promises'
import * as fileType from 'file-type'

fs.mkdir('uploads').then(() => {
globalThis.console.log('Directory uploads created')
Expand All @@ -12,7 +13,10 @@ fs.mkdir('uploads').then(() => {

export async function convertImageUrl(uploadFileKey: string): Promise<string> {
const imageData = await fs.readFile(`uploads/${uploadFileKey}`)
//判断文件格式
const imageType = await fileType.fileTypeFromBuffer(imageData)
const mimeType = imageType.mime;
// 将图片数据转换为 Base64 编码的字符串
const base64Image = imageData.toString('base64')
return `data:image/png;base64,${base64Image}`
}
return `data:${mimeType};base64,${base64Image}`
}
2 changes: 1 addition & 1 deletion src/views/chat/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ onUnmounted(() => {
:headers="uploadHeaders"
:show-file-list="false"
response-type="json"
accept="image/*"
accept="image/png, image/jpeg, image/webp, image/gif"
@finish="handleFinish"
>
<span class="text-xl text-[#4f555e] dark:text-white">
Expand Down

0 comments on commit 2345249

Please sign in to comment.