From efe7ac8e12adfafa9eedebf440d48a9c68f3a8b7 Mon Sep 17 00:00:00 2001 From: Avery Date: Thu, 3 Oct 2024 14:00:45 -0700 Subject: [PATCH] Show GenAI signal analysis errors in Slack message. (#5287) * Show GenAI signal analysis errors in Slack modal. * Retain warning logs. * Update src/dispatch/plugins/dispatch_slack/case/messages.py Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> * Update function name. --------- Co-authored-by: Marc Vilanova <39573146+mvilanova@users.noreply.github.com> --- .../plugins/dispatch_slack/case/messages.py | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/dispatch/plugins/dispatch_slack/case/messages.py b/src/dispatch/plugins/dispatch_slack/case/messages.py index f44643c2544f..aedf77be8f3c 100644 --- a/src/dispatch/plugins/dispatch_slack/case/messages.py +++ b/src/dispatch/plugins/dispatch_slack/case/messages.py @@ -287,6 +287,16 @@ def create_action_buttons_message( return Message(blocks=signal_metadata_blocks).build()["blocks"] +def create_genai_signal_message_metadata_blocks( + signal_metadata_blocks: list[Block], message: str +) -> list[Block]: + signal_metadata_blocks.append( + Section(text=f":magic_wand: *GenAI Alert Analysis*\n\n{message}"), + ) + signal_metadata_blocks.append(Divider()) + return Message(blocks=signal_metadata_blocks).build()["blocks"] + + def create_genai_signal_analysis_message( case: Case, channel_id: str, @@ -316,8 +326,9 @@ def create_genai_signal_analysis_message( ).first() if not first_instance_id or not first_instance_signal: - log.warning("Unable to generate GenAI signal analysis. No signal instances found.") - return signal_metadata_blocks + message = "Unable to generate GenAI signal analysis. No signal instances found." + log.warning(message) + return create_genai_signal_message_metadata_blocks(signal_metadata_blocks, message) # Fetch related cases related_cases = [] @@ -370,16 +381,16 @@ def create_genai_signal_analysis_message( ) if not genai_plugin: - log.warning( + message = ( "Unable to generate GenAI signal analysis. No artificial-intelligence plugin enabled." ) - return signal_metadata_blocks + log.warning(message) + return create_genai_signal_message_metadata_blocks(signal_metadata_blocks, message) if not signal_instance.signal.genai_prompt: - log.warning( - f"Unable to generate GenAI signal analysis. No GenAI prompt defined for {signal_instance.signal.name}" - ) - return signal_metadata_blocks + message = f"Unable to generate GenAI signal analysis. No GenAI prompt defined for {signal_instance.signal.name}" + log.warning(message) + return create_genai_signal_message_metadata_blocks(signal_metadata_blocks, message) response = genai_plugin.instance.chat_completion( prompt=f""" @@ -403,11 +414,11 @@ def create_genai_signal_analysis_message( ) message = response["choices"][0]["message"]["content"] - signal_metadata_blocks.append( - Section(text=f":magic_wand: *GenAI Alert Analysis*\n\n{message}"), - ) - signal_metadata_blocks.append(Divider()) - return Message(blocks=signal_metadata_blocks).build()["blocks"] + if not message: + message = "Unable to generate GenAI signal analysis. We received an empty response from the artificial-intelligence plugin." + log.warning(message) + + return create_genai_signal_message_metadata_blocks(signal_metadata_blocks, message) def create_signal_engagement_message(