Skip to content

Commit

Permalink
Merge pull request #472 from fuergaosi233/feat/add-retry
Browse files Browse the repository at this point in the history
✨ Add retry to start chatgpt
  • Loading branch information
fuergaosi233 authored Jan 7, 2023
2 parents 2115813 + a95ff00 commit 16be236
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 14 deletions.
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
52 changes: 38 additions & 14 deletions src/chatgpt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatGPTAPI, ChatGPTAPIBrowser } from "chatgpt";

import { config } from "./config.js";

import AsyncRetry from "async-retry";
import {
IChatGPTItem,
IConversationItem,
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 16be236

Please sign in to comment.