Fix reaction handling.

This commit is contained in:
OK 2024-03-17 13:25:02 +01:00
parent 73d9b9184d
commit 1bff1c9719
4 changed files with 20 additions and 17 deletions

View File

@ -248,7 +248,7 @@ class AIResponder(AIResponderBase):
async def fix(self, answer: str) -> str: async def fix(self, answer: str) -> str:
raise NotImplementedError() raise NotImplementedError()
async def memory_rewrite(self, memory: str, user: str, question: str, answer: str) -> str: async def memory_rewrite(self, memory: str, message_user: str, answer_user: str, question: str, answer: str) -> str:
raise NotImplementedError() raise NotImplementedError()
async def translate(self, text: str, language: str = "english") -> str: async def translate(self, text: str, language: str = "english") -> str:
@ -292,13 +292,14 @@ class AIResponder(AIResponderBase):
response["picture"] = await self.translate(response["picture"]) response["picture"] = await self.translate(response["picture"])
return True return True
async def memoize(self, user: str, message: str, answer: str) -> None: async def memoize(self, message_user: str, answer_user: str, message: str, answer: str) -> None:
self.memory = await self.memory_rewrite(self.memory, user, message, answer) self.memory = await self.memory_rewrite(self.memory, message_user, answer_user, message, answer)
self.update_memory(self.memory) self.update_memory(self.memory)
async def memoize_reaction(self, message_user: str, reaction_user: str, operation: str, reaction: str, message: str) -> None: async def memoize_reaction(self, message_user: str, reaction_user: str, operation: str, reaction: str, message: str) -> None:
quoted_message = message.replace('\n', '\n> ') quoted_message = message.replace('\n', '\n> ')
await self.memoize(reaction_user, f'{message_user}:\n> {quoted_message}', await self.memoize(message_user, reaction_user,
f'> {quoted_message}',
f'User {reaction_user} has {operation} this raction: {reaction}') f'User {reaction_user} has {operation} this raction: {reaction}')
async def send(self, message: AIMessage) -> AIResponse: async def send(self, message: AIMessage) -> AIResponse:
@ -352,7 +353,7 @@ class AIResponder(AIResponderBase):
# Update memory # Update memory
if answer_message.answer is not None: if answer_message.answer is not None:
await self.memoize(message.user, message.message, answer_message.answer) await self.memoize(message.user, 'assistant', message.message, answer_message.answer)
# Return the updated answer message # Return the updated answer message
return answer_message return answer_message

View File

@ -32,6 +32,7 @@ class FjerkroaBot(commands.Bot):
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
intents.members = True intents.members = True
intents.reactions = True
self._re_user = re.compile(r"[<][@][!]?\s*([0-9]+)[>]") self._re_user = re.compile(r"[<][@][!]?\s*([0-9]+)[>]")
self.init_observer() self.init_observer()
@ -108,21 +109,21 @@ class FjerkroaBot(commands.Bot):
return return
await self.handle_message_through_responder(message) await self.handle_message_through_responder(message)
async def on_reaction_add(self, reaction, user): async def on_reaction_operation(self, reaction, user, operation):
if user.bot: logging.info(f'{operation} reaction {reaction} by {user}.')
return
airesponder = self.get_ai_responder(self.get_channel_name(reaction.message.channel)) airesponder = self.get_ai_responder(self.get_channel_name(reaction.message.channel))
message = str(reaction.message.content) if reaction.message.content else '' message = str(reaction.message.content) if reaction.message.content else ''
if len(message) > 1: if len(message) > 1:
await airesponder.memoize_reaction(reaction.message.user.name, user.name, 'adding', str(reaction.emoji), message) await airesponder.memoize_reaction(reaction.message.author.name, user.name, operation, str(reaction.emoji), message)
async def on_reaction_add(self, reaction, user):
await self.on_reaction_operation(reaction, user, 'adding')
async def on_reaction_remove(self, reaction, user): async def on_reaction_remove(self, reaction, user):
if user.bot: await self.on_reaction_operation(reaction, user, 'removing')
return
airesponder = self.get_ai_responder(self.get_channel_name(reaction.message.channel)) async def on_reaction_clear(self, reaction, user):
message = str(reaction.message.content) if reaction.message.content else '' await self.on_reaction_operation(reaction, user, 'clearing')
if len(message) > 1:
await airesponder.memoize_reaction(reaction.message.user.name, user.name, 'removing', str(reaction.emoji), message)
def on_config_file_modified(self, event): def on_config_file_modified(self, event):
if event.src_path == self.config_file: if event.src_path == self.config_file:

View File

@ -111,15 +111,16 @@ class OpenAIResponder(AIResponder, LeonardoAIDrawMixIn):
logging.warning(f"failed to translate the text: {repr(err)}") logging.warning(f"failed to translate the text: {repr(err)}")
return text return text
async def memory_rewrite(self, memory: str, user: str, question: str, answer: str) -> str: async def memory_rewrite(self, memory: str, message_user: str, answer_user: str, question: str, answer: str) -> str:
if 'memory-model' not in self.config: if 'memory-model' not in self.config:
return memory return memory
messages = [{'role': 'system', 'content': self.config.get('memory-system', 'You are an memory assistant.')}, messages = [{'role': 'system', 'content': self.config.get('memory-system', 'You are an memory assistant.')},
{'role': 'user', 'content': f'Here is my previous memory:\n```\n{memory}\n```\n\n' {'role': 'user', 'content': f'Here is my previous memory:\n```\n{memory}\n```\n\n'
f'Here is my conversanion:\n```\n{user}: {question}\n\nassistant: {answer}\n```\n\n' f'Here is my conversanion:\n```\n{message_user}: {question}\n\n{answer_user}: {answer}\n```\n\n'
f'Please rewrite the memory in a way, that it contain the content mentioned in conversation. ' f'Please rewrite the memory in a way, that it contain the content mentioned in conversation. '
f'The whole memory should not be too long, summarize if required. ' f'The whole memory should not be too long, summarize if required. '
f'Write just new memory data without any comments.'}] f'Write just new memory data without any comments.'}]
logging.info(f'Rewrite memory:\n{pp(messages)}')
try: try:
# logging.info(f'send this memory request:\n{pp(messages)}') # logging.info(f'send this memory request:\n{pp(messages)}')
result = await openai_chat(self.client, result = await openai_chat(self.client,

Binary file not shown.