From 326f46ea0292e29821409d24875e94742e8ecf42 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Sun, 2 Apr 2023 21:22:43 +0200 Subject: [PATCH] cleanup logging output --- matrix_pygmalion_bot/ai/runpod_pygmalion.py | 8 ++++-- matrix_pygmalion_bot/chatlog.py | 4 +++ matrix_pygmalion_bot/core.py | 31 ++++++++++----------- matrix_pygmalion_bot/helpers.py | 6 +++- matrix_pygmalion_bot/translate.py | 6 +++- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/matrix_pygmalion_bot/ai/runpod_pygmalion.py b/matrix_pygmalion_bot/ai/runpod_pygmalion.py index 0cf5813..2a367d5 100644 --- a/matrix_pygmalion_bot/ai/runpod_pygmalion.py +++ b/matrix_pygmalion_bot/ai/runpod_pygmalion.py @@ -57,6 +57,7 @@ async def generate_sync( else: reply = answer.removesuffix('<|endoftext|>').strip() reply.replace("\n{bot_name}: ", " ") + reply.replace("\n: ", " ") return reply elif status == 'IN_PROGRESS' or status == 'IN_QUEUE': job_id = r_json["id"] @@ -83,6 +84,7 @@ async def generate_sync( else: reply = answer.removesuffix('<|endoftext|>').strip() reply = reply.replace("\n{bot_name}: ", " ") + reply.replace("\n: ", " ") return reply else: return "" @@ -114,15 +116,15 @@ async def get_full_prompt(simple_prompt: str, bot, chat_history): continue #if chat_item.message["en"] == bot.greeting: # continue - print("History: " + str(chat_item)) if chat_item.num_tokens == None: chat_item.num_tokens = await num_tokens("{}: {}".format(chat_item.user_name, chat_item.message["en"])) # TODO: is it MAX_TOKENS or MAX_TOKENS - max_new_tokens?? + logger.debug(f"History: " + str(chat_item) + " [" + str(chat_item.num_tokens) + "]") if total_num_tokens < (MAX_TOKENS - max_new_tokens): visible_history.append(chat_item) total_num_tokens += chat_item.num_tokens - print(total_num_tokens) - print("Finally: "+ str(total_num_tokens)) + else: + break visible_history = reversed(visible_history) prompt = bot.name + "'s Persona: " + bot.persona + "\n" diff --git a/matrix_pygmalion_bot/chatlog.py b/matrix_pygmalion_bot/chatlog.py index f827822..ff3e855 100644 --- a/matrix_pygmalion_bot/chatlog.py +++ b/matrix_pygmalion_bot/chatlog.py @@ -1,5 +1,9 @@ import os import matrix_pygmalion_bot.translate as translate +import logging + +logger = logging.getLogger(__name__) + class ChatMessage: def __init__(self, event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language="en", english_original_message=None): diff --git a/matrix_pygmalion_bot/core.py b/matrix_pygmalion_bot/core.py index b7d916a..d7135a2 100644 --- a/matrix_pygmalion_bot/core.py +++ b/matrix_pygmalion_bot/core.py @@ -60,7 +60,7 @@ class Callbacks(object): chat_message = self.bot.chat_history.room(room.display_name).add(event.event_id, event.server_timestamp, room.user_name(event.sender), event.sender == self.client.user, is_command, relates_to, event.body, language, english_original_message) if self.bot.not_synced: return - print( + logger.info( "Message received for room {} | {}: {}".format( room.display_name, room.user_name(event.sender), event.body ) @@ -149,8 +149,8 @@ class Callbacks(object): full_prompt = await ai.get_full_prompt(chat_message.getTranslation("en"), self.bot, self.bot.chat_history.room(room.display_name)) num_tokens = await ai.num_tokens(full_prompt) - logger.info(full_prompt) - logger.info(f"num tokens:" + str(num_tokens)) + logger.debug(full_prompt) + logger.debug(f"Prompt has " + str(num_tokens) + " tokens") # answer = "" # time = 0 # error = None @@ -181,11 +181,10 @@ class Callbacks(object): async def invite_cb(self, room: MatrixRoom, event: InviteEvent) -> None: """Automatically join all rooms we get invited to""" result = await self.client.join(room.room_id) - print('Invited to room: {} {}'.format(room.name, room.room_id)) if isinstance(result, nio.responses.JoinResponse): - print('Joined') + logger.info('Invited and joined room: {} {}'.format(room.name, room.room_id)) else: - print("Error joining room: {}".format(str(result))) + logger.error("Error joining room: {}".format(str(result))) class ChatBot(object): """Main chatbot""" @@ -243,7 +242,7 @@ class ChatBot(object): item = item.strip() event = Event(int(items[0]), int(items[1]), float(items[2]), int(items[3]), items[4].lstrip()) self.events.append(event) - logger.info("event added to event_loop") + logger.debug("event added to event_loop") pass async def login(self): @@ -261,17 +260,17 @@ class ChatBot(object): try: response = await self.client.login(self.password) - print(response) + logger.info(response) #sync_forever_task = asyncio.create_task(self.client.sync_forever(timeout=30000, full_state=True)) except (asyncio.CancelledError, KeyboardInterrupt): - print("Received interrupt.") + logger.error("Received interrupt while login.") await self.client.close() #return sync_forever_task async def watch_for_sync(self, sync_event): - print("Awaiting sync") + logger.debug("Awaiting sync") await sync_event.wait() - print("Client is synced") + logger.debug("Client is synced") self.not_synced = False async def read_conf2(self, section): @@ -319,9 +318,9 @@ class ChatBot(object): filesize=file_stat.st_size, ) if isinstance(resp, UploadResponse): - print("Image was uploaded successfully to server. ") + logger.info("Image was uploaded successfully to server. ") else: - print(f"Failed to upload image. Failure response: {resp}") + logger.error(f"Failed to upload image. Failure response: {resp}") content = { "body": os.path.basename(image), # descriptive title @@ -339,9 +338,9 @@ class ChatBot(object): try: await client.room_send(room_id, message_type="m.room.message", content=content) - print("Image was sent successfully") + logger.info("Image was sent successfully") except Exception: - print(f"Image send of file {image} failed.") + logger.error(f"Image send of file {image} failed.") async def main() -> None: config.read('bot.conf') @@ -372,7 +371,7 @@ async def main() -> None: await bot.read_conf2(section) bots.append(bot) await bot.login() - print("gather") + #logger.info("gather") if sys.version_info[0] == 3 and sys.version_info[1] < 11: tasks = [] for bot in bots: diff --git a/matrix_pygmalion_bot/helpers.py b/matrix_pygmalion_bot/helpers.py index 9fd71fa..cc4aded 100644 --- a/matrix_pygmalion_bot/helpers.py +++ b/matrix_pygmalion_bot/helpers.py @@ -1,4 +1,8 @@ import time +import logging + +logger = logging.getLogger(__name__) + class Event: def __init__(self, tick_start, tick_stop, chance, repeat, command): @@ -17,7 +21,7 @@ class Event: elif (self.tick_stop == 0 or self.tick_stop == self.tick_start) and self.executed == 0: self.execute(bot, tick) def execute(self, bot, tick): - print("event executed for " + bot.name + ". current tick: " + str(tick) + " event: " + str(self.command)) + logger.info("event executed for " + bot.name + ". current tick: " + str(tick) + " event: " + str(self.command)) if self.command.startswith('printtime'): print(time.time()//1000) self.executed += 1 diff --git a/matrix_pygmalion_bot/translate.py b/matrix_pygmalion_bot/translate.py index 93b4f9d..6222de7 100644 --- a/matrix_pygmalion_bot/translate.py +++ b/matrix_pygmalion_bot/translate.py @@ -1,9 +1,13 @@ import argostranslate.package import argostranslate.translate +import logging + +logger = logging.getLogger(__name__) #from_code = "de" #to_code = "en" + def init(from_code, to_code): # Download and install Argos Translate package argostranslate.package.update_package_index() @@ -20,6 +24,6 @@ def translate(text, from_code, to_code): return text # Translate translatedText = argostranslate.translate.translate(text, from_code, to_code) - print('Translated: \"{}\" -> \"{}\"'.format(text, translatedText)) + logger.info('Translated: \"{}\" -> \"{}\"'.format(text, translatedText)) return translatedText # '¡Hola Mundo!'