From a9207a7364c3ff713ea471167164aed9c9eae173 Mon Sep 17 00:00:00 2001 From: Neko Date: Tue, 1 Apr 2025 00:29:16 +0800 Subject: [PATCH] fix(telegram-bot): OOM when handling loop (#106) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 藍+85CD <50108258+kwaa@users.noreply.github.com> Co-authored-by: Chino Moka <31543482+QiroNT@users.noreply.github.com> --- .../telegram-bot/src/bots/telegram/index.ts | 24 ++++++++++++------- .../src/bots/telegram/loop/read-message.ts | 15 +++++++----- services/telegram-bot/src/llm/actions.ts | 4 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/services/telegram-bot/src/bots/telegram/index.ts b/services/telegram-bot/src/bots/telegram/index.ts index f1a615597..a7a6092cc 100644 --- a/services/telegram-bot/src/bots/telegram/index.ts +++ b/services/telegram-bot/src/bots/telegram/index.ts @@ -22,7 +22,7 @@ async function isChatIdBotAdmin(chatId: number) { return admins.includes(chatId.toString()) } -async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) { +async function handleLoopStep(state: BotSelf, msgs?: LLMMessage[], chatId?: string): Promise<() => Promise | undefined> { // Set the start time when beginning new processing state.currentProcessingStartTime = Date.now() @@ -96,7 +96,7 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) if (shouldInterrupt) { state.logger.log(`Interrupting message processing for chat ${action.chatId} - new messages deemed more important`) - return handleLoop(state) + return () => handleLoopStep(state) } else { state.logger.log(`Continuing current processing despite new messages in chat ${action.chatId}`) @@ -123,18 +123,16 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) // } await readMessage(state, state.bot.botInfo.id.toString(), chatId, action, unreadMessagesForThisChat, currentController) - break + return case 'listChats': msgs.push(message.user(`List of chats:${(await listJoinedChats()).map(chat => `ID:${chat.chat_id}, Name:${chat.chat_name}`).join('\n')}`)) - await handleLoop(state, msgs, chatId) - break + return () => handleLoopStep(state, msgs, chatId) case 'sendMessage': await sendMayStructuredMessage(state, action.content, action.groupId) - break + return default: msgs.push(message.user(`The action you sent ${action.action} haven't implemented yet by developer.`)) - await handleLoop(state, msgs, chatId) - break + return () => handleLoopStep(state, msgs, chatId) } } catch (err) { @@ -154,6 +152,16 @@ async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) } } +async function handleLoop(state: BotSelf, msgs?: LLMMessage[], chatId?: string) { + let result = await handleLoopStep(state, msgs, chatId) + + while (typeof result === 'function') { + result = await result() + } + + return result +} + function loop(state: BotSelf) { setTimeout(() => { handleLoop(state) diff --git a/services/telegram-bot/src/bots/telegram/loop/read-message.ts b/services/telegram-bot/src/bots/telegram/loop/read-message.ts index 4893862f8..33c8bc7ba 100644 --- a/services/telegram-bot/src/bots/telegram/loop/read-message.ts +++ b/services/telegram-bot/src/bots/telegram/loop/read-message.ts @@ -10,7 +10,7 @@ import { message } from '@xsai/utils-chat' import { findLastNMessages, findRelevantMessages } from '../../../models' import { recordChatCompletions } from '../../../models/chat-completions-history' import { chatMessageToOneLine, telegramMessageToOneLine } from '../../../models/common' -import { systemPrompt } from '../../../prompts/system-v1' +import { personality } from '../../../prompts/system-v1' import { sendMayStructuredMessage } from '../utils/message' export async function readMessage( @@ -57,20 +57,23 @@ export async function readMessage( state.unreadMessages[action.chatId] = [] const messages = message.messages( - systemPrompt(), + personality(), message.user('' + `Currently, it\'s ${new Date()} on the server that hosts you.` + 'The others in the group may live in a different timezone, so please be aware of the time difference.' + '\n' - + 'Last 30 messages:\n' - + `${lastNMessagesOneliner || 'No messages'}` + + 'You choose to read the messages from the group (perhaps you are already engaging the topics in the group).' + + 'Imaging you are using Telegram app on the mobile phone, and you are reading the messages from the group chat.' + '\n' - + 'I helped you searched these relevant chat messages may help you recall the memories:\n' - + `${relevantChatMessagesOneliner || 'No relevant messages'}` + + 'Previous 30 messages (including what you said):\n' + + `${lastNMessagesOneliner || 'No messages'}` + '\n' + 'All the messages you requested to read:\n' + `${unreadHistoryMessageOneliner || 'No messages'}` + '\n' + + 'Relevant chat messages may help you recall the memories:\n' + + `${relevantChatMessagesOneliner || 'No relevant messages'}` + + '\n' + 'Based on your personalities, imaging you have your own choice and interest over different topics, ' + 'giving the above context and chat history, would you like to participate in the conversation ' + 'about the topic? Or will you aggressively diss or piss off about the opinions of others?\n' diff --git a/services/telegram-bot/src/llm/actions.ts b/services/telegram-bot/src/llm/actions.ts index c02074ec9..165cc42f0 100644 --- a/services/telegram-bot/src/llm/actions.ts +++ b/services/telegram-bot/src/llm/actions.ts @@ -9,7 +9,7 @@ import { message } from '@xsai/utils-chat' import { parse } from 'best-effort-json-parser' import { recordChatCompletions } from '../models/chat-completions-history' -import { systemPrompt } from '../prompts/system-v1' +import { personality } from '../prompts/system-v1' export async function imagineAnAction( _botId: string, @@ -26,7 +26,7 @@ export async function imagineAnAction( agentMessages.push( message.system('' - + `${systemPrompt().content}` + + `${personality().content}` + '\n' + 'I am one of your system component, called Ticking system, which is responsible to keep track of the time, and ' + 'help you schedule, retain focus, and keep eyes on different tasks, and ideas you have.'