Remove "no API" prompt (#120280)

This commit is contained in:
Paulus Schoutsen 2024-06-24 02:57:59 -04:00 committed by GitHub
parent 4785810dc3
commit 5c2db162c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 59 deletions

View File

@ -349,27 +349,22 @@ class GoogleGenerativeAIConversationEntity(
): ):
user_name = user.name user_name = user.name
if llm_api: parts = [
api_prompt = llm_api.api_prompt template.Template(
else: llm.BASE_PROMPT
api_prompt = llm.async_render_no_api_prompt(self.hass) + self.entry.options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT),
self.hass,
return "\n".join( ).async_render(
( {
template.Template( "ha_name": self.hass.config.location_name,
llm.BASE_PROMPT "user_name": user_name,
+ self.entry.options.get( "llm_context": llm_context,
CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT },
), parse_result=False,
self.hass,
).async_render(
{
"ha_name": self.hass.config.location_name,
"user_name": user_name,
"llm_context": llm_context,
},
parse_result=False,
),
api_prompt,
) )
) ]
if llm_api:
parts.append(llm_api.api_prompt)
return "\n".join(parts)

View File

@ -172,28 +172,20 @@ class OpenAIConversationEntity(
user_name = user.name user_name = user.name
try: try:
if llm_api: prompt_parts = [
api_prompt = llm_api.api_prompt template.Template(
else: llm.BASE_PROMPT
api_prompt = llm.async_render_no_api_prompt(self.hass) + options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT),
self.hass,
prompt = "\n".join( ).async_render(
( {
template.Template( "ha_name": self.hass.config.location_name,
llm.BASE_PROMPT "user_name": user_name,
+ options.get(CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT), "llm_context": llm_context,
self.hass, },
).async_render( parse_result=False,
{
"ha_name": self.hass.config.location_name,
"user_name": user_name,
"llm_context": llm_context,
},
parse_result=False,
),
api_prompt,
) )
) ]
except TemplateError as err: except TemplateError as err:
LOGGER.error("Error rendering prompt: %s", err) LOGGER.error("Error rendering prompt: %s", err)
@ -206,6 +198,11 @@ class OpenAIConversationEntity(
response=intent_response, conversation_id=conversation_id 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 # Create a copy of the variable because we attach it to the trace
messages = [ messages = [
ChatCompletionSystemMessageParam(role="system", content=prompt), ChatCompletionSystemMessageParam(role="system", content=prompt),

View File

@ -51,11 +51,11 @@ Answer in plain text. Keep it simple and to the point.
@callback @callback
def async_render_no_api_prompt(hass: HomeAssistant) -> str: def async_render_no_api_prompt(hass: HomeAssistant) -> str:
"""Return the prompt to be used when no API is configured.""" """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 " No longer used since Home Assistant 2024.7.
"and allow access to Home Assistant." """
) return ""
@singleton("llm") @singleton("llm")

View File

@ -35,7 +35,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. 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', 'role': 'user',
}), }),
@ -88,7 +87,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. 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', 'role': 'user',
}), }),
@ -142,7 +140,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. 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, 'tools': None,
}), }),
@ -187,7 +184,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. 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, 'tools': None,
}), }),
@ -244,7 +240,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. Answer in plain text. Keep it simple and to the point.
<no_api_prompt>
''', ''',
'tools': None, 'tools': None,
}), }),
@ -293,7 +288,6 @@
You are a voice assistant for Home Assistant. You are a voice assistant for Home Assistant.
Answer questions about the world truthfully. Answer questions about the world truthfully.
Answer in plain text. Keep it simple and to the point. Answer in plain text. Keep it simple and to the point.
<no_api_prompt>
''', ''',
'tools': None, 'tools': None,
}), }),

View File

@ -75,10 +75,6 @@ async def test_default_prompt(
"homeassistant.components.google_generative_ai_conversation.conversation.llm.AssistAPI._async_get_api_prompt", "homeassistant.components.google_generative_ai_conversation.conversation.llm.AssistAPI._async_get_api_prompt",
return_value="<api_prompt>", return_value="<api_prompt>",
), ),
patch(
"homeassistant.components.google_generative_ai_conversation.conversation.llm.async_render_no_api_prompt",
return_value="<no_api_prompt>",
),
): ):
mock_chat = AsyncMock() mock_chat = AsyncMock()
mock_model.return_value.start_chat.return_value = mock_chat mock_model.return_value.start_chat.return_value = mock_chat