Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot reply message logic #327

Merged
merged 13 commits into from
Oct 6, 2023
36 changes: 18 additions & 18 deletions package-lock.json

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

14 changes: 3 additions & 11 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import { OneCountryBot } from './modules/1country'
import { WalletConnect } from './modules/walletconnect'
import { BotPayments } from './modules/payment'
import { BotSchedule } from './modules/schedule'
import { LlmsBot } from './modules/llms'
import { DocumentHandler } from './modules/document-handler'
import config from './config'
import { commandsHelpText, FEEDBACK, LOVE, MODELS, SUPPORT, TERMS, LANG } from './constants'
import prometheusRegister, { PrometheusMetrics } from './metrics/prometheus'
Expand All @@ -43,7 +41,6 @@ import { autoRetry } from '@grammyjs/auto-retry'
import { run } from '@grammyjs/runner'
import { runBotHeartBit } from './monitoring/monitoring'
import { type BotPaymentLog } from './database/stats.service'
// import { getChatMemberInfo } from './modules/open-ai/utils/web-crawler'
import { TelegramPayments } from './modules/telegram_payment'
import * as Sentry from '@sentry/node'
import * as Events from 'events'
Expand Down Expand Up @@ -190,7 +187,9 @@ function createInitialSessionData (): BotSessionData {
collections: {
activeCollections: [],
collectionRequestQueue: [],
isProcessingQueue: false
isProcessingQueue: false,
currentCollection: '',
collectionConversation: []
},
llms: {
model: config.llms.model,
Expand Down Expand Up @@ -225,8 +224,6 @@ const schedule = new BotSchedule(bot)
const openAiBot = new OpenAIBot(payments)
const oneCountryBot = new OneCountryBot(payments)
const translateBot = new TranslateBot()
const llmsBot = new LlmsBot(payments)
const documentBot = new DocumentHandler()
const telegramPayments = new TelegramPayments(payments)
const voiceTranslateBot = new VoiceTranslateBot(payments)
const textToSpeechBot = new TextToSpeechBot(payments)
Expand Down Expand Up @@ -342,18 +339,13 @@ const PayableBots: Record<string, PayableBotConfig> = {
sdImagesBot: { bot: sdImagesBot },
voiceTranslate: { bot: voiceTranslateBot },
voiceMemo: { bot: voiceMemo },
documentBot: { bot: documentBot },
translateBot: { bot: translateBot },
textToSpeech: { bot: textToSpeechBot },
voiceToText: { bot: voiceToTextBot },
openAiBot: {
enabled: (ctx: OnMessageContext) => ctx.session.openAi.imageGen.isEnabled,
bot: openAiBot
},
llmsBot: {
enabled: (ctx: OnMessageContext) => ctx.session.openAi.imageGen.isEnabled,
bot: llmsBot
},
oneCountryBot: { bot: oneCountryBot }
}

Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default {
? parseInt(process.env.SESSION_TIMEOUT)
: 48, // in hours
llms: {
apiEndpoint: process.env.LLMS_ENDPOINT ?? '',
apiEndpoint: process.env.LLMS_ENDPOINT, // 'http://127.0.0.1:5000',
wordLimit: 50,
model: 'chat-bison',
minimumBalance: 0,
Expand Down
24 changes: 24 additions & 0 deletions src/modules/1country/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { MAX_TRIES, sendMessage } from '../open-ai/helpers'
import { sleep } from '../sd-images/utils'
import { isValidUrl } from '../open-ai/utils/web-crawler'
import { now } from '../../utils/perf'
import OpenAI from 'openai'

export const SupportedCommands = {
register: { name: 'rent' },
Expand Down Expand Up @@ -525,6 +526,14 @@ export class OneCountryBot implements PayableBot {
return input.replace(/[^a-z0-9-]/g, '').toLowerCase()
}

async onEnd (ctx: OnMessageContext | OnCallBackQueryData): Promise<void> {
ctx.session.collections.activeCollections = []
ctx.session.collections.collectionConversation = []
ctx.session.collections.collectionRequestQueue = []
ctx.session.collections.currentCollection = ''
ctx.session.collections.isProcessingQueue = false
}

async onError (
ctx: OnMessageContext | OnCallBackQueryData,
ex: any,
Expand Down Expand Up @@ -573,6 +582,21 @@ export class OneCountryBot implements PayableBot {
`On method "${ex.method}" | ${ex.error_code} - ${ex.description}`
)
}
} else if (ex instanceof OpenAI.APIError) {
// 429 RateLimitError
// e.status = 400 || e.code = BadRequestError
this.logger.error(`OPENAI Error ${ex.status}(${ex.code}) - ${ex.message}`)
if (ex.code === 'context_length_exceeded') {
await sendMessage(ctx, ex.message).catch(async (e) => { await this.onError(ctx, e, retryCount - 1) })
ctx.transient.analytics.actualResponseTime = now()
await this.onEnd(ctx)
} else {
await sendMessage(
ctx,
'Error accessing OpenAI (ChatGPT). Please try later'
).catch(async (e) => { await this.onError(ctx, e, retryCount - 1) })
ctx.transient.analytics.actualResponseTime = now()
}
} else {
this.logger.error(`${ex.toString()}`)
await sendMessage(ctx, 'Error handling your request')
Expand Down
Loading