代码拉取完成,页面将自动刷新
import dotenv from "dotenv-safe";
dotenv.config();
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import { ChatGPTAPI } from "chatgpt";
import { oraPromise } from "ora";
import config from "./config.js";
const app = express().use(cors()).use(bodyParser.json());
const gptApi = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY
});
const Config = configure(config);
class Conversation {
conversationID = null;
parentMessageID = null;
constructor() {}
async sendMessage(msg) {
const res = await gptApi.sendMessage(
msg,
this.conversationID && this.parentMessageID
? {
conversationId: this.conversationID,
parentMessageId: this.parentMessageID,
}
: {}
);
if (res.conversationId) {
this.conversationID = res.conversationId;
}
if (res.parentMessageId) {
this.parentMessageID = res.parentMessageId;
}
if (res.response) {
return res.response;
}
return res;
}
}
const conversation = new Conversation();
app.post("/", async (req, res) => {
try {
const rawReply = await oraPromise(
conversation.sendMessage(req.body.message),
{
text: req.body.message,
}
);
const reply = await Config.parse(rawReply.text);
console.log(`----------\n${reply}\n----------`);
res.json({ reply });
} catch (error) {
console.log(error);
res.status(500);
}
});
async function start() {
await oraPromise(Config.train(), {
text: `Training ChatGPT (${Config.rules.length} plugin rules)`,
});
await oraPromise(
new Promise((resolve) => app.listen(3000, () => resolve())),
{
text: `You may now use the extension`,
}
);
}
function configure({ plugins, ...opts }) {
let rules = [];
let parsers = [];
// Collect rules and parsers from all plugins
for (const plugin of plugins) {
if (plugin.rules) {
rules = rules.concat(plugin.rules);
}
if (plugin.parse) {
parsers.push(plugin.parse);
}
}
// Send ChatGPT a training message that includes all plugin rules
const train = () => {
if (!rules.length) return;
const message = `
Please follow these rules when replying to me:
${rules.map((rule) => `\n- ${rule}`)}
`;
return conversation.sendMessage(message);
};
// Run the ChatGPT response through all plugin parsers
const parse = async (reply) => {
for (const parser of parsers) {
reply = await parser(reply);
}
return reply;
};
return { train, parse, rules, ...opts };
}
start();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。