Browse Source

cleanup logging output

master
Hendrik Langer 2 years ago
parent
commit
326f46ea02
  1. 8
      matrix_pygmalion_bot/ai/runpod_pygmalion.py
  2. 4
      matrix_pygmalion_bot/chatlog.py
  3. 31
      matrix_pygmalion_bot/core.py
  4. 6
      matrix_pygmalion_bot/helpers.py
  5. 6
      matrix_pygmalion_bot/translate.py

8
matrix_pygmalion_bot/ai/runpod_pygmalion.py

@ -57,6 +57,7 @@ async def generate_sync(
else: else:
reply = answer.removesuffix('<|endoftext|>').strip() reply = answer.removesuffix('<|endoftext|>').strip()
reply.replace("\n{bot_name}: ", " ") reply.replace("\n{bot_name}: ", " ")
reply.replace("\n<BOT>: ", " ")
return reply return reply
elif status == 'IN_PROGRESS' or status == 'IN_QUEUE': elif status == 'IN_PROGRESS' or status == 'IN_QUEUE':
job_id = r_json["id"] job_id = r_json["id"]
@ -83,6 +84,7 @@ async def generate_sync(
else: else:
reply = answer.removesuffix('<|endoftext|>').strip() reply = answer.removesuffix('<|endoftext|>').strip()
reply = reply.replace("\n{bot_name}: ", " ") reply = reply.replace("\n{bot_name}: ", " ")
reply.replace("\n<BOT>: ", " ")
return reply return reply
else: else:
return "<ERROR>" return "<ERROR>"
@ -114,15 +116,15 @@ async def get_full_prompt(simple_prompt: str, bot, chat_history):
continue continue
#if chat_item.message["en"] == bot.greeting: #if chat_item.message["en"] == bot.greeting:
# continue # continue
print("History: " + str(chat_item))
if chat_item.num_tokens == None: if chat_item.num_tokens == None:
chat_item.num_tokens = await num_tokens("{}: {}".format(chat_item.user_name, chat_item.message["en"])) 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?? # 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): if total_num_tokens < (MAX_TOKENS - max_new_tokens):
visible_history.append(chat_item) visible_history.append(chat_item)
total_num_tokens += chat_item.num_tokens total_num_tokens += chat_item.num_tokens
print(total_num_tokens) else:
print("Finally: "+ str(total_num_tokens)) break
visible_history = reversed(visible_history) visible_history = reversed(visible_history)
prompt = bot.name + "'s Persona: " + bot.persona + "\n" prompt = bot.name + "'s Persona: " + bot.persona + "\n"

4
matrix_pygmalion_bot/chatlog.py

@ -1,5 +1,9 @@
import os import os
import matrix_pygmalion_bot.translate as translate import matrix_pygmalion_bot.translate as translate
import logging
logger = logging.getLogger(__name__)
class ChatMessage: 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): def __init__(self, event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language="en", english_original_message=None):

31
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) 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: if self.bot.not_synced:
return return
print( logger.info(
"Message received for room {} | {}: {}".format( "Message received for room {} | {}: {}".format(
room.display_name, room.user_name(event.sender), event.body 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)) 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) num_tokens = await ai.num_tokens(full_prompt)
logger.info(full_prompt) logger.debug(full_prompt)
logger.info(f"num tokens:" + str(num_tokens)) logger.debug(f"Prompt has " + str(num_tokens) + " tokens")
# answer = "" # answer = ""
# time = 0 # time = 0
# error = None # error = None
@ -181,11 +181,10 @@ class Callbacks(object):
async def invite_cb(self, room: MatrixRoom, event: InviteEvent) -> None: async def invite_cb(self, room: MatrixRoom, event: InviteEvent) -> None:
"""Automatically join all rooms we get invited to""" """Automatically join all rooms we get invited to"""
result = await self.client.join(room.room_id) result = await self.client.join(room.room_id)
print('Invited to room: {} {}'.format(room.name, room.room_id))
if isinstance(result, nio.responses.JoinResponse): if isinstance(result, nio.responses.JoinResponse):
print('Joined') logger.info('Invited and joined room: {} {}'.format(room.name, room.room_id))
else: else:
print("Error joining room: {}".format(str(result))) logger.error("Error joining room: {}".format(str(result)))
class ChatBot(object): class ChatBot(object):
"""Main chatbot""" """Main chatbot"""
@ -243,7 +242,7 @@ class ChatBot(object):
item = item.strip() item = item.strip()
event = Event(int(items[0]), int(items[1]), float(items[2]), int(items[3]), items[4].lstrip()) event = Event(int(items[0]), int(items[1]), float(items[2]), int(items[3]), items[4].lstrip())
self.events.append(event) self.events.append(event)
logger.info("event added to event_loop") logger.debug("event added to event_loop")
pass pass
async def login(self): async def login(self):
@ -261,17 +260,17 @@ class ChatBot(object):
try: try:
response = await self.client.login(self.password) 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)) #sync_forever_task = asyncio.create_task(self.client.sync_forever(timeout=30000, full_state=True))
except (asyncio.CancelledError, KeyboardInterrupt): except (asyncio.CancelledError, KeyboardInterrupt):
print("Received interrupt.") logger.error("Received interrupt while login.")
await self.client.close() await self.client.close()
#return sync_forever_task #return sync_forever_task
async def watch_for_sync(self, sync_event): async def watch_for_sync(self, sync_event):
print("Awaiting sync") logger.debug("Awaiting sync")
await sync_event.wait() await sync_event.wait()
print("Client is synced") logger.debug("Client is synced")
self.not_synced = False self.not_synced = False
async def read_conf2(self, section): async def read_conf2(self, section):
@ -319,9 +318,9 @@ class ChatBot(object):
filesize=file_stat.st_size, filesize=file_stat.st_size,
) )
if isinstance(resp, UploadResponse): if isinstance(resp, UploadResponse):
print("Image was uploaded successfully to server. ") logger.info("Image was uploaded successfully to server. ")
else: else:
print(f"Failed to upload image. Failure response: {resp}") logger.error(f"Failed to upload image. Failure response: {resp}")
content = { content = {
"body": os.path.basename(image), # descriptive title "body": os.path.basename(image), # descriptive title
@ -339,9 +338,9 @@ class ChatBot(object):
try: try:
await client.room_send(room_id, message_type="m.room.message", content=content) 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: except Exception:
print(f"Image send of file {image} failed.") logger.error(f"Image send of file {image} failed.")
async def main() -> None: async def main() -> None:
config.read('bot.conf') config.read('bot.conf')
@ -372,7 +371,7 @@ async def main() -> None:
await bot.read_conf2(section) await bot.read_conf2(section)
bots.append(bot) bots.append(bot)
await bot.login() await bot.login()
print("gather") #logger.info("gather")
if sys.version_info[0] == 3 and sys.version_info[1] < 11: if sys.version_info[0] == 3 and sys.version_info[1] < 11:
tasks = [] tasks = []
for bot in bots: for bot in bots:

6
matrix_pygmalion_bot/helpers.py

@ -1,4 +1,8 @@
import time import time
import logging
logger = logging.getLogger(__name__)
class Event: class Event:
def __init__(self, tick_start, tick_stop, chance, repeat, command): 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: elif (self.tick_stop == 0 or self.tick_stop == self.tick_start) and self.executed == 0:
self.execute(bot, tick) self.execute(bot, tick)
def execute(self, 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'): if self.command.startswith('printtime'):
print(time.time()//1000) print(time.time()//1000)
self.executed += 1 self.executed += 1

6
matrix_pygmalion_bot/translate.py

@ -1,9 +1,13 @@
import argostranslate.package import argostranslate.package
import argostranslate.translate import argostranslate.translate
import logging
logger = logging.getLogger(__name__)
#from_code = "de" #from_code = "de"
#to_code = "en" #to_code = "en"
def init(from_code, to_code): def init(from_code, to_code):
# Download and install Argos Translate package # Download and install Argos Translate package
argostranslate.package.update_package_index() argostranslate.package.update_package_index()
@ -20,6 +24,6 @@ def translate(text, from_code, to_code):
return text return text
# Translate # Translate
translatedText = argostranslate.translate.translate(text, from_code, to_code) translatedText = argostranslate.translate.translate(text, from_code, to_code)
print('Translated: \"{}\" -> \"{}\"'.format(text, translatedText)) logger.info('Translated: \"{}\" -> \"{}\"'.format(text, translatedText))
return translatedText return translatedText
# '¡Hola Mundo!' # '¡Hola Mundo!'

Loading…
Cancel
Save