Remove from LLM

This commit is contained in:
Michael Hansen 2024-10-29 09:29:34 -05:00
parent 736a5d4a94
commit 5ac873939f
9 changed files with 9 additions and 334 deletions

View file

@ -159,11 +159,6 @@ class AnthropicConversationEntity(
return conversation.ConversationResult(
response=intent_response, conversation_id=user_input.conversation_id
)
if external_result := await llm_api.api.async_handle_externally(user_input):
# Handled externally
return external_result
tools = [
_format_tool(tool, llm_api.custom_serializer) for tool in llm_api.tools
]

View file

@ -204,11 +204,9 @@ class GoogleGenerativeAIConversationEntity(
"""Process a sentence."""
result = conversation.ConversationResult(
response=intent.IntentResponse(language=user_input.language),
conversation_id=(
user_input.conversation_id
if user_input.conversation_id in self.history
else ulid.ulid_now()
),
conversation_id=user_input.conversation_id
if user_input.conversation_id in self.history
else ulid.ulid_now(),
)
assert result.conversation_id
@ -236,11 +234,6 @@ class GoogleGenerativeAIConversationEntity(
f"Error preparing LLM API: {err}",
)
return result
if external_result := await llm_api.api.async_handle_externally(user_input):
# Handled externally
return external_result
tools = [
_format_tool(tool, llm_api.custom_serializer) for tool in llm_api.tools
]
@ -304,9 +297,9 @@ class GoogleGenerativeAIConversationEntity(
trace.ConversationTraceEventType.AGENT_DETAIL,
{
# Make a copy to attach it to the trace event.
"messages": (
messages[:] if supports_system_instruction else messages[2:]
),
"messages": messages[:]
if supports_system_instruction
else messages[2:],
"prompt": prompt,
"tools": [*llm_api.tools] if llm_api else None,
},

View file

@ -172,11 +172,6 @@ class OllamaConversationEntity(
return conversation.ConversationResult(
response=intent_response, conversation_id=user_input.conversation_id
)
if external_result := await llm_api.api.async_handle_externally(user_input):
# Handled externally
return external_result
tools = [
_format_tool(tool, llm_api.custom_serializer) for tool in llm_api.tools
]

View file

@ -153,11 +153,6 @@ class OpenAIConversationEntity(
return conversation.ConversationResult(
response=intent_response, conversation_id=user_input.conversation_id
)
if external_result := await llm_api.api.async_handle_externally(user_input):
# Handled externally
return external_result
tools = [
_format_tool(tool, llm_api.custom_serializer) for tool in llm_api.tools
]

View file

@ -16,12 +16,8 @@ from voluptuous_openapi import UNSUPPORTED, convert
from homeassistant.components.climate import INTENT_GET_TEMPERATURE
from homeassistant.components.conversation import (
ConversationInput,
ConversationResult,
ConversationTraceEventType,
async_conversation_trace_append,
async_handle_intents,
async_handle_sentence_triggers,
)
from homeassistant.components.cover import INTENT_CLOSE_COVER, INTENT_OPEN_COVER
from homeassistant.components.homeassistant import async_should_expose
@ -197,15 +193,6 @@ class API(ABC):
"""Return the instance of the API."""
raise NotImplementedError
async def async_handle_externally(
self, user_input: ConversationInput
) -> ConversationResult | None:
"""Try to handle the input using an external system or agent.
Returns None if the input could not be handled externally.
"""
return None
class IntentTool(Tool):
"""LLM Tool representing an Intent."""
@ -323,22 +310,6 @@ class AssistAPI(API):
custom_serializer=_selector_serializer,
)
async def async_handle_externally(
self, user_input: ConversationInput
) -> ConversationResult | None:
"""Try to handle the input using sentence triggers."""
if trigger_response := await async_handle_sentence_triggers(
self.hass, user_input
):
response = intent.IntentResponse(user_input.language)
response.async_set_speech(trigger_response)
return ConversationResult(response)
if intent_response := await async_handle_intents(self.hass, user_input):
return ConversationResult(intent_response)
return None
@callback
def _async_get_api_prompt(
self, llm_context: LLMContext, exposed_entities: dict | None
@ -524,11 +495,9 @@ def _get_exposed_entities(
info["areas"] = ", ".join(area_names)
if attributes := {
attr_name: (
str(attr_value)
if isinstance(attr_value, (Enum, Decimal, int))
else attr_value
)
attr_name: str(attr_value)
if isinstance(attr_value, (Enum, Decimal, int))
else attr_value
for attr_name, attr_value in state.attributes.items()
if attr_name in interesting_attributes
}:

View file

@ -430,73 +430,6 @@ async def test_assist_api_tools_conversion(
assert tools
async def test_assist_api_handled_externally(
hass: HomeAssistant,
mock_config_entry_with_assist: MockConfigEntry,
mock_init_component,
) -> None:
"""Test that the Assist API handles sentence triggers and registered intents."""
agent_id = "conversation.claude"
context = Context()
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "conversation",
"command": ["my trigger"],
},
"action": {
"set_conversation_response": "my response",
},
}
},
)
# Handled by sentence trigger instead of LLM
result = await conversation.async_converse(
hass,
"my trigger",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.speech["plain"]["speech"] == "my response"
# Reuse custom sentences in test config to trigger default agent.
class OrderBeerIntentHandler(intent.IntentHandler):
intent_type = "OrderBeer"
def __init__(self) -> None:
super().__init__()
self.was_handled = False
async def async_handle(
self, intent_obj: intent.Intent
) -> intent.IntentResponse:
self.was_handled = True
return intent_obj.create_response()
handler = OrderBeerIntentHandler()
intent.async_register(hass, handler)
# Handled by registered intent instead of LLM
result = await conversation.async_converse(
hass,
"I'd like to order a stout",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.intent is not None
assert result.response.intent.intent_type == handler.intent_type
assert handler.was_handled
async def test_unknown_hass_api(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,

View file

@ -24,7 +24,6 @@ from homeassistant.const import ATTR_SUPPORTED_FEATURES, CONF_LLM_HASS_API
from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import intent, llm
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
from tests.typing import WebSocketGenerator
@ -459,73 +458,6 @@ async def test_function_exception(
)
@pytest.mark.usefixtures("mock_init_component")
async def test_assist_api_handled_externally(
hass: HomeAssistant,
mock_config_entry_with_assist: MockConfigEntry,
) -> None:
"""Test that the Assist API handles sentence triggers and registered intents."""
agent_id = mock_config_entry_with_assist.entry_id
context = Context()
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "conversation",
"command": ["my trigger"],
},
"action": {
"set_conversation_response": "my response",
},
}
},
)
# Handled by sentence trigger instead of LLM
result = await conversation.async_converse(
hass,
"my trigger",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.speech["plain"]["speech"] == "my response"
# Reuse custom sentences in test config to trigger default agent.
class OrderBeerIntentHandler(intent.IntentHandler):
intent_type = "OrderBeer"
def __init__(self) -> None:
super().__init__()
self.was_handled = False
async def async_handle(
self, intent_obj: intent.Intent
) -> intent.IntentResponse:
self.was_handled = True
return intent_obj.create_response()
handler = OrderBeerIntentHandler()
intent.async_register(hass, handler)
# Handled by registered intent instead of LLM
result = await conversation.async_converse(
hass,
"I'd like to order a stout",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.intent is not None
assert result.response.intent.intent_type == handler.intent_type
assert handler.was_handled
@pytest.mark.usefixtures("mock_init_component")
async def test_error_handling(
hass: HomeAssistant, mock_config_entry: MockConfigEntry

View file

@ -14,7 +14,6 @@ from homeassistant.const import ATTR_SUPPORTED_FEATURES, CONF_LLM_HASS_API, MATC
from homeassistant.core import Context, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import intent, llm
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@ -300,75 +299,6 @@ async def test_function_exception(
)
@patch("homeassistant.components.ollama.conversation.llm.AssistAPI._async_get_tools")
async def test_assist_api_handled_externally(
mock_get_tools,
hass: HomeAssistant,
mock_config_entry_with_assist: MockConfigEntry,
mock_init_component,
) -> None:
"""Test that the Assist API handles sentence triggers and registered intents."""
agent_id = mock_config_entry_with_assist.entry_id
context = Context()
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "conversation",
"command": ["my trigger"],
},
"action": {
"set_conversation_response": "my response",
},
}
},
)
# Handled by sentence trigger instead of LLM
result = await conversation.async_converse(
hass,
"my trigger",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.speech["plain"]["speech"] == "my response"
# Reuse custom sentences in test config to trigger default agent.
class OrderBeerIntentHandler(intent.IntentHandler):
intent_type = "OrderBeer"
def __init__(self) -> None:
super().__init__()
self.was_handled = False
async def async_handle(
self, intent_obj: intent.Intent
) -> intent.IntentResponse:
self.was_handled = True
return intent_obj.create_response()
handler = OrderBeerIntentHandler()
intent.async_register(hass, handler)
# Handled by registered intent instead of LLM
result = await conversation.async_converse(
hass,
"I'd like to order a stout",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.intent is not None
assert result.response.intent.intent_type == handler.intent_type
assert handler.was_handled
async def test_unknown_hass_api(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,

View file

@ -506,73 +506,6 @@ async def test_assist_api_tools_conversion(
assert tools
async def test_assist_api_handled_externally(
hass: HomeAssistant,
mock_config_entry_with_assist: MockConfigEntry,
mock_init_component,
) -> None:
"""Test that the Assist API handles sentence triggers and registered intents."""
agent_id = mock_config_entry_with_assist.entry_id
context = Context()
assert await async_setup_component(
hass,
"automation",
{
"automation": {
"trigger": {
"platform": "conversation",
"command": ["my trigger"],
},
"action": {
"set_conversation_response": "my response",
},
}
},
)
# Handled by sentence trigger instead of LLM
result = await conversation.async_converse(
hass,
"my trigger",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.speech["plain"]["speech"] == "my response"
# Reuse custom sentences in test config to trigger default agent.
class OrderBeerIntentHandler(intent.IntentHandler):
intent_type = "OrderBeer"
def __init__(self) -> None:
super().__init__()
self.was_handled = False
async def async_handle(
self, intent_obj: intent.Intent
) -> intent.IntentResponse:
self.was_handled = True
return intent_obj.create_response()
handler = OrderBeerIntentHandler()
intent.async_register(hass, handler)
# Handled by registered intent instead of LLM
result = await conversation.async_converse(
hass,
"I'd like to order a stout",
None,
context,
agent_id=agent_id,
)
assert result is not None
assert result.response.intent is not None
assert result.response.intent.intent_type == handler.intent_type
assert handler.was_handled
async def test_unknown_hass_api(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,