From c1af34ba0f37dd972557349be7865c4aa15c0b03 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sun, 4 Aug 2024 21:16:25 +0100 Subject: [PATCH 1/6] ability to summarise a certain number of messages in a channel using openai magic --- cogs/commands/summarise.py | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 cogs/commands/summarise.py diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py new file mode 100644 index 0000000..594f79e --- /dev/null +++ b/cogs/commands/summarise.py @@ -0,0 +1,90 @@ +from discord.ext import commands +import logging +from typing import Optional + +from discord.ext.commands import Bot, Context, clean_content +from cogs.commands.openaiadmin import is_author_banned_openai +from discord import AllowedMentions + +from config import CONFIG +from discord import AllowedMentions +from utils.utils import get_name_and_content, split_into_messages + +import openai + +LONG_HELP_TEXT = """ +Too much yapping? Summarise what people have said using the power of the GPT overlords! +""" + +SHORT_HELP_TEXT = """Summarise messages.""" + +mentions = AllowedMentions(everyone=False, users=False, roles=False, replied_user=True) +chat_cmd = CONFIG.PREFIX + "chat" +summarise_cmd = CONFIG.PREFIX + "summarise" + + +def clean(msg, *prefixes): + for pre in prefixes: + msg = msg.strip().removeprefix(pre) + return msg.strip() + +class Summarise(commands.Cog): + def __init__(self, bot: Bot): + self.bot = bot + openai.api_key = CONFIG.OPENAI_API_KEY + self.model = "gpt-3.5-turbo" + 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:" + + @commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) + async def tldr(self, ctx: Context, number_of_messages: int=100, gpt4: 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): + 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) + + # send the prompt to the ai overlords to process + async with ctx.typing(): + response = await self.dispatch_api(messages, gpt4) + if response: + prev = ctx.message + for content in split_into_messages(response): + prev = await prev.reply(content, allowed_mentions=mentions) + + + async def dispatch_api(self, messages, gpt4) -> Optional[str]: + logging.info(f"Making OpenAI request: {messages}") + + # Make request + model = "gpt-4" if gpt4 else self.model + response = await openai.ChatCompletion.acreate(model=model, messages=messages) + logging.info(f"OpenAI Response: {response}") + + # Remove prefix that chatgpt might add + reply = response.choices[0].message.content + if CONFIG.AI_INCLUDE_NAMES: + name = f"{self.bot.user.display_name}: " + reply = clean(reply, "Apollo: ", "apollo: ", name) + return reply + + + async def create_message(self, message_chain): + # get initial prompt + initial = self.system_prompt + "\n" + + # for each message, append it to the prompt as follows --- author : message \n + async for msg in message_chain: + if CONFIG.AI_INCLUDE_NAMES and msg.author != self.bot.user: + initial+= msg.author.name + ":" + msg.content + "\n" + + messages = [dict(role="system", content=initial)] + + return messages + +async def setup(bot: Bot): + await bot.add_cog(Summarise(bot)) From 87a39476500d6e0202ccd1e6826385ab6f5e6f41 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sun, 4 Aug 2024 21:19:14 +0100 Subject: [PATCH 2/6] ruff ruff --- cogs/commands/summarise.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index 594f79e..3af6992 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -1,16 +1,14 @@ -from discord.ext import commands import logging from typing import Optional -from discord.ext.commands import Bot, Context, clean_content -from cogs.commands.openaiadmin import is_author_banned_openai +import openai from discord import AllowedMentions +from discord.ext import commands +from discord.ext.commands import Bot, Context +from cogs.commands.openaiadmin import is_author_banned_openai from config import CONFIG -from discord import AllowedMentions -from utils.utils import get_name_and_content, split_into_messages - -import openai +from utils.utils import split_into_messages LONG_HELP_TEXT = """ Too much yapping? Summarise what people have said using the power of the GPT overlords! @@ -28,6 +26,7 @@ def clean(msg, *prefixes): msg = msg.strip().removeprefix(pre) return msg.strip() + class Summarise(commands.Cog): def __init__(self, bot: Bot): self.bot = bot @@ -36,14 +35,16 @@ def __init__(self, bot: Bot): 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:" @commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) - async def tldr(self, ctx: Context, number_of_messages: int=100, gpt4: bool=False ): + async def tldr( + self, ctx: Context, number_of_messages: int = 100, gpt4: 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): return - # get the last "number_of_messages" messages from the current channel and build the prompt + # 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) @@ -56,7 +57,6 @@ async def tldr(self, ctx: Context, number_of_messages: int=100, gpt4: bool=False for content in split_into_messages(response): prev = await prev.reply(content, allowed_mentions=mentions) - async def dispatch_api(self, messages, gpt4) -> Optional[str]: logging.info(f"Making OpenAI request: {messages}") @@ -72,19 +72,19 @@ async def dispatch_api(self, messages, gpt4) -> Optional[str]: reply = clean(reply, "Apollo: ", "apollo: ", name) return reply - async def create_message(self, message_chain): # get initial prompt initial = self.system_prompt + "\n" - + # for each message, append it to the prompt as follows --- author : message \n async for msg in message_chain: if CONFIG.AI_INCLUDE_NAMES and msg.author != self.bot.user: - initial+= msg.author.name + ":" + msg.content + "\n" + initial += msg.author.name + ":" + msg.content + "\n" messages = [dict(role="system", content=initial)] return messages + async def setup(bot: Bot): await bot.add_cog(Summarise(bot)) From 4f5b3c51479dbf94e800d141f0615100f69e49c1 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sun, 4 Aug 2024 21:23:15 +0100 Subject: [PATCH 3/6] command added to list of commands --- apollo.py | 65 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/apollo.py b/apollo.py index fc6ee9d..122b90a 100644 --- a/apollo.py +++ b/apollo.py @@ -19,38 +19,39 @@ # The command extensions to be loaded by the bot EXTENSIONS = [ - "cogs.commands.announce", - "cogs.commands.birthday", - "cogs.commands.counting", - "cogs.commands.chatgpt", - "cogs.commands.dalle", - "cogs.commands.date", - "cogs.commands.event_sync", - "cogs.commands.flip", - "cogs.commands.karma_admin", - "cogs.commands.karma_blacklist", - "cogs.commands.karma", - "cogs.commands.lcalc", - "cogs.commands.misc", - "cogs.commands.onmessage", - "cogs.commands.openaiadmin", - "cogs.commands.quotes", - "cogs.commands.rolemenu", - "cogs.commands.reminders", - "cogs.commands.roll", - "cogs.commands.run", - "cogs.commands.roomsearch", - "cogs.commands.say", - "cogs.commands.system", - "cogs.commands.tex", - "cogs.commands.vote", - "cogs.commands.widen", - "cogs.commands.xkcd", - "cogs.channel_checker", - "cogs.database", - "cogs.irc", - "cogs.parallelism", - "cogs.welcome", + "cogs.commands.announce", + "cogs.commands.birthday", + "cogs.commands.counting", + "cogs.commands.chatgpt", + "cogs.commands.dalle", + "cogs.commands.date", + "cogs.commands.event_sync", + "cogs.commands.flip", + "cogs.commands.karma_admin", + "cogs.commands.karma_blacklist", + "cogs.commands.karma", + "cogs.commands.lcalc", + "cogs.commands.misc", + "cogs.commands.onmessage", + "cogs.commands.openaiadmin", + "cogs.commands.quotes", + "cogs.commands.rolemenu", + "cogs.commands.reminders", + "cogs.commands.roll", + "cogs.commands.run", + "cogs.commands.roomsearch", + "cogs.commands.say", + "cogs.commands.summarise", + "cogs.commands.system", + "cogs.commands.tex", + "cogs.commands.vote", + "cogs.commands.widen", + "cogs.commands.xkcd", + "cogs.channel_checker", + "cogs.database", + "cogs.irc", + "cogs.parallelism", + "cogs.welcome", ] From f02c0111dc9c437f44c0050c3ba1e969e1b5ac1b Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sat, 10 Aug 2024 12:51:02 +0100 Subject: [PATCH 4/6] cog added --- apollo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/apollo.py b/apollo.py index 122b90a..67a731a 100644 --- a/apollo.py +++ b/apollo.py @@ -25,6 +25,7 @@ "cogs.commands.chatgpt", "cogs.commands.dalle", "cogs.commands.date", + "cogs.commands.eightball", "cogs.commands.event_sync", "cogs.commands.flip", "cogs.commands.karma_admin", From fbfbcb6e0c2ed211c1beac08e130f0d33f4fe5c3 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sat, 10 Aug 2024 12:59:11 +0100 Subject: [PATCH 5/6] ruff changes --- apollo.py | 68 +++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/apollo.py b/apollo.py index 67a731a..3b84d54 100644 --- a/apollo.py +++ b/apollo.py @@ -19,40 +19,40 @@ # The command extensions to be loaded by the bot EXTENSIONS = [ - "cogs.commands.announce", - "cogs.commands.birthday", - "cogs.commands.counting", - "cogs.commands.chatgpt", - "cogs.commands.dalle", - "cogs.commands.date", - "cogs.commands.eightball", - "cogs.commands.event_sync", - "cogs.commands.flip", - "cogs.commands.karma_admin", - "cogs.commands.karma_blacklist", - "cogs.commands.karma", - "cogs.commands.lcalc", - "cogs.commands.misc", - "cogs.commands.onmessage", - "cogs.commands.openaiadmin", - "cogs.commands.quotes", - "cogs.commands.rolemenu", - "cogs.commands.reminders", - "cogs.commands.roll", - "cogs.commands.run", - "cogs.commands.roomsearch", - "cogs.commands.say", - "cogs.commands.summarise", - "cogs.commands.system", - "cogs.commands.tex", - "cogs.commands.vote", - "cogs.commands.widen", - "cogs.commands.xkcd", - "cogs.channel_checker", - "cogs.database", - "cogs.irc", - "cogs.parallelism", - "cogs.welcome", + "cogs.commands.announce", + "cogs.commands.birthday", + "cogs.commands.counting", + "cogs.commands.chatgpt", + "cogs.commands.dalle", + "cogs.commands.date", + "cogs.commands.eightball", + "cogs.commands.event_sync", + "cogs.commands.flip", + "cogs.commands.karma_admin", + "cogs.commands.karma_blacklist", + "cogs.commands.karma", + "cogs.commands.lcalc", + "cogs.commands.misc", + "cogs.commands.onmessage", + "cogs.commands.openaiadmin", + "cogs.commands.quotes", + "cogs.commands.rolemenu", + "cogs.commands.reminders", + "cogs.commands.roll", + "cogs.commands.run", + "cogs.commands.roomsearch", + "cogs.commands.say", + "cogs.commands.summarise", + "cogs.commands.system", + "cogs.commands.tex", + "cogs.commands.vote", + "cogs.commands.widen", + "cogs.commands.xkcd", + "cogs.channel_checker", + "cogs.database", + "cogs.irc", + "cogs.parallelism", + "cogs.welcome", ] From cfd9100d2caa4616d330d5dbab07154ed5591661 Mon Sep 17 00:00:00 2001 From: Ali Aljishi Date: Sat, 10 Aug 2024 17:42:20 +0100 Subject: [PATCH 6/6] changed model to gpt4o, removed unused commands, openai ban response --- cogs/commands/summarise.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cogs/commands/summarise.py b/cogs/commands/summarise.py index 3af6992..efd30f2 100644 --- a/cogs/commands/summarise.py +++ b/cogs/commands/summarise.py @@ -17,9 +17,7 @@ SHORT_HELP_TEXT = """Summarise messages.""" mentions = AllowedMentions(everyone=False, users=False, roles=False, replied_user=True) -chat_cmd = CONFIG.PREFIX + "chat" -summarise_cmd = CONFIG.PREFIX + "summarise" - +model = "gpt-4o-mini" def clean(msg, *prefixes): for pre in prefixes: @@ -31,17 +29,16 @@ class Summarise(commands.Cog): def __init__(self, bot: Bot): self.bot = bot openai.api_key = CONFIG.OPENAI_API_KEY - self.model = "gpt-3.5-turbo" 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:" @commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT) async def tldr( - self, ctx: Context, number_of_messages: int = 100, gpt4: bool = False - ): + self, ctx: Context, number_of_messages: int = 100): 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 @@ -51,17 +48,16 @@ async def tldr( # send the prompt to the ai overlords to process async with ctx.typing(): - response = await self.dispatch_api(messages, gpt4) + response = await self.dispatch_api(messages) if response: prev = ctx.message for content in split_into_messages(response): prev = await prev.reply(content, allowed_mentions=mentions) - async def dispatch_api(self, messages, gpt4) -> Optional[str]: + async def dispatch_api(self, messages) -> Optional[str]: logging.info(f"Making OpenAI request: {messages}") # Make request - model = "gpt-4" if gpt4 else self.model response = await openai.ChatCompletion.acreate(model=model, messages=messages) logging.info(f"OpenAI Response: {response}")