From 87796af446297539e7e136bb0c73986ebf4259bb Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sun, 11 Aug 2024 14:16:49 +0100 Subject: [PATCH 1/2] added cooldown period, and semi modifiable prompt --- cogs/commands/summarise.py | 26 +++++++++++++++++++------- config.example.yaml | 4 ++++ config/config.py | 2 ++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index efd30f2..ff8ccf2 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -19,6 +19,8 @@ mentions = AllowedMentions(everyone=False, users=False, roles=False, replied_user=True) model = "gpt-4o-mini" + + def clean(msg, *prefixes): for pre in prefixes: msg = msg.strip().removeprefix(pre) @@ -31,20 +33,30 @@ def __init__(self, bot: Bot): openai.api_key = CONFIG.OPENAI_API_KEY self.system_prompt = "People yap too much, I don't want to read all of it. In 200 words or less give me the gist of what is being said. Note that the messages are in reverse chronological order:" + def build_prompt(self, response_word_limit, bullet_points, channel_name): + + response_word_limit = 500 if response_word_limit > 500 else response_word_limit + 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 {response_word_limit} 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 + + @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): + self, ctx: Context, number_of_messages: int = 100, response_word_limit: int = 100, bullet_point_output: bool = False ): number_of_messages = 400 if number_of_messages > 400 else number_of_messages - + + # avoid banned users if not await is_author_banned_openai(ctx): await ctx.send("You are banned from OpenAI!") return # get the last "number_of_messages" messages from the current channel and build the prompt - curr_channel = ctx.guild.get_channel(ctx.channel.id) - messages = curr_channel.history(limit=number_of_messages) - messages = await self.create_message(messages) + prompt = self.build_prompt(response_word_limit, bullet_point_output, ctx.channel) + messages = ctx.channel.history(limit=number_of_messages) + messages = await self.create_message(messages, prompt) # send the prompt to the ai overlords to process async with ctx.typing(): @@ -68,9 +80,9 @@ async def dispatch_api(self, messages) -> Optional[str]: reply = clean(reply, "Apollo: ", "apollo: ", name) return reply - async def create_message(self, message_chain): + async def create_message(self, message_chain, prompt): # get initial prompt - initial = self.system_prompt + "\n" + initial = prompt + "\n" # for each message, append it to the prompt as follows --- author : message \n async for msg in message_chain: diff --git a/config.example.yaml b/config.example.yaml index fa2ef17..8b3d6fc 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -35,6 +35,10 @@ config: portainer_api_key: portainer # Liege Chancellor User ID liege_chancellor_id: 1234 + # Summarise Use Limit + summarise_limit: 3 + # Summarise Cooldown Period (minutes) + summarise_cooldown: 10 # Configuration # Level of messages logged to file diff --git a/config/config.py b/config/config.py index e0d655a..ee91532 100644 --- a/config/config.py +++ b/config/config.py @@ -27,6 +27,8 @@ def __init__(self, filepath: str): self.AI_SYSTEM_PROMPT: str = parsed.get("ai_system_prompt") self.PORTAINER_API_KEY: str = parsed.get("portainer_api_key") 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") # Configuration self.LOG_LEVEL: str = parsed.get("log_level") From 58b107845848293d27018b49b8765a8d8f88ac8c Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sun, 11 Aug 2024 17:13:27 +0100 Subject: [PATCH 2/2] updated prompt to reduce response length --- cogs/commands/summarise.py | 10 ++++------ config/config.py | 3 --- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index ff8ccf2..291c49d 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -31,20 +31,18 @@ class Summarise(commands.Cog): def __init__(self, bot: Bot): self.bot = bot openai.api_key = CONFIG.OPENAI_API_KEY - self.system_prompt = "People yap too much, I don't want to read all of it. In 200 words or less give me the gist of what is being said. Note that the messages are in reverse chronological order:" - def build_prompt(self, response_word_limit, bullet_points, channel_name): + def build_prompt(self, bullet_points, channel_name): - response_word_limit = 500 if response_word_limit > 500 else response_word_limit 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 {response_word_limit} words or less give me the gist of what is being said. {bullet_points} Note that the messages are in reverse chronological order: + 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, response_word_limit: int = 100, bullet_point_output: bool = False ): + 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 @@ -54,7 +52,7 @@ async def tldr( return # get the last "number_of_messages" messages from the current channel and build the prompt - prompt = self.build_prompt(response_word_limit, bullet_point_output, ctx.channel) + prompt = self.build_prompt(bullet_point_output, ctx.channel) messages = ctx.channel.history(limit=number_of_messages) messages = await self.create_message(messages, prompt) diff --git a/config/config.py b/config/config.py index 5cdc808..64cc34a 100644 --- a/config/config.py +++ b/config/config.py @@ -27,12 +27,9 @@ def __init__(self, filepath: str): self.AI_SYSTEM_PROMPT: str = parsed.get("ai_system_prompt") self.PORTAINER_API_KEY: str = parsed.get("portainer_api_key") self.LIEGE_CHANCELLOR_ID: int = parsed.get("liege_chancellor_id") -<<<<<<< HEAD self.SUMMARISE_LIMIT: int = parsed.get("summarise_limit") self.SUMMARISE_COOLDOWN: int = parsed.get("summarise_cooldown") -======= self.MARKOV_ENABLED: bool = parsed.get("markov_enabled") ->>>>>>> master # Configuration self.LOG_LEVEL: str = parsed.get("log_level")