From 5399d57e3868a4f7b846b7338d1d6ba45eded27d Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Mon, 20 Feb 2023 13:23:13 +0800 Subject: [PATCH] feat: update beta --- LowerNode.md | 2 +- README.md | 11 +-- REDIS | 0 apps/management.js | 69 +++++++++++++++++++ config/config.example.js | 4 +- node_modules/.bin/chatgpt | 17 +++++ node_modules/.bin/uuid | 17 +++++ node_modules/chatgpt | 1 + node_modules/delay | 1 + node_modules/keyv-file | 1 + node_modules/node-fetch | 1 + node_modules/puppeteer-extra | 1 + node_modules/puppeteer-extra-plugin-recaptcha | 1 + node_modules/puppeteer-extra-plugin-stealth | 1 + node_modules/random | 1 + node_modules/undici | 1 + node_modules/uuid | 1 + package.json | 17 +++++ utils/common.js | 7 ++ utils/config.js | 4 +- utils/conversation.js | 2 +- utils/redis-key.js | 0 22 files changed, 147 insertions(+), 13 deletions(-) delete mode 100644 REDIS create mode 100755 node_modules/.bin/chatgpt create mode 100755 node_modules/.bin/uuid create mode 120000 node_modules/chatgpt create mode 120000 node_modules/delay create mode 120000 node_modules/keyv-file create mode 120000 node_modules/node-fetch create mode 120000 node_modules/puppeteer-extra create mode 120000 node_modules/puppeteer-extra-plugin-recaptcha create mode 120000 node_modules/puppeteer-extra-plugin-stealth create mode 120000 node_modules/random create mode 120000 node_modules/undici create mode 120000 node_modules/uuid create mode 100644 package.json delete mode 100644 utils/redis-key.js diff --git a/LowerNode.md b/LowerNode.md index 13ad1f4..1233068 100644 --- a/LowerNode.md +++ b/LowerNode.md @@ -1,4 +1,4 @@ -## 本页面内容部分过时,仅修改node-fetch部分可供参考。 +## 本页面已过时 ### Node.js >= 14 (并且 <18)时的安装方法 diff --git a/README.md b/README.md index c78b2e3..dfcf037 100644 --- a/README.md +++ b/README.md @@ -29,14 +29,8 @@ Node.js >= 18 / Node.js >= 14(with node-fetch) > * 浏览器模式通过在本地启动Chrome等浏览器模拟用户访问ChatGPT网站,使得获得和官方以及API2模式一模一样的回复质量,同时保证安全性。缺点是本方法对环境要求较高,需要提供桌面环境和一个可用的代理(能够访问ChatGPT的IP地址),且响应速度不如API,而且高峰期容易无法使用。一般作为API3的下位替代。 > * 必应(Bing)将调用微软新必应接口进行对话。需要在必应网页能够正常使用新必应且设置有效的Bing登录Cookie方可使用。 1. 进入 Yunzai根目录 -2. 检查 Node.js 版本 -``` -node -v -``` -若 Node.js >= 18,根据下方步骤安装即可。否则参考[这里](LowerNode.md) - -3. 请将 chatgpt-plugin 放置在 Yunzai-Bot 的 plugins 目录下 +2. 请将 chatgpt-plugin 放置在 Yunzai-Bot 的 plugins 目录下 推荐使用 git 进行安装,以方便后续升级。在 Yunzai-Bot 根目录夹打开终端,运行下述指令进行安装 @@ -49,7 +43,8 @@ pnpm install -w undici chatgpt showdown mathjax-node delay uuid remark strip-mar 如果您需要使用基于浏览器的访问模式,请运行下述指令 -> 浏览器模式仅为备选,如您需要使用浏览器模式,您还需要有**桌面环境**,优先级建议:必应>API>浏览器 +> 浏览器模式仅为备选,如您需要使用浏览器模式,您还需要有**桌面环境**,优先级建议:必应>API3>浏览器 +> 2.20更新:必应被大削,变得蠢了,建议还是API3优先 请注意:**若使用API模式,chatgpt的版本号注意要大于4.4.0** **若使用Bing模式,@waylaidwanderer/chatgpt-api 尽可能保持最新版本** diff --git a/REDIS b/REDIS deleted file mode 100644 index e69de29..0000000 diff --git a/apps/management.js b/apps/management.js index bbf2215..6f34abf 100644 --- a/apps/management.js +++ b/apps/management.js @@ -1,6 +1,8 @@ import plugin from '../../../lib/plugins/plugin.js' import { Config } from '../utils/config.js' import { BingAIClient } from '@waylaidwanderer/chatgpt-api' +import { exec } from 'child_process' +import {checkPnpm} from "../utils/common.js"; export class ChatgptManagement extends plugin { constructor (e) { @@ -62,6 +64,10 @@ export class ChatgptManagement extends plugin { { reg: '^#chatgpt模式$', fnc: 'modeHelp' + }, + { + reg: '^#chatgpt(强制)更新$', + fnc: 'updateChatGPTPlugin' } ] }) @@ -185,6 +191,69 @@ export class ChatgptManagement extends plugin { } } + async checkAuth (e) { + if (!e.isMaster) { + e.reply(`只有主人才能命令ChatGPT哦~ + (*/ω\*)`) + return false + } + return true + } + + async updateChatGPTPlugin (e) { + let timer + if (!await this.checkAuth(e)) { + return true + } + let isForce = e.msg.includes('强制') + let command = 'git pull' + if (isForce) { + command = 'git checkout . && git pull' + e.reply('正在执行强制更新操作,请稍等') + } else { + e.reply('正在执行更新操作,请稍等') + } + const _path = process.cwd() + exec(command, { cwd: `${_path}/plugins/miao-plugin/` }, async function (error, stdout, stderr) { + if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) { + e.reply('目前已经是最新版ChatGPT了~') + return true + } + if (error) { + e.reply('ChatGPT更新失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。') + return true + } + e.reply('ChatGPT更新成功,正在尝试重新启动Yunzai以应用更新...') + timer && clearTimeout(timer) + + let data = JSON.stringify({ + isGroup: !!this.e.isGroup, + id: this.e.isGroup ? this.e.group_id : this.e.user_id, + time: new Date().getTime() + }) + await redis.set('Yz:restart', data, { EX: 120 }) + let npm = await checkPnpm() + timer = setTimeout(function () { + let command = `${npm} start` + if (process.argv[1].includes('pm2')) { + command = `${npm} run restart` + } + exec(command, function (error, stdout, stderr) { + if (error) { + e.reply('自动重启失败,请手动重启以应用新版ChatGPT。\nError code: ' + error.code + '\n' + error.stack + '\n') + Bot.logger.error(`重启失败\n${error.stack}`) + return true + } else if (stdout) { + Bot.logger.mark('重启成功,运行已转为后台,查看日志请用命令:npm run log') + Bot.logger.mark('停止后台运行命令:npm stop') + process.exit() + } + }) + }, 1000) + }) + return true + } + async modeHelp () { let mode = await redis.get('CHATGPT:USE') const modeMap = { diff --git a/config/config.example.js b/config/config.example.js index b297b84..def12d6 100644 --- a/config/config.example.js +++ b/config/config.example.js @@ -34,8 +34,10 @@ export default { // 以下为API3方式的配置 * // *********************************************************************************************************************************** // from https://github.com/acheong08/ChatGPT + // 或者: https://gpt.pawan.krd/backend-api/conversation // api: 'https://chatgpt.duti.tech/api/conversation', - // apiBaseUrl: 'https://chatgpt.duti.tech', + // 或者 https://gpt.pawan.krd/backend-api + // apiBaseUrl: 'https://chatgpt.duti.tech/api', // *********************************************************************************************************************************** // 以下为API2方式的配置 * // *********************************************************************************************************************************** diff --git a/node_modules/.bin/chatgpt b/node_modules/.bin/chatgpt new file mode 100755 index 0000000..f758c1c --- /dev/null +++ b/node_modules/.bin/chatgpt @@ -0,0 +1,17 @@ +#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") + +case `uname` in + *CYGWIN*) basedir=`cygpath -w "$basedir"`;; +esac + +if [ -z "$NODE_PATH" ]; then + export NODE_PATH="/Users/ikechan8370/Yunzai-Bot/node_modules/.pnpm/node_modules" +else + export NODE_PATH="$NODE_PATH:/Users/ikechan8370/Yunzai-Bot/node_modules/.pnpm/node_modules" +fi +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/../../../../node_modules/.pnpm/chatgpt@4.7.1/node_modules/chatgpt/bin/cli.js" "$@" +else + exec node "$basedir/../../../../node_modules/.pnpm/chatgpt@4.7.1/node_modules/chatgpt/bin/cli.js" "$@" +fi diff --git a/node_modules/.bin/uuid b/node_modules/.bin/uuid new file mode 100755 index 0000000..b7c89bb --- /dev/null +++ b/node_modules/.bin/uuid @@ -0,0 +1,17 @@ +#!/bin/sh +basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')") + +case `uname` in + *CYGWIN*) basedir=`cygpath -w "$basedir"`;; +esac + +if [ -z "$NODE_PATH" ]; then + export NODE_PATH="/Users/ikechan8370/Yunzai-Bot/node_modules/.pnpm/node_modules" +else + export NODE_PATH="$NODE_PATH:/Users/ikechan8370/Yunzai-Bot/node_modules/.pnpm/node_modules" +fi +if [ -x "$basedir/node" ]; then + exec "$basedir/node" "$basedir/../../../../node_modules/.pnpm/uuid@9.0.0/node_modules/uuid/dist/bin/uuid" "$@" +else + exec node "$basedir/../../../../node_modules/.pnpm/uuid@9.0.0/node_modules/uuid/dist/bin/uuid" "$@" +fi diff --git a/node_modules/chatgpt b/node_modules/chatgpt new file mode 120000 index 0000000..d14849e --- /dev/null +++ b/node_modules/chatgpt @@ -0,0 +1 @@ +../../../node_modules/.pnpm/chatgpt@4.7.1/node_modules/chatgpt \ No newline at end of file diff --git a/node_modules/delay b/node_modules/delay new file mode 120000 index 0000000..3499a0b --- /dev/null +++ b/node_modules/delay @@ -0,0 +1 @@ +../../../node_modules/.pnpm/delay@5.0.0/node_modules/delay \ No newline at end of file diff --git a/node_modules/keyv-file b/node_modules/keyv-file new file mode 120000 index 0000000..aaf1f66 --- /dev/null +++ b/node_modules/keyv-file @@ -0,0 +1 @@ +../../../node_modules/.pnpm/keyv-file@0.2.0/node_modules/keyv-file \ No newline at end of file diff --git a/node_modules/node-fetch b/node_modules/node-fetch new file mode 120000 index 0000000..dabe7fd --- /dev/null +++ b/node_modules/node-fetch @@ -0,0 +1 @@ +../../../node_modules/.pnpm/node-fetch@3.3.0/node_modules/node-fetch \ No newline at end of file diff --git a/node_modules/puppeteer-extra b/node_modules/puppeteer-extra new file mode 120000 index 0000000..0fc6986 --- /dev/null +++ b/node_modules/puppeteer-extra @@ -0,0 +1 @@ +../../../node_modules/.pnpm/puppeteer-extra@3.3.4/node_modules/puppeteer-extra \ No newline at end of file diff --git a/node_modules/puppeteer-extra-plugin-recaptcha b/node_modules/puppeteer-extra-plugin-recaptcha new file mode 120000 index 0000000..83d8a2e --- /dev/null +++ b/node_modules/puppeteer-extra-plugin-recaptcha @@ -0,0 +1 @@ +../../../node_modules/.pnpm/puppeteer-extra-plugin-recaptcha@3.6.6_puppeteer-extra@3.3.4/node_modules/puppeteer-extra-plugin-recaptcha \ No newline at end of file diff --git a/node_modules/puppeteer-extra-plugin-stealth b/node_modules/puppeteer-extra-plugin-stealth new file mode 120000 index 0000000..8f01a02 --- /dev/null +++ b/node_modules/puppeteer-extra-plugin-stealth @@ -0,0 +1 @@ +../../../node_modules/.pnpm/puppeteer-extra-plugin-stealth@2.11.1_puppeteer-extra@3.3.4/node_modules/puppeteer-extra-plugin-stealth \ No newline at end of file diff --git a/node_modules/random b/node_modules/random new file mode 120000 index 0000000..7140886 --- /dev/null +++ b/node_modules/random @@ -0,0 +1 @@ +../../../node_modules/.pnpm/random@4.1.0/node_modules/random \ No newline at end of file diff --git a/node_modules/undici b/node_modules/undici new file mode 120000 index 0000000..e6148d4 --- /dev/null +++ b/node_modules/undici @@ -0,0 +1 @@ +../../../node_modules/.pnpm/undici@5.20.0/node_modules/undici \ No newline at end of file diff --git a/node_modules/uuid b/node_modules/uuid new file mode 120000 index 0000000..05090c1 --- /dev/null +++ b/node_modules/uuid @@ -0,0 +1 @@ +../../../node_modules/.pnpm/uuid@9.0.0/node_modules/uuid \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b46fc2a --- /dev/null +++ b/package.json @@ -0,0 +1,17 @@ +{ + "name": "chatgpt-plugin", + "type": "module", + "author": "ikechan8370", + "dependencies": { + "chatgpt": "^4.4.1", + "delay": "^5.0.0", + "keyv-file": "^0.2.0", + "node-fetch": "^3.3.0", + "puppeteer-extra": "^3.3.4", + "puppeteer-extra-plugin-recaptcha": "^3.6.6", + "puppeteer-extra-plugin-stealth": "^2.11.1", + "random": "^4.1.0", + "undici": "^5.19.1", + "uuid": "^9.0.0" + } +} \ No newline at end of file diff --git a/utils/common.js b/utils/common.js index 7ab54d7..bd72840 100644 --- a/utils/common.js +++ b/utils/common.js @@ -180,3 +180,10 @@ function getDOMException (errorMessage) { ? new Error(errorMessage) : new DOMException(errorMessage) } + +export async function checkPnpm () { + let npm = 'npm' + let ret = await this.execSync('pnpm -v') + if (ret.stdout) npm = 'pnpm' + return npm +} \ No newline at end of file diff --git a/utils/config.js b/utils/config.js index b8c9624..9d9abc8 100644 --- a/utils/config.js +++ b/utils/config.js @@ -10,8 +10,8 @@ const defaultConfig = { cacheUrl: 'https://content.alcedogroup.com', apiKey: '', model: '', - api: 'https://chatgpt.duti.tech/api/conversation', - apiBaseUrl: 'https://chatgpt.duti.tech', + api: 'https://gpt.pawan.krd/backend-api/conversation', + apiBaseUrl: 'https://chatgpt.duti.tech/api', plus: false, reverseProxy: 'https://chatgpt.pawan.krd/api/completions', promptPrefixOverride: 'Your answer shouldn\'t be too verbose. If you are generating a list, do not have too many items. Keep the number of items short. Prefer to answer in Chinese.', diff --git a/utils/conversation.js b/utils/conversation.js index 96fe0e3..885cde2 100644 --- a/utils/conversation.js +++ b/utils/conversation.js @@ -6,7 +6,7 @@ export async function getConversations (qq = '') { if (!accessToken) { throw new Error('未绑定ChatGPT AccessToken,请使用#chatgpt设置token命令绑定token') } - let response = await fetch(`${Config.apiBaseUrl}/api/conversations?offset=0&limit=20`, { + let response = await fetch(`${Config.apiBaseUrl}/conversations?offset=0&limit=20`, { method: 'GET', headers: { 'Content-Type': 'application/json', diff --git a/utils/redis-key.js b/utils/redis-key.js deleted file mode 100644 index e69de29..0000000