From 361ed11bfa94f5d3df85ee9b14b56b878910206b Mon Sep 17 00:00:00 2001 From: Brett Anda Date: Sun, 5 May 2024 22:48:55 -0600 Subject: [PATCH] fixed the welcome ai messages --- cogs/chat.py | 9 ++++----- cogs/welcome.py | 17 +++++++---------- index.py | 5 +++++ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/cogs/chat.py b/cogs/chat.py index 59cdd617..1e562cb6 100644 --- a/cogs/chat.py +++ b/cogs/chat.py @@ -32,8 +32,6 @@ log = logging.getLogger(__name__) -openai = AsyncOpenAI(api_key=os.environ["OPENAI"]) - POSSIBLE_SENSITIVE_MESSAGE = "*Possibly sensitive:* ||" POSSIBLE_OFFENSIVE_MESSAGE = "**I failed to respond because my message might have been offensive, please choose another topic or try again**" @@ -347,6 +345,7 @@ class Chat(commands.Cog): def __init__(self, bot: Friday): self.bot: Friday = bot + self.openai = AsyncOpenAI(api_key=os.environ["OPENAI"]) # https://help.openai.com/en/articles/5008629-can-i-use-concurrent-api-calls self.api_lock = asyncio.Semaphore(2) # bot.openai_api_lock @@ -592,7 +591,7 @@ async def content_filter_flagged(self, text: str) -> tuple[bool, list[str]]: if self.bot.testing: return False, [] async with self.api_lock: - response = await openai.moderations.create( + response = await self.openai.moderations.create( model="text-moderation-latest", input=text, ) @@ -621,14 +620,14 @@ async def openai_req( async with self.api_lock: if _continue: log.info(messages) - response = await openai.chat.completions.create(model=PremiumPerks(current_tier).model, + response = await self.openai.chat.completions.create(model=PremiumPerks(current_tier).model, messages=messages, temperature=0, max_tokens=PremiumPerks(current_tier).max_chat_tokens, user=str(msg.channel.id)) else: log.info(messages + [{'role': 'user', 'content': f"{content}"}]) - response = await openai.chat.completions.create(model=PremiumPerks(current_tier).model, + response = await self.openai.chat.completions.create(model=PremiumPerks(current_tier).model, messages=messages + [{'role': 'user', 'content': f"{content}"}], max_tokens=PremiumPerks(current_tier).max_chat_tokens, user=str(msg.channel.id)) diff --git a/cogs/welcome.py b/cogs/welcome.py index a15eb847..601a8c2d 100644 --- a/cogs/welcome.py +++ b/cogs/welcome.py @@ -109,25 +109,22 @@ async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel): log.info(f"removed welcome channel for {channel.guild} (ID:{channel.guild.id})") async def send_ai_welcome_message(self, config: Config, member: discord.Member) -> None: - chat: Chat = self.bot.get_cog("Chat") # type: ignore - if chat is None: + if self.bot.chat is None: log.error("Chat cog not loaded") return - async with chat.api_lock: - response = await self.bot.loop.run_in_executor( - None, - functools.partial( - lambda: openai.ChatCompletion.create( + async with self.bot.chat.api_lock: + response = await self.bot.chat.openai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {'role': 'user', 'content': f"You're WelcomeGPT. You welcome new users to the Discord server '{member.guild.name}' with at very short and unique messages with a joke about their name and include their user mention as their name at least once. Don't include quotation marks.\n Now welcome, {member.display_name} to the server, with the user mention {member.mention}."}, ], max_tokens=75, - user=str(member.id), - ))) + user=str(member.id) + ) if response is None: return None - message = response.get("choices")[0]["message"]["content"] # type: ignore + message = response.choices[0].message.content + assert message is not None if member.mention not in message: message = member.mention + " " + message if config.channel: diff --git a/index.py b/index.py index 7d5f24d6..6feb57b4 100755 --- a/index.py +++ b/index.py @@ -39,6 +39,7 @@ from .cogs.patreons import Patreons from .cogs.reminder import Reminder from .cogs.sharding import Event + from .cogs.chat import Chat load_dotenv() @@ -361,6 +362,10 @@ def patreon(self) -> Optional[Patreons]: def support(self) -> Optional[Support]: return self.get_cog("Support") # type: ignore + @property + def chat(self) -> Optional[Chat]: + return self.get_cog("Chat") # type: ignore + # @property # def sharding(self) -> Optional[Sharding]: # return self.get_cog("Sharding") # type: ignore