Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update-general-cogs-utils #216

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 33 additions & 20 deletions bot/cogs/general.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from __future__ import annotations

import random
import os
import random
from typing import TYPE_CHECKING, Tuple, Union

from typing import TYPE_CHECKING, Union, Tuple

import openai
import discord
import openai
from discord.ext import commands, tasks
from openai import AsyncOpenAI
from discord.utils import oauth_url
from openai import AsyncOpenAI
from sentry_sdk import capture_exception
from utils.clear_dir import clean_cache

from utils import gpt
from utils.clear_dir import clean_cache

if TYPE_CHECKING:
from ..bot import LhBot
Expand All @@ -29,9 +27,7 @@ def __init__(self, client: LhBot):
"client_secret": self.client.config.twitch_client_secret,
"grant_type": "client_credentials",
}
self.open_ai_client = AsyncOpenAI(
api_key=self.client.config.openai_api_key
)
self.open_ai_client = AsyncOpenAI(api_key=self.client.config.openai_api_key)
self.status_task.start()
self.clean_dir.start()

Expand Down Expand Up @@ -161,22 +157,39 @@ async def on_message(self, message: discord.Message):
else:
name = message.author.name
try:
chat_completion = await self.open_ai_client.chat.completions.create(
messages=[
{"role": "system", "content": gpt.context + f"when you answer someone, answer them by {name}"},
{"role": "user", "content": message.content.strip(f"<@!{self.client.user.id}>")}
chat_completion = await self.open_ai_client.chat.completions.create(
messages=[
{
"role": "system",
"content": gpt.context
+ f"when you answer someone, answer them by {name}",
},
{
"role": "user",
"content": message.content.strip(
f"<@!{self.client.user.id}>"
),
},
],
model="gpt-4o"
)
await message.channel.send(chat_completion.choices[0].message.content)
model="gpt-4o",
)
await message.channel.send(
chat_completion.choices[0].message.content
)
except openai.APIConnectionError as e:
await message.channel.send("I am currently experiencing connection issues, please try again later.")
await message.channel.send(
"I am currently experiencing connection issues, please try again later."
)
capture_exception(e)
except openai.RateLimitError as e:
await message.channel.send("I am currently experiencing rate limit issues, please try again later.")
await message.channel.send(
"I am currently experiencing rate limit issues, please try again later."
)
capture_exception(e)
except openai.APIStatusError as e:
await message.channel.send("I am currently experiencing API status issues, please try again later.")
await message.channel.send(
"I am currently experiencing API status issues, please try again later."
)
capture_exception(e)

@commands.hybrid_command("join", with_app_command=True)
Expand Down
1 change: 1 addition & 0 deletions bot/utils/clear_dir.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from sentry_sdk import capture_exception


Expand Down