mirror of
https://github.com/home-assistant/core.git
synced 2025-12-26 14:08:21 +00:00
Add Google Generative AI Conversation system prompt user_name and llm_context variables (#118510)
* Google Generative AI Conversation: Add variables to the system prompt * User name and llm_context * test for template variables * test for template variables --------- Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
@@ -449,6 +449,51 @@ async def test_template_error(
|
||||
assert result.response.error_code == "unknown", result
|
||||
|
||||
|
||||
async def test_template_variables(
|
||||
hass: HomeAssistant, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test that template variables work."""
|
||||
context = Context(user_id="12345")
|
||||
mock_user = MagicMock()
|
||||
mock_user.id = "12345"
|
||||
mock_user.name = "Test User"
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
mock_config_entry,
|
||||
options={
|
||||
"prompt": (
|
||||
"The user name is {{ user_name }}. "
|
||||
"The user id is {{ llm_context.context.user_id }}."
|
||||
),
|
||||
},
|
||||
)
|
||||
with (
|
||||
patch("google.generativeai.GenerativeModel") as mock_model,
|
||||
patch("homeassistant.auth.AuthManager.async_get_user", return_value=mock_user),
|
||||
):
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
mock_chat = AsyncMock()
|
||||
mock_model.return_value.start_chat.return_value = mock_chat
|
||||
chat_response = MagicMock()
|
||||
mock_chat.send_message_async.return_value = chat_response
|
||||
mock_part = MagicMock()
|
||||
mock_part.text = "Model response"
|
||||
chat_response.parts = [mock_part]
|
||||
result = await conversation.async_converse(
|
||||
hass, "hello", None, context, agent_id=mock_config_entry.entry_id
|
||||
)
|
||||
|
||||
assert (
|
||||
result.response.response_type == intent.IntentResponseType.ACTION_DONE
|
||||
), result
|
||||
assert (
|
||||
"The user name is Test User."
|
||||
in mock_model.mock_calls[1][2]["history"][0]["parts"]
|
||||
)
|
||||
assert "The user id is 12345." in mock_model.mock_calls[1][2]["history"][0]["parts"]
|
||||
|
||||
|
||||
async def test_conversation_agent(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
|
||||
Reference in New Issue
Block a user