Browse Source

cleanup

master
Hendrik Langer 2 years ago
parent
commit
658ecf48bf
  1. 2
      matrix_pygmalion_bot/ai/runpod_pygmalion.py
  2. 10
      matrix_pygmalion_bot/chatlog.py
  3. 23
      matrix_pygmalion_bot/core.py

2
matrix_pygmalion_bot/ai/runpod_pygmalion.py

@ -82,7 +82,7 @@ async def generate_sync(
reply = answer[:idx].strip() reply = answer[:idx].strip()
else: else:
reply = answer.removesuffix('<|endoftext|>').strip() reply = answer.removesuffix('<|endoftext|>').strip()
reply.replace("\n{bot_name}: ", " ") reply = reply.replace("\n{bot_name}: ", " ")
return reply return reply
else: else:
return "<ERROR>" return "<ERROR>"

10
matrix_pygmalion_bot/chatlog.py

@ -2,11 +2,12 @@ import os
import matrix_pygmalion_bot.translate as translate import matrix_pygmalion_bot.translate as translate
class ChatMessage: class ChatMessage:
def __init__(self, event_id, timestamp, user_name, is_own_message, 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):
self.event_id = event_id self.event_id = event_id
self.timestamp = timestamp self.timestamp = timestamp
self.user_name = user_name self.user_name = user_name
self.is_own_message = is_own_message self.is_own_message = is_own_message
self.is_command = is_command
self.relates_to_event = relates_to_event self.relates_to_event = relates_to_event
self.num_tokens = None self.num_tokens = None
self.message = {} self.message = {}
@ -18,6 +19,7 @@ class ChatMessage:
self.message["en"] = translate.translate(message, language, "en") self.message["en"] = translate.translate(message, language, "en")
self.language = language self.language = language
self.num_tokens = None self.num_tokens = None
def __str__(self): def __str__(self):
return str("{}: {}".format(self.user_name, self.message[self.language])) return str("{}: {}".format(self.user_name, self.message[self.language]))
def getTranslation(self, to_lang): def getTranslation(self, to_lang):
@ -43,15 +45,15 @@ class ChatHistory:
for i in range(num_items): for i in range(num_items):
event_id, item = self.chat_history.popitem() event_id, item = self.chat_history.popitem()
return item return item
def add(self, event_id, timestamp, user_name, is_own_message, relates_to_event, message, language="en", english_original_message=None): def add(self, event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language="en", english_original_message=None):
chat_message = ChatMessage(event_id, timestamp, user_name, is_own_message, relates_to_event, message, language, english_original_message) chat_message = ChatMessage(event_id, timestamp, user_name, is_own_message, is_command, relates_to_event, message, language, english_original_message)
self.chat_history[chat_message.event_id] = chat_message self.chat_history[chat_message.event_id] = chat_message
os.makedirs("./chatlogs", exist_ok=True) os.makedirs("./chatlogs", exist_ok=True)
with open("chatlogs/" + self.bot_name + "_" + self.room_name + ".txt", "a") as f: with open("chatlogs/" + self.bot_name + "_" + self.room_name + ".txt", "a") as f:
f.write("{}: {}\n".format(user_name, message)) f.write("{}: {}\n".format(user_name, message))
return chat_message return chat_message
def getLastItem(self): def getLastItem(self):
key, chat_item = reversed(self.chat_history.items())[0] key, chat_item = list(self.chat_history.items())[-1]
return chat_item return chat_item
class BotChatHistory: class BotChatHistory:

23
matrix_pygmalion_bot/core.py

@ -38,28 +38,22 @@ class Callbacks(object):
async def message_cb(self, room: MatrixRoom, event: RoomMessageText) -> None: async def message_cb(self, room: MatrixRoom, event: RoomMessageText) -> None:
if not hasattr(event, 'body'): if not hasattr(event, 'body'):
return return
#message = event.body
is_own_message = False
if event.sender == self.client.user:
is_own_message = True
is_command = False
if event.body.startswith('!'):
is_command = True
if not (self.bot.owner is None):
if not (event.sender == self.bot.owner or is_own_message):
return
relates_to = None relates_to = None
if 'm.relates_to' in event.source["content"]: if 'm.relates_to' in event.source["content"]:
relates_to = event.source["content"]['m.relates_to']["event_id"] relates_to = event.source["content"]['m.relates_to']["event_id"]
is_command = False
if event.body.startswith('!'):
is_command = True
language = "en" language = "en"
if not (self.bot.translate is None) and not is_command: if not (self.bot.translate is None) and not is_command:
language = self.bot.translate language = self.bot.translate
if 'original_message' in event.source["content"]: if 'original_message' in event.source["content"]:
english_original_message = event.source["content"]['original_message'] english_original_message = event.source["content"]['original_message']
else: else:
english_original_message = None english_original_message = None
chat_message = self.bot.chat_history.room(room.display_name).add(event.event_id, event.server_timestamp, room.user_name(event.sender), is_own_message, 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( print(
@ -70,10 +64,11 @@ class Callbacks(object):
await self.client.room_read_markers(room.room_id, event.event_id, event.event_id) await self.client.room_read_markers(room.room_id, event.event_id, event.event_id)
# Ignore messages from ourselves # Ignore messages from ourselves
if is_own_message: if chat_message.is_own_message:
return return
# Ignore message from strangers
if not hasattr(event, 'body'): if not (self.bot.owner is None):
if not (event.sender == self.bot.owner or chat_message.is_own_message):
return return
if event.body.startswith('!replybot'): if event.body.startswith('!replybot'):

Loading…
Cancel
Save