mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-16 21:37:11 +00:00
feat: support dalle api
This commit is contained in:
parent
47976f2325
commit
8c9c098e25
5 changed files with 81 additions and 1 deletions
55
apps/draw.js
Normal file
55
apps/draw.js
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
import plugin from '../../../lib/plugins/plugin.js'
|
||||||
|
import { segment } from 'oicq'
|
||||||
|
import { createImage } from '../utils/dalle.js'
|
||||||
|
import { makeForwardMsg} from "../utils/common.js";
|
||||||
|
import _ from 'lodash'
|
||||||
|
|
||||||
|
export class dalle extends plugin {
|
||||||
|
constructor (e) {
|
||||||
|
super({
|
||||||
|
name: 'ChatGPT-Plugin Dalle 绘图',
|
||||||
|
dsc: 'ChatGPT-Plugin基于OpenAI Dalle的绘图插件',
|
||||||
|
event: 'message',
|
||||||
|
priority: 500,
|
||||||
|
rule: [
|
||||||
|
{
|
||||||
|
reg: '#(chatgpt|ChatGPT|dalle|Dalle)(绘图|画图)',
|
||||||
|
fnc: 'draw'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async draw (e) {
|
||||||
|
let splits = _.split(e.msg, '图', 2)
|
||||||
|
if (splits.length < 2) {
|
||||||
|
this.reply('请带上绘图要求')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
let rules = _.split(splits[1], '/')
|
||||||
|
let [prompt = '', num = '1', size = '512x512'] = rules.slice(0, 3)
|
||||||
|
if (['256x256', '512x512', '1024x1024'].indexOf(size) === -1) {
|
||||||
|
this.reply('大小不符合要求,必须是256x256/512x512/1024x1024中的一个')
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
let priceMap = {
|
||||||
|
'1024x1024': 0.02,
|
||||||
|
'512x512': 0.018,
|
||||||
|
'256x256': 0.016
|
||||||
|
}
|
||||||
|
num = parseInt(num, 10)
|
||||||
|
await this.reply(`正在为您绘制大小为${size}的${num}张图片,预计消耗${priceMap[size] * num}美元余额,请稍候……`)
|
||||||
|
try {
|
||||||
|
let images = (await createImage(prompt, num, size)).map(image => segment.image(`base64://${image}`))
|
||||||
|
if (images.length > 1) {
|
||||||
|
this.reply(await makeForwardMsg(e, images, prompt))
|
||||||
|
} else {
|
||||||
|
this.reply(images[0], true)
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
this.reply(`绘图失败: err`, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -24,6 +24,11 @@ let helpData = [
|
||||||
icon: 'text',
|
icon: 'text',
|
||||||
title: '#chatgpt文本模式',
|
title: '#chatgpt文本模式',
|
||||||
desc: '机器人以文本形式回答,默认选项'
|
desc: '机器人以文本形式回答,默认选项'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
icon: 'text',
|
||||||
|
title: '#chatgpt画图+prompt(/张数/图片大小)',
|
||||||
|
desc: '调用OpenAI Dalle API进行绘图,需要有API key并消耗余额。图片大小只能是256x256/512x512/1024x1024中的一个.默认为1张、512x512'
|
||||||
}
|
}
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
"delay": "^5.0.0",
|
"delay": "^5.0.0",
|
||||||
"keyv-file": "^0.2.0",
|
"keyv-file": "^0.2.0",
|
||||||
"node-fetch": "^3.3.0",
|
"node-fetch": "^3.3.0",
|
||||||
|
"openai": "^3.1.0",
|
||||||
"puppeteer-extra": "^3.3.4",
|
"puppeteer-extra": "^3.3.4",
|
||||||
"puppeteer-extra-plugin-recaptcha": "^3.6.6",
|
"puppeteer-extra-plugin-recaptcha": "^3.6.6",
|
||||||
"puppeteer-extra-plugin-stealth": "^2.11.1",
|
"puppeteer-extra-plugin-stealth": "^2.11.1",
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ export function escapeHtml (str) {
|
||||||
return str.replace(/[&<>"'/]/g, (match) => htmlEntities[match])
|
return str.replace(/[&<>"'/]/g, (match) => htmlEntities[match])
|
||||||
}
|
}
|
||||||
|
|
||||||
export function randomString(length = 5) {
|
export function randomString (length = 5) {
|
||||||
let str = ''
|
let str = ''
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
str += lodash.random(36).toString(36)
|
str += lodash.random(36).toString(36)
|
||||||
|
|
|
||||||
19
utils/dalle.js
Normal file
19
utils/dalle.js
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { Configuration, OpenAIApi } from 'openai'
|
||||||
|
import { Config } from './config.js'
|
||||||
|
|
||||||
|
export async function createImage (prompt, n = 1, size = '512x512') {
|
||||||
|
const configuration = new Configuration({
|
||||||
|
apiKey: Config.apiKey
|
||||||
|
})
|
||||||
|
const openai = new OpenAIApi(configuration)
|
||||||
|
if (Config.debug) {
|
||||||
|
logger.info({ prompt, n, size })
|
||||||
|
}
|
||||||
|
const response = await openai.createImage({
|
||||||
|
prompt,
|
||||||
|
n,
|
||||||
|
size,
|
||||||
|
response_format: 'b64_json'
|
||||||
|
})
|
||||||
|
return response.data.data?.map(pic => pic.b64_json)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue