Add API class to LLM helper (#117707)

* Add API class to LLM helper

* Add more tests

* Rename intent to assist to broaden scope
This commit is contained in:
Paulus Schoutsen
2024-05-18 21:14:05 -04:00
committed by GitHub
parent bfc52b9fab
commit d001e7daea
2 changed files with 125 additions and 42 deletions

View File

@@ -10,11 +10,33 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, intent, llm
async def test_get_api_no_existing(hass: HomeAssistant) -> None:
"""Test getting an llm api where no config exists."""
with pytest.raises(HomeAssistantError):
llm.async_get_api(hass, "non-existing")
async def test_register_api(hass: HomeAssistant) -> None:
"""Test registering an llm api."""
api = llm.AssistAPI(
hass=hass,
id="test",
name="Test",
prompt_template="Test",
)
llm.async_register_api(hass, api)
assert llm.async_get_api(hass, "test") is api
assert api in llm.async_get_apis(hass)
with pytest.raises(HomeAssistantError):
llm.async_register_api(hass, api)
async def test_call_tool_no_existing(hass: HomeAssistant) -> None:
"""Test calling an llm tool where no config exists."""
with pytest.raises(HomeAssistantError):
await llm.async_call_tool(
hass,
await llm.async_get_api(hass, "intent").async_call_tool(
llm.ToolInput(
"test_tool",
{},
@@ -27,8 +49,8 @@ async def test_call_tool_no_existing(hass: HomeAssistant) -> None:
)
async def test_intent_tool(hass: HomeAssistant) -> None:
"""Test IntentTool class."""
async def test_assist_api(hass: HomeAssistant) -> None:
"""Test Assist API."""
schema = {
vol.Optional("area"): cv.string,
vol.Optional("floor"): cv.string,
@@ -42,8 +64,11 @@ async def test_intent_tool(hass: HomeAssistant) -> None:
intent.async_register(hass, intent_handler)
assert len(list(llm.async_get_tools(hass))) == 1
tool = list(llm.async_get_tools(hass))[0]
assert len(llm.async_get_apis(hass)) == 1
api = llm.async_get_api(hass, "assist")
tools = api.async_get_tools()
assert len(tools) == 1
tool = tools[0]
assert tool.name == "test_intent"
assert tool.description == "Execute Home Assistant test_intent intent"
assert tool.parameters == vol.Schema(intent_handler.slot_schema)
@@ -66,7 +91,7 @@ async def test_intent_tool(hass: HomeAssistant) -> None:
with patch(
"homeassistant.helpers.intent.async_handle", return_value=intent_response
) as mock_intent_handle:
response = await llm.async_call_tool(hass, tool_input)
response = await api.async_call_tool(tool_input)
mock_intent_handle.assert_awaited_once_with(
hass,