From 1753f9a4cc223c2f02aca9461108fe59b914f44e Mon Sep 17 00:00:00 2001 From: zhangboyu <313267717@qq.com> Date: Tue, 30 Jan 2024 10:47:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=B8=80=E4=B8=8B?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E4=BD=BF=E7=94=A8=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- server.js | 75 ++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 8e8531f..1bc6214 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ #### 使用说明 1. 注册微信公众号 -2. 创建 OpenAI (ChatGPT) 账号 +2. 使用第三方OpenAI接口账号,https://openkey.cloud/register?aff=UZlS 3. 创建 AirCode.io 账号 4. 新建云应用 5. 配置环境变量 diff --git a/server.js b/server.js index ec2dbd7..9b6a635 100644 --- a/server.js +++ b/server.js @@ -7,6 +7,7 @@ const TOKEN = process.env.TOKEN || ""; // 微信服务器配置 Token const OPENAI_KEY = process.env.OPENAI_KEY || ""; // OpenAI 的 Key const OPENAI_MODEL = process.env.MODEL || "gpt-3.5-turbo"; // 使用的 AI 模型 +// const OPENAI_MODEL = process.env.MODEL || "gpt-4"; // 使用的 AI 模型 const OPENAI_MAX_TOKEN = process.env.MAX_TOKEN || 1024; // 最大 token 的值 const LIMIT_HISTORY_MESSAGES = 50; // 限制历史会话最大条数 @@ -20,18 +21,58 @@ const UNSUPPORTED_MESSAGE_TYPES = { music: "暂不支持音乐消息", news: "暂不支持图文消息", }; - +const messageList = [ + { + label: "查看上一次问题的回复", + name: "1", + value: "lastQuestion", + }, + { + label: "清除上下文", + return: "✅ 记忆已清除", + name: "清除", + value: "clear", + }, + { + label: "获取更多帮助", + name: "帮助", + value: "help", + }, + { + label: "获取我的主页", + name: "主页", + return: `主页地址: +http://website.diehunter1024.work/ +如果打不开,请复制到浏览器打开。`, + value: "blog", + }, + { + label: "获取自营流量卡网站", + name: "流量卡", + return: `自营流量卡网站: +http://website.diehunter1024.work/card.html +如果打不开,请复制到浏览器打开。`, + value: "card", + }, + { + label: "获取书签", + name: "书签", + return: `个人书签: +http://website.diehunter1024.work/bookmarks/index.html +如果打不开,请复制到浏览器打开。`, + value: "bookmarks", + }, +]; const WAIT_MESSAGE = `处理中 ... \n\n请稍等几秒后发送【1】查看回复`; const NO_MESSAGE = `暂无内容,请稍后回复【1】再试`; const CLEAR_MESSAGE = `✅ 记忆已清除`; -const HELP_MESSAGE = `感谢你的关注,公众号已接入ChatGPT,快来与我对话吧! +const HELP_MESSAGE = `感谢你的关注,未来的500强股东! +公众号已接入ChatGPT,快来与我对话吧! 指令使用指南 -Usage: - 1 查看上一次问题的回复 - /clear 清除上下文 - /help 获取更多帮助 - `; - +Usage:${messageList.map((it) => { + const { name, label } = it; + return `\n回复:“${name}” ${label}`; +})}\n发送其他消息即可与GPT聊天`; const Message = db.table("messages"); const Event = db.table("events"); @@ -51,12 +92,14 @@ function toXML(payload, content) { `; } -async function processCommandText({ sessionId, question }) { +async function processCommandText({ sessionId, question }, msg) { // 清理历史会话 - if (question === "/clear") { + if (question === messageList[1].name) { const now = new Date(); await Message.where({ sessionId }).set({ deletedAt: now }).save(); return CLEAR_MESSAGE; + } else if (msg.name === question) { + return msg.return ?? HELP_MESSAGE; } else { return HELP_MESSAGE; } @@ -112,7 +155,9 @@ async function getOpenAIReply(prompt) { const config = { method: "post", maxBodyLength: Infinity, - url: "https://api.openai.com/v1/chat/completions", + // url: "https://api.openai.com/v1/chat/completions", + url: "https://openkey.cloud/v1/chat/completions", + headers: { Authorization: `Bearer ${OPENAI_KEY}`, "Content-Type": "application/json", @@ -146,7 +191,7 @@ async function replyText(message) { const { question, sessionId, msgid } = message; // 检查是否是重试操作 - if (question === "1") { + if (question === messageList[0].name) { const now = new Date(); // const earliestAt = new Date(now.getTime() - CONVERSATION_MAX_AGE) const lastMessage = await Message.where({ @@ -162,10 +207,10 @@ async function replyText(message) { return NO_MESSAGE; } - // 发送指令 - if (question.startsWith("/")) { - return await processCommandText(message); + const msg = messageList.find((it) => it.name === question); + if (msg) { + return await processCommandText(message, msg); } // OpenAI 回复内容 -- Gitee