From a95ff0047e2e5d47cda4213eed910cb9e635a999 Mon Sep 17 00:00:00 2001 From: Holegots Date: Sun, 8 Jan 2023 00:40:57 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20retry=20to=20start=20chatgpt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 63 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/chatgpt.ts | 52 +++++++++++++++++++++++++++----------- 3 files changed, 103 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 13a61c8..6c185f9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "async-retry": "^1.3.3", "chatgpt": "^3.3.9", "dotenv": "^16.0.3", "execa": "^6.1.0", @@ -19,6 +20,7 @@ "yaml": "^2.1.3" }, "devDependencies": { + "@types/async-retry": "^1.4.5", "@types/qrcode": "^1.5.0", "@types/uuid": "^9.0.0", "nodemon": "^2.0.20", @@ -680,6 +682,15 @@ "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==", "dev": true }, + "node_modules/@types/async-retry": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/@types/async-retry/-/async-retry-1.4.5.tgz", + "integrity": "sha512-YrdjSD+yQv7h6d5Ip+PMxh3H6ZxKyQk0Ts+PvaNRInxneG9PFVZjFg77ILAN+N6qYf7g4giSJ1l+ZjQ1zeegvA==", + "dev": true, + "dependencies": { + "@types/retry": "*" + } + }, "node_modules/@types/debug": { "version": "4.1.7", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.7.tgz", @@ -720,6 +731,12 @@ "@types/node": "*" } }, + "node_modules/@types/retry": { + "version": "0.12.2", + "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.2.tgz", + "integrity": "sha512-XISRgDJ2Tc5q4TRqvgJtzsRkFYNJzZrhTdtMoGVBttwzzQJkPnS3WWTFc7kuDRoPtPakl+T+OfdEUjYJj7Jbow==", + "dev": true + }, "node_modules/@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -911,6 +928,22 @@ "npm": ">=7" } }, + "node_modules/async-retry": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/async-retry/-/async-retry-1.3.3.tgz", + "integrity": "sha512-wfr/jstw9xNi/0teMHrRW7dsz3Lt5ARhYNZ2ewpadnhaIp5mbALhOAP+EAdsC7t4Z6wqsDVv9+W6gm1Dk9mEyw==", + "dependencies": { + "retry": "0.13.1" + } + }, + "node_modules/async-retry/node_modules/retry": { + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz", + "integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==", + "engines": { + "node": ">= 4" + } + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -6086,6 +6119,15 @@ "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==", "dev": true }, + "@types/async-retry": { + "version": "1.4.5", + "resolved": "https://registry.npmjs.org/@types/async-retry/-/async-retry-1.4.5.tgz", + "integrity": "sha512-YrdjSD+yQv7h6d5Ip+PMxh3H6ZxKyQk0Ts+PvaNRInxneG9PFVZjFg77ILAN+N6qYf7g4giSJ1l+ZjQ1zeegvA==", + "dev": true, + "requires": { + "@types/retry": "*" + } + }, "@types/debug": { "version": "4.1.7", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.7.tgz", @@ -6126,6 +6168,12 @@ "@types/node": "*" } }, + "@types/retry": { + "version": "0.12.2", + "resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.2.tgz", + "integrity": "sha512-XISRgDJ2Tc5q4TRqvgJtzsRkFYNJzZrhTdtMoGVBttwzzQJkPnS3WWTFc7kuDRoPtPakl+T+OfdEUjYJj7Jbow==", + "dev": true + }, "@types/unist": { "version": "2.0.6", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.6.tgz", @@ -6264,6 +6312,21 @@ "resolved": "https://registry.npmjs.org/async-map-like/-/async-map-like-1.0.2.tgz", "integrity": "sha512-TfbF6NQOVZPKGXhQmfh8gOzC7+XSBGmqU6eqRoMdc4soScKkkui5ZwV40A4Scc3fWP+zZ5qJjOXzCuksUWqluA==" }, + "async-retry": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/async-retry/-/async-retry-1.3.3.tgz", + "integrity": "sha512-wfr/jstw9xNi/0teMHrRW7dsz3Lt5ARhYNZ2ewpadnhaIp5mbALhOAP+EAdsC7t4Z6wqsDVv9+W6gm1Dk9mEyw==", + "requires": { + "retry": "0.13.1" + }, + "dependencies": { + "retry": { + "version": "0.13.1", + "resolved": "https://registry.npmjs.org/retry/-/retry-0.13.1.tgz", + "integrity": "sha512-XQBQ3I8W1Cge0Seh+6gjj03LbmRFWuoszgK9ooCpwYIrhhoO80pfq4cUkU5DkknwfOfFteRwlZ56PYOGYyFWdg==" + } + } + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", diff --git a/package.json b/package.json index b222ac5..6e72033 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "ISC", "dependencies": { + "async-retry": "^1.3.3", "chatgpt": "^3.3.9", "dotenv": "^16.0.3", "execa": "^6.1.0", @@ -21,6 +22,7 @@ "yaml": "^2.1.3" }, "devDependencies": { + "@types/async-retry": "^1.4.5", "@types/qrcode": "^1.5.0", "@types/uuid": "^9.0.0", "nodemon": "^2.0.20", diff --git a/src/chatgpt.ts b/src/chatgpt.ts index c82b76b..a509fd6 100644 --- a/src/chatgpt.ts +++ b/src/chatgpt.ts @@ -1,7 +1,7 @@ import { ChatGPTAPI, ChatGPTAPIBrowser } from "chatgpt"; import { config } from "./config.js"; - +import AsyncRetry from "async-retry"; import { IChatGPTItem, IConversationItem, @@ -63,19 +63,43 @@ export class ChatGPTPool { this.conversationsPool.delete(talkid); } async startPools() { - this.chatGPTPools = await Promise.all( - config.chatGPTAccountPool.map(async (account) => { - const chatGpt = new ChatGPTAPIBrowser({ - ...account, - proxyServer: config.openAIProxy, - }); - await chatGpt.initSession(); - return { - chatGpt: chatGpt, - account: account, - }; - }) - ); + const chatGPTPools = []; + for (const account of config.chatGPTAccountPool) { + const chatGpt = new ChatGPTAPIBrowser({ + ...account, + proxyServer: config.openAIProxy, + }); + try { + await AsyncRetry( + async () => { + await chatGpt.initSession(); + }, + { retries: 3 } + ); + chatGPTPools.push({ + chatGpt: chatGpt, + account: account, + }); + } catch { + console.error( + `Try init account: ${account.email} failed, remove it from pool` + ); + } + } + // this.chatGPTPools = await Promise.all( + // config.chatGPTAccountPool.map(async (account) => { + // const chatGpt = new ChatGPTAPIBrowser({ + // ...account, + // proxyServer: config.openAIProxy, + // }); + // await chatGpt.initSession(); + // return { + // chatGpt: chatGpt, + // account: account, + // }; + // }) + // ); + this.chatGPTPools = chatGPTPools; if (this.chatGPTPools.length === 0) { throw new Error("⚠️ No chatgpt account in pool"); }