fix: Enhance error handling for user authentication

When processing large volumes of data, it's essential to handle errors gracefully and provide clear feedback to users. This change introduces additional checks to ensure robust error handling during user authentication, reducing the likelihood of errors propagating further down the pipeline.

This improvement not only enhances the overall stability of the system but also provides a better user experience by providing more informative error messages in the event of an issue.
This commit is contained in:
Kumi 2024-05-10 18:18:40 +02:00
parent f6a3f4ce66
commit 9a4c250eb4
Signed by: kumi
GPG key ID: ECBCC9082395383F
4 changed files with 32 additions and 16 deletions

View file

@ -17,7 +17,7 @@ def get_version():
return None
return package_version
def main():
async def main():
# Parse command line arguments
parser = ArgumentParser()
parser.add_argument(
@ -40,7 +40,7 @@ def main():
config.read(args.config)
# Create bot
bot, new_config = GPTBot.from_config(config)
bot, new_config = await GPTBot.from_config(config)
# Update config with new values
if new_config:
@ -52,7 +52,7 @@ def main():
# Start bot
try:
asyncio.run(bot.run())
await bot.run()
except KeyboardInterrupt:
print("Received KeyboardInterrupt - exiting...")
except SystemExit:
@ -60,4 +60,4 @@ def main():
if __name__ == "__main__":
main()
asyncio.get_event_loop().run_until_complete(main())

View file

@ -200,7 +200,7 @@ class GPTBot:
USER_AGENT = "matrix-gptbot/dev (+https://kumig.it/kumitterer/matrix-gptbot)"
@classmethod
def from_config(cls, config: ConfigParser):
async def from_config(cls, config: ConfigParser):
"""Create a new GPTBot instance from a config file.
Args:
@ -381,7 +381,12 @@ class GPTBot:
self.default_system_message if system_message is None else system_message
)
encoding = tiktoken.encoding_for_model(model)
try:
encoding = tiktoken.encoding_for_model(model)
except:
# TODO: Handle this more gracefully
encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
total_tokens = 0
system_message_tokens = (

View file

@ -164,7 +164,7 @@ class OpenAI:
if count > 5:
self.logger.log(f"Recursion depth exceeded, aborting.")
return await self.generate_chat_response(
messsages=messages,
messages=messages,
user=user,
room=room,
allow_override=False, # TODO: Could this be a problem?
@ -189,7 +189,7 @@ class OpenAI:
if allow_override and not "gpt-3.5-turbo" in original_model:
if self.bot.config.getboolean("OpenAI", "ForceTools", fallback=False):
self.logger.log(f"Overriding chat model to use tools")
chat_model = "gpt-3.5-turbo-0125"
chat_model = "gpt-3.5-turbo"
out_messages = []
@ -267,9 +267,20 @@ class OpenAI:
"OpenAI", "MaxTokens", fallback=4000
)
api_url = self.base_url
if chat_model.startswith("gpt-"):
if not self.chat_model.startswith("gpt-"):
# The model is overridden, we have to ensure that OpenAI is used
if self.api_key.startswith("sk-"):
self.openai_api.base_url = "https://api.openai.com/v1/"
chat_partial = partial(self.openai_api.chat.completions.create, **kwargs)
response = await self._request_with_retries(chat_partial)
# Setting back the API URL to whatever it was before
self.openai_api.base_url = api_url
choice = response.choices[0]
result_text = choice.message.content

View file

@ -8,18 +8,18 @@ async def command_help(room: MatrixRoom, event: RoomMessageText, bot):
- !gptbot help - Show this message
- !gptbot botinfo - Show information about the bot
- !gptbot privacy - Show privacy information
- !gptbot newroom \<room name\> - Create a new room and invite yourself to it
- !gptbot newroom <room name> - Create a new room and invite yourself to it
- !gptbot stats - Show usage statistics for this room
- !gptbot systemmessage \<message\> - Get or set the system message for this room
- !gptbot systemmessage <message> - Get or set the system message for this room
- !gptbot space [enable|disable|update|invite] - Enable, disable, force update, or invite yourself to your space
- !gptbot coin - Flip a coin (heads or tails)
- !gptbot dice [number] - Roll a dice with the specified number of sides (default: 6)
- !gptbot imagine \<prompt\> - Generate an image from a prompt
- !gptbot calculate [--text] [--details] \<query\> - Calculate a result to a calculation, optionally forcing text output instead of an image, and optionally showing additional details like the input interpretation
- !gptbot chat \<message\> - Send a message to the chat API
- !gptbot classify \<message\> - Classify a message using the classification API
- !gptbot custom \<message\> - Used for custom commands handled by the chat model and defined through the room's system message
- !gptbot roomsettings [use_classification|use_timing|always_reply|system_message|tts] [true|false|\<message\>] - Get or set room settings
- !gptbot imagine <prompt> - Generate an image from a prompt
- !gptbot calculate [--text] [--details] <query> - Calculate a result to a calculation, optionally forcing text output instead of an image, and optionally showing additional details like the input interpretation
- !gptbot chat <message> - Send a message to the chat API
- !gptbot classify <message> - Classify a message using the classification API
- !gptbot custom <message> - Used for custom commands handled by the chat model and defined through the room's system message
- !gptbot roomsettings [use_classification|use_timing|always_reply|system_message|tts] [true|false|<message>] - Get or set room settings
- !gptbot ignoreolder - Ignore messages before this point as context
"""