mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-17 22:07:10 +00:00
fix: add a image caption tool
This commit is contained in:
parent
6d5a9ed97d
commit
34bba041e5
3 changed files with 80 additions and 1 deletions
|
|
@ -128,6 +128,7 @@ const defaultConfig = {
|
|||
amapKey: '',
|
||||
azSerpKey: '',
|
||||
serpSource: 'ikechan8370',
|
||||
extraUrl: '',
|
||||
version: 'v2.7.0'
|
||||
}
|
||||
const _path = process.cwd()
|
||||
|
|
|
|||
51
utils/tools/ImageCaptionTool.js
Normal file
51
utils/tools/ImageCaptionTool.js
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
import { AbstractTool } from './AbstractTool.js'
|
||||
import fetch, { File, FormData } from 'node-fetch'
|
||||
import { Config } from '../config.js'
|
||||
import fs from 'fs'
|
||||
import crypto from 'crypto'
|
||||
export class ImageCaptionTool extends AbstractTool {
|
||||
name = 'imageCaption'
|
||||
|
||||
parameters = {
|
||||
properties: {
|
||||
imgUrl: {
|
||||
type: 'string',
|
||||
description: 'the url of the image.'
|
||||
},
|
||||
qq: {
|
||||
type: 'string',
|
||||
description: 'if the picture is an avatar of a user, just give his qq number'
|
||||
}
|
||||
},
|
||||
required: []
|
||||
}
|
||||
|
||||
description = 'useful when you want to know what is inside a picture, such as user\'s avatar'
|
||||
|
||||
func = async function (opts) {
|
||||
let { imgUrl, qq } = opts
|
||||
if (qq) {
|
||||
imgUrl = `https://q1.qlogo.cn/g?b=qq&s=160&nk=${qq}`
|
||||
}
|
||||
if (!imgUrl) {
|
||||
return 'you must give at least one parameter of imgUrl and qq'
|
||||
}
|
||||
const imageResponse = await fetch(imgUrl)
|
||||
const blob = await imageResponse.blob()
|
||||
const arrayBuffer = await blob.arrayBuffer()
|
||||
const buffer = Buffer.from(arrayBuffer)
|
||||
// await fs.writeFileSync(`data/chatgpt/${crypto.randomUUID()}`, buffer)
|
||||
let formData = new FormData()
|
||||
formData.append('file', new File([buffer], 'file.png', { type: 'image/png' }))
|
||||
let captionRes = await fetch(`${Config.extraUrl}/image-captioning`, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
if (captionRes.status === 200) {
|
||||
let result = await captionRes.text()
|
||||
return `the content of this picture is: ${result}`
|
||||
} else {
|
||||
return 'error happened'
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue