This commit is contained in:
zyc404 2023-02-25 12:01:27 +08:00
commit 81b693a605
7 changed files with 106 additions and 16 deletions

62
apps/draw.js Normal file
View file

@ -0,0 +1,62 @@
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 ttl = await redis.ttl(`CHATGPT:DRAW:${e.sender.user_id}`)
if (ttl > 0 && !e.isMaster) {
this.reply(`冷却中,请${ttl}秒后再试`)
return false
}
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
}
await redis.set(`CHATGPT:DRAW:${e.sender.user_id}`, 'c', {EX: 30})
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)
await redis.del(`CHATGPT:DRAW:${e.sender.user_id}`)
}
}
}

View file

@ -24,6 +24,11 @@ let helpData = [
icon: 'text',
title: '#chatgpt文本模式',
desc: '机器人以文本形式回答,默认选项'
},
{
icon: 'text',
title: '#chatgpt画图+prompt(/张数/图片大小)',
desc: '调用OpenAI Dalle API进行绘图需要有API key并消耗余额。图片大小只能是256x256/512x512/1024x1024中的一个.默认为1张、512x512'
}
]

View file

@ -28,6 +28,8 @@ export default {
// cacheUrl: 'https://content.alcedogroup.com',
// 图片内容渲染服务器开启预制访问代码,当渲染服务器访问较慢时可以开启,但无法保证远程渲染是否成功
// cacheEntry: false,
// 绘图指令的CD时间主人不受限制
// drawCD: 30,
// ***********************************************************************************************************************************
// 以下为API方式(默认)的配置 *
// ***********************************************************************************************************************************

View file

@ -8,6 +8,7 @@
"delay": "^5.0.0",
"keyv-file": "^0.2.0",
"node-fetch": "^3.3.0",
"openai": "^3.1.0",
"puppeteer-extra": "^3.3.4",
"puppeteer-extra-plugin-recaptcha": "^3.6.6",
"puppeteer-extra-plugin-stealth": "^2.11.1",

View file

@ -20,7 +20,7 @@ export function escapeHtml (str) {
return str.replace(/[&<>"'/]/g, (match) => htmlEntities[match])
}
export function randomString(length = 5) {
export function randomString (length = 5) {
let str = ''
for (let i = 0; i < length; i++) {
str += lodash.random(36).toString(36)

View file

@ -13,6 +13,7 @@ const defaultConfig = {
cacheUrl: 'https://content.alcedogroup.com',
cacheEntry: false,
apiKey: '',
drawCD: 30,
model: '',
api: 'https://chatgpt.duti.tech/api/conversation',
apiBaseUrl: 'https://chatgpt.duti.tech/api',
@ -35,28 +36,28 @@ const defaultConfig = {
const _path = process.cwd()
let config = {}
if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/config/config.js`)) {
const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/config.js`);
config = (await import(`file://${fullPath}`)).default;
const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/config.js`)
config = (await import(`file://${fullPath}`)).default
} else if (fs.existsSync(`${_path}/plugins/chatgpt-plugin/config/index.js`)) {
// 兼容旧版本
const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/index.js`);
config = (await import(`file://${fullPath}`)).Config;
const fullPath = fs.realpathSync(`${_path}/plugins/chatgpt-plugin/config/index.js`)
config = (await import(`file://${fullPath}`)).Config
}
config = Object.assign({}, defaultConfig, config)
export const Config = new Proxy(config, {
set(target, property, value) {
target[property] = value;
const change = lodash.transform(target, function(result, value, key) {
if (!lodash.isEqual(value, defaultConfig[key])) {
result[key] = value;
}
});
set (target, property, value) {
target[property] = value
const change = lodash.transform(target, function (result, value, key) {
if (!lodash.isEqual(value, defaultConfig[key])) {
result[key] = value
}
})
try {
fs.writeFileSync(`${_path}/plugins/chatgpt-plugin/config/config.js`, `export default ${JSON.stringify(change, '', '\t')}`, { flag: 'w' })
} catch (err) {
console.error(err)
return false;
return false
}
return true;
},
});
return true
}
})

19
utils/dalle.js Normal file
View 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)
}