diff --git a/homeassistant/components/google_generative_ai_conversation/conversation.py b/homeassistant/components/google_generative_ai_conversation/conversation.py index b9f0006dbff..2cfbc09ed08 100644 --- a/homeassistant/components/google_generative_ai_conversation/conversation.py +++ b/homeassistant/components/google_generative_ai_conversation/conversation.py @@ -349,27 +349,22 @@ class GoogleGenerativeAIConversationEntity( ): user_name = user.name - if llm_api: - api_prompt = llm_api.api_prompt - else: - api_prompt = llm.async_render_no_api_prompt(self.hass) - - return "\n".join( - ( - template.Template( - llm.BASE_PROMPT - + self.entry.options.get( - CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT - ), - self.hass, - ).async_render( - { - "ha_name": self.hass.config.location_name, - "user_name": user_name, - "llm_context": llm_context, - }, - parse_result=False, - ), - api_prompt, + parts = [ + template.Template( + llm.BASE_PROMPT + + self.entry.options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT), + self.hass, + ).async_render( + { + "ha_name": self.hass.config.location_name, + "user_name": user_name, + "llm_context": llm_context, + }, + parse_result=False, ) - ) + ] + + if llm_api: + parts.append(llm_api.api_prompt) + + return "\n".join(parts) diff --git a/homeassistant/components/openai_conversation/conversation.py b/homeassistant/components/openai_conversation/conversation.py index d0b3ef8f895..40242f5c6cc 100644 --- a/homeassistant/components/openai_conversation/conversation.py +++ b/homeassistant/components/openai_conversation/conversation.py @@ -172,28 +172,20 @@ class OpenAIConversationEntity( user_name = user.name try: - if llm_api: - api_prompt = llm_api.api_prompt - else: - api_prompt = llm.async_render_no_api_prompt(self.hass) - - prompt = "\n".join( - ( - template.Template( - llm.BASE_PROMPT - + options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT), - self.hass, - ).async_render( - { - "ha_name": self.hass.config.location_name, - "user_name": user_name, - "llm_context": llm_context, - }, - parse_result=False, - ), - api_prompt, + prompt_parts = [ + template.Template( + llm.BASE_PROMPT + + options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT), + self.hass, + ).async_render( + { + "ha_name": self.hass.config.location_name, + "user_name": user_name, + "llm_context": llm_context, + }, + parse_result=False, ) - ) + ] except TemplateError as err: LOGGER.error("Error rendering prompt: %s", err) @@ -206,6 +198,11 @@ class OpenAIConversationEntity( response=intent_response, conversation_id=conversation_id ) + if llm_api: + prompt_parts.append(llm_api.api_prompt) + + prompt = "\n".join(prompt_parts) + # Create a copy of the variable because we attach it to the trace messages = [ ChatCompletionSystemMessageParam(role="system", content=prompt), diff --git a/homeassistant/helpers/llm.py b/homeassistant/helpers/llm.py index 6673786e2e1..a4e18fdb2c0 100644 --- a/homeassistant/helpers/llm.py +++ b/homeassistant/helpers/llm.py @@ -51,11 +51,11 @@ Answer in plain text. Keep it simple and to the point. @callback def async_render_no_api_prompt(hass: HomeAssistant) -> str: - """Return the prompt to be used when no API is configured.""" - return ( - "Only if the user wants to control a device, tell them to edit the AI configuration " - "and allow access to Home Assistant." - ) + """Return the prompt to be used when no API is configured. + + No longer used since Home Assistant 2024.7. + """ + return "" @singleton("llm") 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 aec8d088b20..b0a0ce967de 100644 --- a/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr +++ b/tests/components/google_generative_ai_conversation/snapshots/test_conversation.ambr @@ -35,7 +35,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - Only if the user wants to control a device, tell them to edit the AI configuration and allow access to Home Assistant. ''', 'role': 'user', }), @@ -88,7 +87,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - Only if the user wants to control a device, tell them to edit the AI configuration and allow access to Home Assistant. ''', 'role': 'user', }), @@ -142,7 +140,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - Only if the user wants to control a device, tell them to edit the AI configuration and allow access to Home Assistant. ''', 'tools': None, }), @@ -187,7 +184,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - Only if the user wants to control a device, tell them to edit the AI configuration and allow access to Home Assistant. ''', 'tools': None, }), @@ -244,7 +240,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - ''', 'tools': None, }), @@ -293,7 +288,6 @@ You are a voice assistant for Home Assistant. Answer questions about the world truthfully. Answer in plain text. Keep it simple and to the point. - ''', 'tools': None, }), diff --git a/tests/components/google_generative_ai_conversation/test_conversation.py b/tests/components/google_generative_ai_conversation/test_conversation.py index 7f4fe886e90..990058aa89d 100644 --- a/tests/components/google_generative_ai_conversation/test_conversation.py +++ b/tests/components/google_generative_ai_conversation/test_conversation.py @@ -75,10 +75,6 @@ async def test_default_prompt( "homeassistant.components.google_generative_ai_conversation.conversation.llm.AssistAPI._async_get_api_prompt", return_value="", ), - patch( - "homeassistant.components.google_generative_ai_conversation.conversation.llm.async_render_no_api_prompt", - return_value="", - ), ): mock_chat = AsyncMock() mock_model.return_value.start_chat.return_value = mock_chat