From 69f237fa9ea61fb769909ba46715c8f553c0f79b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 25 May 2024 00:02:53 -0400 Subject: [PATCH] Update Google safety defaults to match Google (#118084) --- .../const.py | 2 +- .../conversation.py | 14 ++++++-- .../snapshots/test_conversation.ambr | 32 +++++++++---------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/google_generative_ai_conversation/const.py b/homeassistant/components/google_generative_ai_conversation/const.py index 549883d4fb9..a83ffed2d88 100644 --- a/homeassistant/components/google_generative_ai_conversation/const.py +++ b/homeassistant/components/google_generative_ai_conversation/const.py @@ -22,4 +22,4 @@ CONF_HARASSMENT_BLOCK_THRESHOLD = "harassment_block_threshold" CONF_HATE_BLOCK_THRESHOLD = "hate_block_threshold" CONF_SEXUAL_BLOCK_THRESHOLD = "sexual_block_threshold" CONF_DANGEROUS_BLOCK_THRESHOLD = "dangerous_block_threshold" -RECOMMENDED_HARM_BLOCK_THRESHOLD = "BLOCK_LOW_AND_ABOVE" +RECOMMENDED_HARM_BLOCK_THRESHOLD = "BLOCK_MEDIUM_AND_ABOVE" diff --git a/homeassistant/components/google_generative_ai_conversation/conversation.py b/homeassistant/components/google_generative_ai_conversation/conversation.py index 21d26ab5616..f08c6c14e60 100644 --- a/homeassistant/components/google_generative_ai_conversation/conversation.py +++ b/homeassistant/components/google_generative_ai_conversation/conversation.py @@ -263,10 +263,20 @@ class GoogleGenerativeAIConversationEntity( genai_types.BlockedPromptException, genai_types.StopCandidateException, ) as err: - LOGGER.error("Error sending message: %s", err) + LOGGER.error("Error sending message: %s %s", type(err), err) + + if isinstance( + err, genai_types.StopCandidateException + ) and "finish_reason: SAFETY\n" in str(err): + error = "The message got blocked by your safety settings" + else: + error = ( + f"Sorry, I had a problem talking to Google Generative AI: {err}" + ) + intent_response.async_set_error( intent.IntentResponseErrorCode.UNKNOWN, - f"Sorry, I had a problem talking to Google Generative AI: {err}", + error, ) return conversation.ConversationResult( response=intent_response, conversation_id=conversation_id diff --git a/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr b/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr index 112e1f91b55..d3a4f4b4b58 100644 --- a/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr +++ b/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr @@ -14,10 +14,10 @@ }), 'model_name': 'models/gemini-1.5-flash-latest', 'safety_settings': dict({ - 'DANGEROUS': 'BLOCK_LOW_AND_ABOVE', - 'HARASSMENT': 'BLOCK_LOW_AND_ABOVE', - 'HATE': 'BLOCK_LOW_AND_ABOVE', - 'SEXUAL': 'BLOCK_LOW_AND_ABOVE', + 'DANGEROUS': 'BLOCK_MEDIUM_AND_ABOVE', + 'HARASSMENT': 'BLOCK_MEDIUM_AND_ABOVE', + 'HATE': 'BLOCK_MEDIUM_AND_ABOVE', + 'SEXUAL': 'BLOCK_MEDIUM_AND_ABOVE', }), 'tools': None, }), @@ -67,10 +67,10 @@ }), 'model_name': 'models/gemini-1.5-flash-latest', 'safety_settings': dict({ - 'DANGEROUS': 'BLOCK_LOW_AND_ABOVE', - 'HARASSMENT': 'BLOCK_LOW_AND_ABOVE', - 'HATE': 'BLOCK_LOW_AND_ABOVE', - 'SEXUAL': 'BLOCK_LOW_AND_ABOVE', + 'DANGEROUS': 'BLOCK_MEDIUM_AND_ABOVE', + 'HARASSMENT': 'BLOCK_MEDIUM_AND_ABOVE', + 'HATE': 'BLOCK_MEDIUM_AND_ABOVE', + 'SEXUAL': 'BLOCK_MEDIUM_AND_ABOVE', }), 'tools': None, }), @@ -120,10 +120,10 @@ }), 'model_name': 'models/gemini-1.5-flash-latest', 'safety_settings': dict({ - 'DANGEROUS': 'BLOCK_LOW_AND_ABOVE', - 'HARASSMENT': 'BLOCK_LOW_AND_ABOVE', - 'HATE': 'BLOCK_LOW_AND_ABOVE', - 'SEXUAL': 'BLOCK_LOW_AND_ABOVE', + 'DANGEROUS': 'BLOCK_MEDIUM_AND_ABOVE', + 'HARASSMENT': 'BLOCK_MEDIUM_AND_ABOVE', + 'HATE': 'BLOCK_MEDIUM_AND_ABOVE', + 'SEXUAL': 'BLOCK_MEDIUM_AND_ABOVE', }), 'tools': None, }), @@ -173,10 +173,10 @@ }), 'model_name': 'models/gemini-1.5-flash-latest', 'safety_settings': dict({ - 'DANGEROUS': 'BLOCK_LOW_AND_ABOVE', - 'HARASSMENT': 'BLOCK_LOW_AND_ABOVE', - 'HATE': 'BLOCK_LOW_AND_ABOVE', - 'SEXUAL': 'BLOCK_LOW_AND_ABOVE', + 'DANGEROUS': 'BLOCK_MEDIUM_AND_ABOVE', + 'HARASSMENT': 'BLOCK_MEDIUM_AND_ABOVE', + 'HATE': 'BLOCK_MEDIUM_AND_ABOVE', + 'SEXUAL': 'BLOCK_MEDIUM_AND_ABOVE', }), 'tools': None, }),