Skip to content

Commit

Permalink
temp removed channel updating
Browse files Browse the repository at this point in the history
  • Loading branch information
Mole1424 committed Oct 13, 2023
1 parent 23398f2 commit 23fbfb3
Showing 1 changed file with 0 additions and 75 deletions.
75 changes: 0 additions & 75 deletions cogs/commands/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ def clean(msg, *prefixes):
class ChatGPT(commands.Cog):
def __init__(self, bot: Bot):
self.bot = bot
# When apollo gained ai capabilities, for calculating API costs
self.ai_epoch = date(year=2023, month=3, day=1)
self.usage_endpoint = "https://api.openai.com/dashboard/billing/usage"

openai.api_key = CONFIG.OPENAI_API_KEY
self.model = "gpt-3.5-turbo"
Expand All @@ -52,9 +49,6 @@ def __init__(self, bot: Bot):
self.system_prompt += "\nYou are in a Discord chat room, each message is prepended by the name of the message's author separated by a colon."
self.cooldowns = {}

# have to start the task loop
self.update_channel_descriptions.start()

@commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT)
async def prompt(self, ctx: Context, *, message: str):
# Effectively a dummy command, since just needs something to allow a prompt message
Expand Down Expand Up @@ -202,75 +196,6 @@ async def fetch_previous(
return await message.channel.fetch_message(message.reference.message_id)
return None

@tasks.loop(minutes=30)
async def update_channel_descriptions(self):
"""
Update the AI chat description to include the current API usage.
Assumes the channel to be updated is the first one in the list.
Runs in a task loop, firing periodically
"""
channel = self.bot.get_channel(CONFIG.AI_CHAT_CHANNELS[0])
if not isinstance(channel, discord.TextChannel):
raise Exception("First channel in AI_CHAT_CHANNELS must be a text channel")
spent_usd = await self.get_api_usage() / 100

# we need to do currency conversion
async with aiohttp.ClientSession() as session:
resp = await session.get(
"https://api.exchangerate.host/convert?from=USD&to=GBP"
)
rate = (await resp.json())["result"]

spent_gbp = round(spent_usd * rate, 4)
await channel.edit(
topic=f"Chat with Apollo here! API usage to date: £{spent_gbp}"
)

@update_channel_descriptions.before_loop
async def before_updates(self):
"""
Tells the task loop to wait until the bot is ready before starting
"""
await self.bot.wait_until_ready()

async def get_api_usage(self) -> float:
"""
Gets the cumulative usage of the OpenAI API since self.ai_epoch
Returns the usage in USD cents
**The API endpoint being used is undocumented, and may break at any time**
"""
# we have to fetch it in 100 day chunks, because of odd API limitations
chunks = [date.today() + timedelta(days=1)]
while chunks[-1] > self.ai_epoch:
chunks.append(chunks[-1] - timedelta(days=98))

date_pairs = (
{"start_date": str(start), "end_date": str(end)}
for (end, start) in zip(chunks, chunks[1:])
)

headers = {"Authorization": f"Bearer {CONFIG.OPENAI_API_KEY}"}
async with aiohttp.ClientSession(headers=headers) as session:
# asyncio.gather schedules all the futures simultaneously
responses = await asyncio.gather(
*(session.get(url=self.usage_endpoint, params=ps) for ps in date_pairs)
)

if all(r.ok for r in responses):
# happy path
# fetching response content as json is a coro
usage = await asyncio.gather(*(r.json() for r in responses))
return sum(float(u["total_usage"]) for u in usage)
else:
# pin down the errors
statuses = ", ".join(
f"{r.status}: {r.reason}" for r in responses if r.status != 200
)
raise Exception(
"Failed to fetch OpenAI API usage: error responses were: "
+ statuses
)


async def setup(bot: Bot):
await bot.add_cog(ChatGPT(bot))

0 comments on commit 23fbfb3

Please sign in to comment.