Skip to content

Commit

Permalink
feat: minor fixes for 429s and 403s
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit committed Dec 15, 2022
1 parent 88ad997 commit 81450d9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
# -----------------------------------------------------------------------------

# see the `chatgpt` npm package readme for how to find this
SESSION_TOKEN=
CLEARANCE_TOKEN=
CHATGPT_ACCOUNTS

# -----------------------------------------------------------------------------
# Twitter API v2 (using OAuth 2.0)
Expand Down
40 changes: 29 additions & 11 deletions src/chatgpt-api-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
}

const authInfo = await getOpenAIAuth(accountInit)
console.log(accountId, authInfo)
api = new ChatGPTAPI({
...authInfo,
...this._chatgptapiOptions
Expand Down Expand Up @@ -180,6 +181,7 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
}

if (this._accountsOnCooldown.size >= this.accounts.length) {
console.log(`ChatGPT all accounts are on cooldown; sleeping...`)
// All API accounts are on cooldown, so wait and try again
await delay(1000)
}
Expand All @@ -206,7 +208,7 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
if (!this._accountsOnCooldown.has(account.id)) {
return account
}
} while (numTries < 10)
} while (numTries < 3)

const error = new ChatError(
`ChatGPTAPIPool account on cooldown "${accountId}"`
Expand Down Expand Up @@ -278,6 +280,8 @@ export class ChatGPTAPIPool extends ChatGPTAPI {

if (account.email && account.password) {
const authInfo = await getOpenAIAuth(account)
console.log(accountId, authInfo)

account.api = new ChatGPTAPI({
...this._chatgptapiOptions,
...authInfo
Expand Down Expand Up @@ -315,10 +319,7 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
// If there is no account specified, but the request is part of an existing
// conversation, then use the default account which handled all conversations
// before we added support for multiple accounts.
accountId =
this.accounts.find(
(account) => account.id === 'fisch09202+8@gmail.com'
)?.id || this.accounts[0].id
accountId = this.accounts[0].id
}

if (accountId) {
Expand All @@ -329,20 +330,32 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
// we previously used in this conversation is no longer available... I'm
// really not sure how to handle this aside from throwing an unrecoverable
// error to the user
const error = new ChatError(
`ChatGPTAPIPool account not found "${accountId}"`
console.warn(
`chatgpt account "${accountId}" not found; falling back to new account`
)
error.type = 'chatgpt:pool:account-not-found'
error.isFinal = true
error.accountId = accountId
throw error

accountId = null
opts.conversationId = undefined
opts.parentMessageId = undefined
account = await this.getAPIAccount()

// const error = new ChatError(
// `ChatGPTAPIPool account not found "${accountId}"`
// )
// error.type = 'chatgpt:pool:account-not-found'
// error.isFinal = false
// error.accountId = accountId
// throw error
}
} else {
account = await this.getAPIAccount()
}
}

console.log('using chatgpt account', account.id)
const moderationPre = await account.api.sendModeration(prompt)
console.log('chatgpt moderation pre', account.id, moderationPre)

const response = await account.api.sendMessage(prompt, rest)

const responseL = response.toLowerCase()
Expand Down Expand Up @@ -375,6 +388,11 @@ export class ChatGPTAPIPool extends ChatGPTAPI {
return null
}

const moderationPost = await account.api.sendModeration(
`${prompt} ${response}`
)
console.log('chatgpt moderation post', account.id, moderationPost)

return { response, accountId: account.id }
} catch (err) {
if (err.name === 'TimeoutError') {
Expand Down
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,27 +231,27 @@ async function main() {
console.log(
`rate limited ${
session.isRateLimited ? 'chatgpt' : 'twitter'
}; sleeping...`
}; sleeping for 2m...`
)
await delay(2 * 60 * 1000) // 1m
await delay(2 * 60 * 1000) // 2m

if (session.isRateLimitedTwitter) {
console.log('sleeping longer for twitter rate limit...')
console.log('sleeping longer for twitter rate limit (5m)...')
await delay(5 * 60 * 1000) // 5m
}
}

const validSessionInteractions = session.interactions.filter(
(interaction) =>
!interaction.error && interaction.responseTweetIds?.length
)
// const validSessionInteractions = session.interactions.filter(
// (interaction) =>
// !interaction.error && interaction.responseTweetIds?.length
// )

if (!validSessionInteractions?.length) {
console.log('sleeping...')
if (!session.interactions?.length) {
console.log('sleeping for 30s...')
// sleep if there were no mentions to process
await delay(30000) // 30s
} else {
console.log('sleeping...')
console.log('sleeping for 2s...')
// still sleep if there are active mentions because of rate limits...
await delay(2000)
}
Expand Down
2 changes: 1 addition & 1 deletion src/mentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export async function getTweetMentionsBatch({
// - older tweets that we haven't responded to yet get a small boost
for (let i = 0; i < numMentionsCandidates; ++i) {
const mention = batch.mentions[i]
let score = (numMentionsCandidates - i) / numMentionsCandidates
let score = (0.5 * (numMentionsCandidates - i)) / numMentionsCandidates

const repliedToTweetRef = mention.referenced_tweets?.find(
(t) => t.type === 'replied_to'
Expand Down
4 changes: 2 additions & 2 deletions src/respond-to-new-mentions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export async function respondToNewMentions({
if (index > 0) {
// slight slow down between ChatGPT requests
console.log('pausing for chatgpt...')
await delay(4000)
await delay(6000)
}

try {
Expand Down Expand Up @@ -396,7 +396,7 @@ export async function respondToNewMentions({
} else if (err.type === 'chatgpt:pool:rate-limit') {
// That account will be taken out of the pool and put on cooldown, but
// for a hard 429, let's still rate limit ourselves to avoid IP bans.
// session.isRateLimited = true
session.isRateLimited = true
} else if (err.type === 'chatgpt:pool:account-not-found') {
console.error(err.toString())

Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function getChatGPTResponse(

if (chatgpt instanceof ChatGPTAPIPool) {
const res = await chatgpt.sendMessageToAccount(prompt, {
timeoutMs: timeoutMs,
timeoutMs,
conversationId,
parentMessageId,
accountId: chatgptAccountId,
Expand All @@ -69,7 +69,7 @@ export async function getChatGPTResponse(
response = res.response
} else {
response = await chatgpt.sendMessage(prompt, {
timeoutMs: timeoutMs,
timeoutMs,
conversationId,
parentMessageId,
onConversationResponse
Expand Down

0 comments on commit 81450d9

Please sign in to comment.