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

FIX: Fix crash when openai api key is invalid #101

Merged
merged 1 commit into from
Mar 16, 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
1 change: 1 addition & 0 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ TF2_LOGFILE_PATH=H:\Programs\Steam\steamapps\common\Team Fortress 2\tf\console.l
OPENAI_API_KEY=

[COMMANDS]
ENABLE_OPENAI_COMMANDS=True
GPT_COMMAND=!gpt3
; Private chat
CHATGPT_COMMAND=!pc
Expand Down
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Config(BaseModel):
GPT4_COMMAND: str
GPT4_LEGACY_COMMAND: str

ENABLE_OPENAI_COMMANDS: bool
GPT3_MODEL: str
GPT3_CHAT_MODEL: str
GPT4_MODEL: str
Expand Down Expand Up @@ -169,5 +170,9 @@ def init_config():
)

config = Config(**config_dict)

if not config.ENABLE_OPENAI_COMMANDS and not config.ENABLE_CUSTOM_MODEL:
buffered_message("You haven't enabled any AI related commands.")

except (pydantic.ValidationError, Exception) as e:
show_error_window(e)
6 changes: 5 additions & 1 deletion modules/api/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ def get_response(conversation_history: MessageHistory, username: str, model) ->
log_gui_general_message(f"Wasn't able to connect to OpenAI API. Cancelling...")
main_logger.error(f"APIError happened. [{e}]")
return
except openai.error.AuthenticationError:
log_gui_general_message("Your OpenAI api key is invalid.")
main_logger.error("OpenAI API key is invalid.")
return
except Exception as e:
log_gui_general_message(f"Unhandled error happened! Cancelling ({e})")
main_logger(f"Unhandled error happened! Cancelling ({e})")
main_logger.error(f"Unhandled error happened! Cancelling ({e})")
return

if attempts == max_attempts:
Expand Down
11 changes: 6 additions & 5 deletions modules/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ def parse_console_logs_and_build_conversation_history() -> None:

# Commands
controller.register_command("!gh", handle_gh_command)
controller.register_command(config.GPT4_COMMAND, handle_gpt4)
controller.register_command(config.GPT4_LEGACY_COMMAND, handle_gpt4l)
controller.register_command(config.RTD_COMMAND, handle_rtd)
controller.register_command(config.GPT_COMMAND, handle_gpt3)
controller.register_command(config.CHATGPT_COMMAND, handle_user_chat)
controller.register_command(config.CLEAR_CHAT_COMMAND, handle_clear)
controller.register_command(config.GLOBAL_CHAT_COMMAND, handle_global_chat)
if config.ENABLE_OPENAI_COMMANDS:
controller.register_command(config.GPT4_COMMAND, handle_gpt4)
controller.register_command(config.GPT4_LEGACY_COMMAND, handle_gpt4l)
controller.register_command(config.CHATGPT_COMMAND, handle_user_chat)
controller.register_command(config.GLOBAL_CHAT_COMMAND, handle_global_chat)
controller.register_command(config.GPT_COMMAND, handle_gpt3)
if config.ENABLE_CUSTOM_MODEL:
controller.register_command(config.CUSTOM_MODEL_COMMAND, handle_custom_model)
controller.register_command(config.CUSTOM_MODEL_CHAT_COMMAND, handle_custom_user_chat)
Expand Down
Loading