最近chatgpt火热,chatgpt也能根据文本生成图片
1废话不多说,上代码
exports.getimg = async (types, id, propmt) => {
try {
propmt = propmt.slice(propmt.indexOf("图"), propmt.length - 1)
const resonse = await openai.createImage({
prompt: propmt,
n: 1,
size: "512x512",
});
image_url = response.data.data[0].url;
await SendMessage.SendMessage(types, `[CQ:image,file=${image_url}]`, id)
} catch (e) {
if (e) {
await SendMessage.SendMessage(types, `图片出错了${e}`, id)
}
}
}
其中n是返回的图片数目,size是尺寸,propmt是输入的文本
2 .代码
1.首先是要下载openai提供的包
npm insatll openai
2.引入包
const {Configuration, OpenAIApi} = require("openai");
3 完整代码
const {Configuration, OpenAIApi} = require("openai");
const configuration = new Configuration({
apiKey: token.apikey, //自己的apikey,要有openai账号
});
const openai = new OpenAIApi(configuration);
exports.getimg = async (types, id, propmt) => {
try {
propmt = propmt.slice(propmt.indexOf("图"), propmt.length - 1)
const resonse = await openai.createImage({
prompt: propmt,
n: 1,
size: "512x512",
});
image_url = response.data.data[0].url;
// console.log(image_url)
// fs.writeFileSync("chatgpt.txt", "Human:" + propmt + "\n" + image_url)
await SendMessage.SendMessage(types, `[CQ:image,file=${image_url}]`, id)
} catch (e) {
if (e) {
await SendMessage.SendMessage(types, `图片出错了${e}`, id)
}
}
}
4.在群聊中调用getimg方法
//只有管理才能调用此方法
if (data.user_id === config.manager[0]) {
if (data.message.includes("画图")) {
chatgpt.getimg("group", data.group_id, data.message)
}
}