From 54eb3fa6fe9defa107f5392fd267708e5203bc42 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Thu, 22 Aug 2024 15:47:00 +0100 Subject: [PATCH 1/4] variable response size using sigmoid function --- cogs/commands/summarise.py | 38 +++++++++++++++++++++++++++----------- config.example.yaml | 6 ++++-- config/config.py | 1 + 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index 291c49db..e2a4e58d 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -8,6 +8,7 @@ from cogs.commands.openaiadmin import is_author_banned_openai from config import CONFIG +from math import exp, ceil from utils.utils import split_into_messages LONG_HELP_TEXT = """ @@ -19,6 +20,10 @@ mentions = AllowedMentions(everyone=False, users=False, roles=False, replied_user=True) model = "gpt-4o-mini" +# weights/coefficients for sigmoid function +a = 750 +b = 7 +c = 400 def clean(msg, *prefixes): @@ -32,19 +37,18 @@ def __init__(self, bot: Bot): self.bot = bot openai.api_key = CONFIG.OPENAI_API_KEY - def build_prompt(self, bullet_points, channel_name): - bullet_points = "Put it in bullet points for readability." if bullet_points else "" - prompt = f"""People yap too much, I don't want to read all of it. The topic is something related to {channel_name}. In 2 sentences or less give me the gist of what is being said. {bullet_points} Note that the messages are in reverse chronological order: - """ - return prompt + + + @commands.cooldown(CONFIG.SUMMARISE_LIMIT, CONFIG.SUMMARISE_COOLDOWN * 60, commands.BucketType.channel) @commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) async def tldr( self, ctx: Context, number_of_messages: int = 100, bullet_point_output: bool = False ): - number_of_messages = 400 if number_of_messages > 400 else number_of_messages + + number_of_messages = CONFIG.SUMMARISE_MESSAGE_LIMIT if number_of_messages > CONFIG.SUMMARISE_MESSAGE_LIMIT else number_of_messages # avoid banned users if not await is_author_banned_openai(ctx): @@ -52,9 +56,10 @@ async def tldr( return # get the last "number_of_messages" messages from the current channel and build the prompt - prompt = self.build_prompt(bullet_point_output, ctx.channel) + prompt = self.build_prompt(bullet_point_output, ctx.channel, self.sigmoid(number_of_messages)) + messages = ctx.channel.history(limit=number_of_messages) - messages = await self.create_message(messages, prompt) + messages = await self.create_message(messages, prompt, ctx) # send the prompt to the ai overlords to process async with ctx.typing(): @@ -78,19 +83,30 @@ async def dispatch_api(self, messages) -> Optional[str]: reply = clean(reply, "Apollo: ", "apollo: ", name) return reply - async def create_message(self, message_chain, prompt): + async def create_message(self, message_chain, prompt, ctx): # get initial prompt initial = prompt + "\n" # for each message, append it to the prompt as follows --- author : message \n + message_length = 0 async for msg in message_chain: if CONFIG.AI_INCLUDE_NAMES and msg.author != self.bot.user: - initial += msg.author.name + ":" + msg.content + "\n" - + message_length += len(msg.content.split()) + initial += msg.author.name + ": " + msg.content + "\n" messages = [dict(role="system", content=initial)] return messages + def build_prompt(self, bullet_points, channel_name, response_size): + + bullet_points = "Put it in bullet points for readability." if bullet_points else "" + prompt = f"""People yap too much, I don't want to read all of it. The topic is related to {channel_name}. In {response_size} words or less give me the gist of what is being said. {bullet_points} Note that the messages are in reverse chronological order: + """ + return prompt + + def sigmoid(self, x): + return int(ceil(c / (1 + b * exp((-x)/ a)))) + async def setup(bot: Bot): await bot.add_cog(Summarise(bot)) diff --git a/config.example.yaml b/config.example.yaml index 9b2e9060..3dc4c5ec 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -35,10 +35,12 @@ config: portainer_api_key: portainer # Liege Chancellor User ID liege_chancellor_id: 1234 - # Summarise Use Limit + # Summarise/TLDR Command Usage Limit summarise_limit: 3 - # Summarise Cooldown Period (minutes) + # Summarise/TLDR Command Cooldown Period (minutes) summarise_cooldown: 10 + # Summarise/TLDR Command Message Limit + summarise_message_limit : 400 # whether to load general.txt and markov chains markov_enabled: False diff --git a/config/config.py b/config/config.py index 64cc34ab..6ccd225f 100644 --- a/config/config.py +++ b/config/config.py @@ -29,6 +29,7 @@ def __init__(self, filepath: str): self.LIEGE_CHANCELLOR_ID: int = parsed.get("liege_chancellor_id") self.SUMMARISE_LIMIT: int = parsed.get("summarise_limit") self.SUMMARISE_COOLDOWN: int = parsed.get("summarise_cooldown") + self.SUMMARISE_MESSAGE_LIMIT: int = parsed.get("summarise_message_limit") self.MARKOV_ENABLED: bool = parsed.get("markov_enabled") # Configuration From 4d31b6608d1c28da1569064f3aa6f7b8fcb376f2 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Thu, 22 Aug 2024 21:55:21 +0100 Subject: [PATCH 2/4] custom error messages --- cogs/commands/summarise.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index e2a4e58d..a7269911 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -1,5 +1,6 @@ import logging from typing import Optional +from datetime import datetime, timedelta, timezone import openai from discord import AllowedMentions @@ -35,18 +36,17 @@ def clean(msg, *prefixes): class Summarise(commands.Cog): def __init__(self, bot: Bot): self.bot = bot + self.cooldowns = {} openai.api_key = CONFIG.OPENAI_API_KEY - - - @commands.cooldown(CONFIG.SUMMARISE_LIMIT, CONFIG.SUMMARISE_COOLDOWN * 60, commands.BucketType.channel) @commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) async def tldr( self, ctx: Context, number_of_messages: int = 100, bullet_point_output: bool = False ): - + if await self.in_cooldown(ctx): + return number_of_messages = CONFIG.SUMMARISE_MESSAGE_LIMIT if number_of_messages > CONFIG.SUMMARISE_MESSAGE_LIMIT else number_of_messages @@ -69,6 +69,28 @@ async def tldr( for content in split_into_messages(response): prev = await prev.reply(content, allowed_mentions=mentions) + + async def in_cooldown(self, ctx): + now = datetime.now(timezone.utc) + # channel based cooldown + if self.cooldowns.get(ctx.channel.id): + # check that message limit hasn't been reached + if CONFIG.SUMMARISE_LIMIT <= self.cooldowns[ctx.channel.id][1]: + + message_time = self.cooldowns[ctx.channel.id][0] + cutoff = message_time + timedelta(minutes=CONFIG.SUMMARISE_COOLDOWN) + # check that message time + cooldown time period is still in the future + if now < cutoff: + await ctx.reply("STFU!! Wait " + str(int((cutoff - now).total_seconds())) + " Seconds. You are on Cool Down." ) + return True + else: + self.cooldowns[ctx.channel.id] = [now, 1] # reset the cooldown + else: + self.cooldowns[ctx.channel.id][1]+=1 + else: + self.cooldowns[ctx.channel.id] = [now, 1] + return False + async def dispatch_api(self, messages) -> Optional[str]: logging.info(f"Making OpenAI request: {messages}") From 53a8ac6341cdc4c9ab87970c13ca2e36967d7d90 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Thu, 22 Aug 2024 22:03:39 +0100 Subject: [PATCH 3/4] removed print statement --- cogs/commands/summarise.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index 485d187b..d1974ae0 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -134,7 +134,6 @@ def build_prompt(self, bullet_points, channel_name, response_size): bullet_points = "Put it in bullet points for readability." if bullet_points else "" prompt = f"""People yap too much, I don't want to read all of it. The topic is related to {channel_name}. In {response_size} words or less give me the gist of what is being said. {bullet_points} Note that the messages are in reverse chronological order: """ - print(prompt) return prompt def sigmoid(self, x): From 8434b6522eebc59d5f93b8859d51508cb3f0eaa9 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Thu, 22 Aug 2024 22:04:05 +0100 Subject: [PATCH 4/4] ruff check --- cogs/commands/summarise.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index d1974ae0..ca060a7f 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -1,7 +1,8 @@ import contextlib import logging -from typing import Optional from datetime import datetime, timedelta, timezone +from math import ceil, exp +from typing import Optional import openai from discord import AllowedMentions @@ -10,7 +11,6 @@ from cogs.commands.openaiadmin import is_author_banned_openai from config import CONFIG -from math import exp, ceil from utils.utils import split_into_messages LONG_HELP_TEXT = """