Skip to content

Commit

Permalink
fixed the welcome ai messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Brettanda committed May 6, 2024
1 parent 63e2f7a commit 361ed11
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
9 changes: 4 additions & 5 deletions cogs/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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**"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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))
Expand Down
17 changes: 7 additions & 10 deletions cogs/welcome.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 361ed11

Please sign in to comment.