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
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)

View File

@ -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),

View File

@ -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")

View File

@ -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.
<no_api_prompt>
''',
'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.
<no_api_prompt>
''',
'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",
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_model.return_value.start_chat.return_value = mock_chat