From 7bc60472a658a17c41499f6ee4875abc2643eddb Mon Sep 17 00:00:00 2001 From: John Li Date: Wed, 10 Jul 2024 14:20:47 -0700 Subject: [PATCH] Improve ErrorResponse User Messages the text field is a user-facing message. Show cleaner error messages, and put the raw exception in raw_response instead. --- fireworks_poe_bot/fw_poe_image_bot.py | 13 +++++++++- fireworks_poe_bot/fw_poe_qr_bot.py | 13 +++++++++- .../fw_poe_stability_image_bot.py | 13 +++++++++- fireworks_poe_bot/fw_poe_text_bot.py | 25 +++++++++++++++---- fireworks_poe_bot/fw_poe_video_bot.py | 19 ++++++++++++-- 5 files changed, 73 insertions(+), 10 deletions(-) diff --git a/fireworks_poe_bot/fw_poe_image_bot.py b/fireworks_poe_bot/fw_poe_image_bot.py index 886026f..4b3c2c3 100644 --- a/fireworks_poe_bot/fw_poe_image_bot.py +++ b/fireworks_poe_bot/fw_poe_image_bot.py @@ -288,9 +288,20 @@ async def get_response( ) if "prompt is too long" in str(e): error_type = "user_message_too_long" + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The user message is too long. Please try again with a shorter message." + ) else: error_type = None - yield ErrorResponse(allow_retry=False, error_type=error_type, text=str(e)) + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The bot encountered an unexpected error." + ) return finally: fireworks.client.api_key = orig_api_key diff --git a/fireworks_poe_bot/fw_poe_qr_bot.py b/fireworks_poe_bot/fw_poe_qr_bot.py index ea26fad..d39b1a9 100644 --- a/fireworks_poe_bot/fw_poe_qr_bot.py +++ b/fireworks_poe_bot/fw_poe_qr_bot.py @@ -341,9 +341,20 @@ async def get_response( ) if "prompt is too long" in str(e): error_type = "user_message_too_long" + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The user message is too long. Please try again with a shorter message." + ) else: error_type = None - yield ErrorResponse(allow_retry=False, error_type=error_type, text=str(e)) + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The bot encountered an unexpected error." + ) return finally: fireworks.client.api_key = orig_api_key diff --git a/fireworks_poe_bot/fw_poe_stability_image_bot.py b/fireworks_poe_bot/fw_poe_stability_image_bot.py index e5ccdc5..ddab1d6 100644 --- a/fireworks_poe_bot/fw_poe_stability_image_bot.py +++ b/fireworks_poe_bot/fw_poe_stability_image_bot.py @@ -287,9 +287,20 @@ async def fetch_image(): ) if "prompt is too long" in str(e): error_type = "user_message_too_long" + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The user message is too long. Please try again with a shorter message." + ) else: error_type = None - yield ErrorResponse(allow_retry=False, error_type=error_type, text=str(e)) + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The bot encountered an unexpected error." + ) return # Function to upload a PIL Image to an S3 bucket with a presigned URL diff --git a/fireworks_poe_bot/fw_poe_text_bot.py b/fireworks_poe_bot/fw_poe_text_bot.py index ee0e3ed..e74b045 100644 --- a/fireworks_poe_bot/fw_poe_text_bot.py +++ b/fireworks_poe_bot/fw_poe_text_bot.py @@ -230,7 +230,11 @@ async def get_response( protocol_message.attachments[0].url ) except Exception as e: - yield ErrorResponse(allow_retry=False, text=str(e)) + yield ErrorResponse( + allow_retry=False, + raw_response=e, + text="The bot encountered an error while processing an attached image." + ) raise RuntimeError(str(e)) if img_buffer: @@ -238,7 +242,7 @@ async def get_response( if cumulative_image_size_mb > 8: # Apigee has a limit of 10MB for payload, we set image total limit to 8MB yield ErrorResponse( - allow_retry=False, text="The total image size is too big" + allow_retry=False, text="The total image size is too big. Limit is 8MB." ) raise RuntimeError("The total image size is too big") messages.append( @@ -306,8 +310,8 @@ async def get_response( ) yield ErrorResponse( allow_retry=False, - text="Error performing image safety check: " - + str(e), + text="Error performing image safety check.", + raw_response=e, ) return last_image_kept = True @@ -493,9 +497,20 @@ def merge_messages_groups( ) if "prompt is too long" in str(e): error_type = "user_message_too_long" + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The user message is too long. Please try again with a shorter message." + ) else: error_type = None - yield ErrorResponse(allow_retry=False, error_type=error_type, text=str(e)) + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The bot encountered an unexpected error." + ) return finally: fireworks.client.api_key = orig_api_key diff --git a/fireworks_poe_bot/fw_poe_video_bot.py b/fireworks_poe_bot/fw_poe_video_bot.py index f091aef..52e2d33 100644 --- a/fireworks_poe_bot/fw_poe_video_bot.py +++ b/fireworks_poe_bot/fw_poe_video_bot.py @@ -214,7 +214,11 @@ async def get_response( protocol_message.attachments[0].url ) except Exception as e: - yield ErrorResponse(allow_retry=False, text=str(e)) + yield ErrorResponse( + allow_retry=False, + raw_response=e, + text="The bot encountered an error while processing an attached image." + ) raise RuntimeError(str(e)) else: yield self.replace_response_event( @@ -311,9 +315,20 @@ async def get_response( ) if "prompt is too long" in str(e): error_type = "user_message_too_long" + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The user message is too long. Please try again with a shorter message." + ) else: error_type = None - yield ErrorResponse(allow_retry=False, error_type=error_type, text=str(e)) + yield ErrorResponse( + allow_retry=False, + error_type=error_type, + raw_response=e, + text="The bot encountered an unexpected error." + ) return finally: fireworks.client.api_key = orig_api_key