diff --git a/src/client.py b/src/client.py index 10933b6..6b3037a 100644 --- a/src/client.py +++ b/src/client.py @@ -4,6 +4,7 @@ import discord from Constants import CYAN, DELETED_MESSAGES_CHANNEL_ID, GENERAL_CHAT_ID, BOSS_BOT_CHANNEL_ID +from src.llm_system.llm_errors import LLMError, NoTokenError, RanOutOfMoneyError from src.llm_system import gpt from src import file_handeler from src.message_handeler import call_command @@ -205,7 +206,17 @@ async def on_dm(message: discord.Message): raise ValueError("This function is only for DMs") if message.content == "": return - answer = await gpt.message_chat(message) + try: + answer = await gpt.message_chat(message) + except NoTokenError as e: + await message.reply("Zeki sistemi devre dışı bırakıldı, çalışması için yöneticiyle iletişime geçin") + raise e # re-raise the error to log it + except RanOutOfMoneyError as e: + await message.reply("Fakirlik vurdu, düzeltilmesini bekleyin, bu biraz zaman alabilir") + raise e # re-raise the error to log it + except LLMError as e: + await message.reply("Bir şeyler ters gitti... lütfen sonra tekrar dene") + raise e # re-raise the error to log it await message.reply(str(answer)) # not using an embed because it's easier to parse history this way. diff --git a/src/llm_system/gpt.py b/src/llm_system/gpt.py index b8f3f6b..3aa0911 100644 --- a/src/llm_system/gpt.py +++ b/src/llm_system/gpt.py @@ -4,16 +4,21 @@ import discord from dotenv import load_dotenv -from openai import OpenAI +from openai import OpenAI, RateLimitError from src.llm_system.llm_data import MessageHistory -from src.llm_system.openai_fixer import GPTMessages from src.llm_system.llm_discord_integration import ( get_message_from_interaction, get_message_history_from_discord_channel, get_message_history_from_discord_message, ) -from src.llm_system.llm_errors import APICallFailedError, NoTokenError +from src.llm_system.llm_errors import ( + APICallFailedError, + NoTokenError, + RanOutOfMoneyError, + TooFastError, +) +from src.llm_system.openai_fixer import GPTMessages SYSTEM_PROMPT_BASE = ( "You are a discord bot named '{bot_name}' in a discord {server_name}" @@ -38,6 +43,12 @@ async def chat(message_history: GPTMessages) -> str: model="gpt-3.5-turbo", messages=message_history.to_gpt_list(), ) + except RateLimitError as e: + LOGGER.error(f"Rate limit error {e}") + if e.code == 'insufficient_quota': + raise RanOutOfMoneyError() from e + raise TooFastError("Rate limit hit") from e + except Exception as e: LOGGER.error(f"Failed to complete message history {e}") raise APICallFailedError("Something went wrong with the API call") from e @@ -49,7 +60,7 @@ async def chat(message_history: GPTMessages) -> str: response = choices[0].message.content if not response: - raise APICallFailedError("No response from the API") + raise APICallFailedError("No response from the API results content") return response diff --git a/src/llm_system/llm_errors.py b/src/llm_system/llm_errors.py index 6cd7ab7..0f3b50e 100644 --- a/src/llm_system/llm_errors.py +++ b/src/llm_system/llm_errors.py @@ -23,5 +23,10 @@ class APICallFailedError(LLMError): pass -class RanOutOfMoneyError(APICallFailedError): +class TooFastError(APICallFailedError): pass + + +class RanOutOfMoneyError(APICallFailedError): + def __init__(self, message: Optional[str] = None) -> None: + super().__init__(message or "Ran out of money")