Skip to content

Commit

Permalink
improve error checking for gpt
Browse files Browse the repository at this point in the history
  • Loading branch information
kytpbs committed Sep 19, 2024
1 parent d92a4a0 commit 0f1d92c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.


Expand Down
19 changes: 15 additions & 4 deletions src/llm_system/gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/llm_system/llm_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 0f1d92c

Please sign in to comment.