File size: 5,501 Bytes
23bbc26 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | const axios = require('axios');
// config
const apiKey = "";
const maxTokens = 500;
const numberGenerateImage = 4;
const maxStorageMessage = 4;
if (!global.temp.openAIUsing)
global.temp.openAIUsing = {};
if (!global.temp.openAIHistory)
global.temp.openAIHistory = {};
const { openAIUsing, openAIHistory } = global.temp;
module.exports = {
config: {
name: "gpt",
version: "1.3",
author: "NTKhang",
countDown: 5,
role: 0,
shortDescription: {
vi: "GPT chat",
en: "GPT chat"
},
longDescription: {
vi: "GPT chat",
en: "GPT chat"
},
category: "box chat",
guide: {
vi: " {pn} <draw> <nội dung> - tạo hình ảnh từ nội dung"
+ "\n {pn} <clear> - xóa lịch sử chat với gpt"
+ "\n {pn} <nội dung> - chat với gpt",
en: " {pn} <draw> <content> - create image from content"
+ "\n {pn} <clear> - clear chat history with gpt"
+ "\n {pn} <content> - chat with gpt"
}
},
langs: {
vi: {
apiKeyEmpty: "Vui lòng cung cấp api key cho openai tại file scripts/cmds/gpt.js",
invalidContentDraw: "Vui lòng nhập nội dung bạn muốn vẽ",
yourAreUsing: "Bạn đang sử dụng gpt chat, vui lòng chờ quay lại sau khi yêu cầu trước kết thúc",
processingRequest: "Đang xử lý yêu cầu của bạn, quá trình này có thể mất vài phút, vui lòng chờ",
invalidContent: "Vui lòng nhập nội dung bạn muốn chat",
error: "Đã có lỗi xảy ra\n%1",
clearHistory: "Đã xóa lịch sử chat của bạn với gpt"
},
en: {
apiKeyEmpty: "Please provide api key for openai at file scripts/cmds/gpt.js",
invalidContentDraw: "Please enter the content you want to draw",
yourAreUsing: "You are using gpt chat, please wait until the previous request ends",
processingRequest: "Processing your request, this process may take a few minutes, please wait",
invalidContent: "Please enter the content you want to chat",
error: "An error has occurred\n%1",
clearHistory: "Your chat history with gpt has been deleted"
}
},
onStart: async function ({ message, event, args, getLang, prefix, commandName }) {
if (!apiKey)
return message.reply(getLang('apiKeyEmpty', prefix));
switch (args[0]) {
case 'img':
case 'image':
case 'draw': {
if (!args[1])
return message.reply(getLang('invalidContentDraw'));
if (openAIUsing[event.senderID])
return message.reply(getLang("yourAreUsing"));
openAIUsing[event.senderID] = true;
let sending;
try {
sending = message.reply(getLang('processingRequest'));
const responseImage = await axios({
url: "https://api.openai.com/v1/images/generations",
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
data: {
prompt: args.slice(1).join(' '),
n: numberGenerateImage,
size: '1024x1024'
}
});
const imageUrls = responseImage.data.data;
const images = await Promise.all(imageUrls.map(async (item) => {
const image = await axios.get(item.url, {
responseType: 'stream'
});
image.data.path = `${Date.now()}.png`;
return image.data;
}));
return message.reply({
attachment: images
});
}
catch (err) {
const errorMessage = err.response?.data.error.message || err.message;
return message.reply(getLang('error', errorMessage || ''));
}
finally {
delete openAIUsing[event.senderID];
message.unsend((await sending).messageID);
}
}
case 'clear': {
openAIHistory[event.senderID] = [];
return message.reply(getLang('clearHistory'));
}
default: {
if (!args[0])
return message.reply(getLang('invalidContent'));
handleGpt(event, message, args, getLang, commandName);
}
}
},
onReply: async function ({ Reply, message, event, args, getLang, commandName }) {
const { author } = Reply;
if (author != event.senderID)
return;
handleGpt(event, message, args, getLang, commandName);
}
};
async function askGpt(event) {
const response = await axios({
url: "https://api.openai.com/v1/chat/completions",
method: "POST",
headers: {
"Authorization": `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
data: {
model: "gpt-3.5-turbo",
messages: openAIHistory[event.senderID],
max_tokens: maxTokens,
temperature: 0.7
}
});
return response;
}
async function handleGpt(event, message, args, getLang, commandName) {
try {
openAIUsing[event.senderID] = true;
if (
!openAIHistory[event.senderID] ||
!Array.isArray(openAIHistory[event.senderID])
)
openAIHistory[event.senderID] = [];
if (openAIHistory[event.senderID].length >= maxStorageMessage)
openAIHistory[event.senderID].shift();
openAIHistory[event.senderID].push({
role: 'user',
content: args.join(' ')
});
const response = await askGpt(event);
const text = response.data.choices[0].message.content;
openAIHistory[event.senderID].push({
role: 'assistant',
content: text
});
return message.reply(text, (err, info) => {
global.GoatBot.onReply.set(info.messageID, {
commandName,
author: event.senderID,
messageID: info.messageID
});
});
}
catch (err) {
const errorMessage = err.response?.data.error.message || err.message || "";
return message.reply(getLang('error', errorMessage));
}
finally {
delete openAIUsing[event.senderID];
}
} |